-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmahdad_hw1.c
201 lines (144 loc) · 4.42 KB
/
mahdad_hw1.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/******************************************************************************
* Name: Ahmed Tanvir Mahdad *
* BlazerID: mahdad *
* Course Section: CS 732 *
* Homework #1 *
* To Compile: gcc -O mahdad_hw1.c (to print matrices add -DINTPRINT) *
* To run: ./a.out <problem_size> <maximum_step> * *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
//comparison_flag will track if the present state of game is same as next state.
//Default value is 0, will set to 1 when match found
int comparison_flag=0;
double gettime(void) {
struct timeval tval;
gettimeofday(&tval, NULL);
return( (double)tval.tv_sec + (double)tval.tv_usec/1000000.0 );
}
double **allocarray(int P, int Q) {
int i;
double *p, **a;
p = (double *)malloc(P*Q*sizeof(double));
a = (double **)malloc(P*sizeof(double*));
if (p == NULL || a == NULL)
printf("Error allocating memory\n");
/* for row major storage */
for (i = 0; i < P; i++)
a[i] = &p[i*Q];
return a;
}
double **initarray(double **a, int mrows, int ncols, int value) {
int i,j;
for (i=0; i<mrows; i++)
for (j=0; j<ncols; j++)
if(i==0 || j==0 || i==mrows-1 || j==ncols-1)
a[i][j]=0;
else
a[i][j] = (int) (drand48()*value);
//a[i][j] = value;
return a;
}
void printarray(double **a, int mrows, int ncols) {
int i,j;
for (i=0; i<mrows; i++) {
for (j=0; j<ncols; j++)
printf("%f ", a[i][j]);
printf("\n");
}
}
double **arrayCopy(double **a,double **b,int N){
int i,j;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
a[i][j]=b[i][j];
return a;
}
/* Game of Life Function*/
double **gameOfLife(double **a, double **b,int N)
{
//flip -- It will track if the next array is different then the present array
int i, j, k;
int flip=0;
int neighbourNumber;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
if(i==0 || j==0 || i==N-1 || j==N-1)
b[i][j]=0;
else {
neighbourNumber=a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1];
if(a[i][j]==1){
if(neighbourNumber==2 || neighbourNumber==3)
b[i][j]=1;
else{
b[i][j]=0;
flip=1;
}
}else{
if(neighbourNumber==3){
b[i][j]=1;
flip=1;
}
else
b[i][j]=0;
}
}
if(flip==0){
comparison_flag=1;
}
return b;
}
int main(int argc, char **argv)
{
srand(123456);
int N, i, j, k,max, comparison;
int count=0;
double **present=NULL, **nextArr=NULL;
double starttime, endtime;
if (argc != 3) {
printf("Usage: %s <N>\n", argv[0]);
exit(-1);
}
N = atoi(argv[1]);
max=atoi(argv[2]);
/* Allocate memory for all three matrices and temporary arrays */
present = allocarray(N+2, N+2);
nextArr = allocarray(N+2, N+2);
/* Initialize the matrices */
srand48(123456);
present = initarray(present, N+2, N+2, 2);
nextArr = initarray(nextArr, N+2, N+2, 0);
#ifdef INTPRINT
printarray(present,N+2,N+2);
printf("\n");
#endif
//caluclating time on main loop
starttime = gettime();
//while loop will implement game of life until maximum iteration or present or next state is same
while(count<max){
nextArr=gameOfLife(present,nextArr,N+2);
count++;
#ifdef HALT_CHECKER
if(comparison_flag==1)
printf("Game halted at: %d\n",count );
#endif
#ifdef INTPRINT
printarray(nextArr,N+2,N+2);
printf("\n");
#endif
//If present and next state is same, exit from the while loop
if(comparison_flag==1)
break;
present=arrayCopy(present,nextArr,N+2);
}
endtime = gettime();
#ifdef TOTALSTEP
printf("Total Step is: %d\n",count );
#endif
//Freeing the memory
free(present);
free(nextArr);
printf("Time taken = %lf seconds\n", endtime-starttime);
return 0;
}