-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
62 lines (62 loc) · 1.14 KB
/
test.cpp
File metadata and controls
62 lines (62 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<iostream>
#include<string>
void display(int *a,int n)
{
int i,j;
int *c=a;
system("clear");
printf("Controls\n\n W\n\nA S D\n\n");
for(i=0;i<n;i++)
{
printf("\t\t\t\b");
for(j=0;j<n;j++)
{
c=a+(i*n)+j;
if(*c !=0)
printf("%4d",*c);
else
printf(" ");
printf(" |\t");
}
printf("\n\n");
}
}
int main()
{
int n;
printf("Enter the dimension");
scanf("%d",&n);
int *a=(int*)malloc(n*n*sizeof(int));
int *c=a;
int e=0,i,j,r;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c=a+(i*n)+j;
if(*c==0) e++;
}
}
printf("%d",e);
if(e==0) return 0;
r=(int)(e*((float)rand()/RAND_MAX))+1;
printf("%d",r);
e=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c=a+(i*n)+j;
if(*c==0) e++;
if(e==r)
{
if(rand()%2==0) *c=2;
else *c=4;
break;
}
}
if(e==r) break;
}
display(a,n);
}