I'm having problems setting up the grid for this problem and i'm not really sure if it's completely correct..So could you please help me out where you can..

Here's the question:

WRITE A PROGAM TO STIMULATE THE “GAME OF LIFE”. THERE IS A CITY THAT IS LAID OUT AS A GRID. EACH CELL IN THE GRID CAN BE EITHER ALIVE OR DEAD. EACHCELL HAS A MAXIMUM OF EIGHT NEIGHBOURS, WHICH ARE THE CELLS ADJACENT TO IT IN THE GRID. IF A CELL HAS MORE THAN FOUR NEIGHBOURS, IT WILL DIE OF OVERCROWDING IN THE GENERATION. IF IT HAS LESS THAN THREE NEIGHBOURS IT WILL ALSO DIE IN THE NEXT GENERATION FROM LONELINESS, OTHERWISE THE CELL LIVES IN THE NEXT GENERATION. THE GRID HAS NO BOUNDARIES, SO IF YOU ARE AT THE LAST CELL OF THE RIGHT EDGE OF THE GRID AND MOVE ONE CELL TO THE RIGHT, YOU WILL BE IN THE FIRST OF THE LEFT EDGE OF THE GRID AND VICE VERSA. IF YOU ARE THE LAST CELL AT THE BOTTOM OF THE GRID AND MOVE DOWN ONE CELL, YOU WILL BE AT THE FIRST CELL AT THE TOP OF THE GRID AND VICE VERSA. THE PROGRAM SHOULD RANDOMLY POPULATE A 50*50 GRID WITH LIVING CELLS, DISPLAY THE GRID ON THE SCREEN AND SHOULD ITERATE THROUGH 1000 GENERATIONS. AFTER EACH GENERATION THE NEW GRID IS DISPLAYED ON THE SCREEN.

And here's the code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int array[50][50]={0};
int array1[50][50]={0};

int function1(int num,int num2)
{
int num3;
num3=array[num][num2];
return num3;
}

int function2(int num,int num2)
{
int num3=0;

if(num<0&&num2<0)
{
num3=function1(49,49);
return num3;
}
else
if(num<0&&num2>49)
{
num3=function1(49,0);
return num3;
}
else
if(num>49&&num2<0)
{
num3=function1(0,49);
return num3;
}
else
if(num>49&&num2>49)
{
num3=function1(0,0);
return num3;
}
else
if(num<0&&num2>=0)
{
num3=function1(49,num2);
return num3;
}
else
if(num>49&&num2>=0)
{
num3=function1(0,num2);
return num3;
}
else
if(num>=0&&num2<0)
{
num3=function1(num,49);
return num3;
}
else
if(num>=0&&num2>49)
{
num3=function1(num,0);
return num3;
}
return 0;
}

void function3()
{
int num,num2;
for(num=0;num<50;num++)
{
for(num2=0;num2<50;num2++)
{
array[num][num2]=array1[num][num2];
}
}
for(num=0;num<50;num++)
{
for(num2=0;num2<50;num2++)
{
array1[num][num2]=0;
}
}
}

int function4(int num,int num2)
{
int num3=0;
if((num<0||num>49)||(num2<0||num2>49))
{
num3=function2(num,num2);
return num3;
}
else
{
num3=function1(num,num2);
return num3;
}
}

int function5(int num,int num2)
{
int num3=0;

if((function4((num-1),(num2-1)))==1)
{
num3++;
}
if((function4((num-1),num2))==1)
{
num3++;
}
if((function4((num-1),(num2+1)))==1)
{
num3++;
}
if((function4(num,(num2+1)))==1)
{
num3++;
}
if((function4((num+1),(num2+1)))==1)
{
num3++;
}
if((function4((num+1),num2))==1)
{
num3++;
}
if((function4((num+1),(num2-1)))==1)
{
num3++;
}
if((function4(num,(num2-1)))==1)
{
num3++;
}
return num3;
}

void function7()
{
int num,num2;
for(num=0;num<50;num++)
{
for(num2=0;num2<50;num2++)
{
if(((function5(num,num2))==4)||((function5(num,num2))==3))
{
array1[num][num2]=1;
}
else
{
array1[num][num2]=0;
}
}
}
}

void main()
{
int num,num2,num3;

array[49][0]=1;
array[49][49]=1;
array[0][0]=1;
array[0][1]=1;
array[0][2]=1;
array[0][3]=1;
array[0][4]=1;
array[0][5]=1;
array[0][6]=1;
array[0][7]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;
array[2][49]=1;

for(num=0;num<50;num++)
{
for(num2=0;num2<50;num2++)
{
if((array[num][num2])==1)
{
printf("*");
}
else
{
printf(" ");
}
}printf("\n");
}

for(num3=0;num3<300;num3++)
{
system("cls");
function7();
function3();
for(num=0;num<50;num++)
{
for(num2=0;num2<50;num2++)
{
if((array[num][num2])==1)
{
printf("*");
}
else
{
printf(" ");
}
}printf("\n");
}Sleep(240);
}
}```

2. You should really give your functions and variables meaningful names and comment your code. It makes it a lot easier to understand what's going on

What problem is it that you're having, exactly?

3. Basically i think i have everything else down ok,except for the whole grid situation..
The whole idea is for it to be like John Conway's "Game of Life", and it should all be in a grid. But i don't know how to do that..I'm new to programming so please bear with me.

4. Void main should be int main
Is this C or C++ code?
Also your functions should have prototypes before they are implemented

5. Maybe you should start off smaller and build up to the full program? So, work on a function that draws the grid.

I don't know what you're referring to re: Conway's Game of Life, but I can't imagine this code does what you want to do... Start off small and build up to the full program. Label your variables and functions meaningfully.. eg. Score instead of num1.. or whatever

Atm you have stuff like this in there :-
Code:
```array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;
array[1][49]=1;```
I mean every one of those lines does the same thing, hehe, so.... you probably don't know what this code really does either

6. Code:
```int array[50][50]={0};
int array1[50][50]={0};```
2D arrays must be initialized with
Code:
`{{0}}`

7. Originally Posted by dwks
2D arrays must be initialized with
Code:
`{{0}}`
Uh, really?

The problem with the code is that it is probably too long, it doesn't use descriptive names, it does use magic numbers all over the place etc. Therefore it is somewhat hard to help with that.

In general, with a game of life, you'll need something like that:

1) Mark cells randomly to be alive.

2) Loop over the grid and count the living neighbours of each cell. Since your world wraps around, you can probably use the modulo operator (&#37 and won't need any special cases for borders at all.
Depending on the count of neighbours make a desicion what will happen to this cell in the next generation. But don't modify the cell right away. Instead save the decision somewhere (you'll need another array for that).

3) Loop over the grid again, and change the state of the cells according to the decisions that you have stored.

4) Output and repeat with 2).

By the way, do "dead" cells come to life too?

8. > Uh, really?
Oops. I was thinking of something else . . .

By the way, do "dead" cells come to life too?
Yes. http://en.wikipedia.org/wiki/Conway's_Game_of_Life

The problem with the code is that it is probably too long, it doesn't use descriptive names, it does use magic numbers all over the place etc. Therefore it is somewhat hard to help with that.
Yes, definitely. I'm sure the OP can think of better names than function1, function2 etc.

Also your functions should have prototypes before they are implemented
Function prototypes are not strictly required here, since every function call comes after the function has been defined.