Help with a C program assignment. Please?
Ok I am currently working on an assignment that creates a 10x15 two-dimensional array for John Conway's "Game of Life". I need to get user input for the "occupied" slots in the array and then print the grid. I have this done. I then need to generate new generations of the the grid based on the specifications in the game. This requires two functions, one deciding the number of "neighbors" around a coordinate and one deciding the the new generation and putting it into a new array.
I am having problems with my function. I know I need a temporary array to be able to write over my old array once I have my new generation. I am not sure how I can take the new generated array and be able to print it using my function.
Here is my code so far. Any suggestions for my problem? I am very confused and lost on how to achieve thise.
Code:
#include <stdio.h>
#define SIZE 10
#define SIZE2 15
#define max(A,B) ( (A) > (B) ? (A):(B))
#define min(A,B) ( (A) < (B) ? (A):(B))
void initconfig(int [][SIZE2], int, int, int);
int occ(int [][SIZE2], int, int);
int* generate(int [][SIZE2], int, int);
void printgrid(int [][SIZE2], int, int );
int main ()
{
int coordArray[SIZE][SIZE2] = {0}, x = 0, y = 0,
generations = 0;
initconfig(coordArray, x, y, generations);
printf("\n\n\nInitial State\n\n");
printgrid(coordArray, SIZE, SIZE2);
printgrid(generate(coordArray, SIZE, SIZE2), SIZE, SIZE2);
return 0;
}
void initconfig(int coordArray[][SIZE2], int x, int y, int generations)
{
printf("\nEnter the number of generations: ");
scanf("%d", &generations);
printf("\nEnter coordinates: ");
scanf("%d%d", &x, &y);
while (x != -1){
if(x<1 || x>10 || y<1 || y>15)
printf("\nInvalid input.");
else
coordArray[x-1][y-1] = 1;
printf("\nEnter coordinates: ");
scanf("%d%d", &x, &y);
}
}
int* generate(int coordArray[][SIZE2], int row, int col)
{
int tempArray[SIZE][SIZE2] = {0}, i=0, j=0;
for(i=0; i <= row - 1; i++){
for(j=0; j <= col - 1; j++){
if(coordArray[i][j] == 1){
if(occ(coordArray, i, j) == 2)
tempArray[i][j] = 1;
if(occ(coordArray, i, j) == 3)
tempArray[i][j] = 1;
else
tempArray[i][j] = 0;}
if(coordArray[i][j] == 0){
if(occ(coordArray, i, j) == 3)
tempArray[i][j] = 1;
else
tempArray[i][j] = 0;}
}}
for(i=0; i<= row - 1; i++){
for(j=0; j <= col - 1; j++){
coordArray[i][j] = tempArray[i][j];}}
return coordArray;
}
int occ( int coordArray[][SIZE2], int x, int y )
{
int neighbourCount = 0, i, j;
for(i = max( 0, x - 1 ) ; i < min( SIZE2, x + 1 ); i++ )
{
for(j = max( 0, y - 1 ) ; j < min( SIZE, y + 1 ); j++ )
{
if ( ( x == i ) & ( y == j ) )
{
continue;
}
if ( coordArray[ i ][ j ] == 1 )
{
neighbourCount++ ;
}
}
}
return neighbourCount;
}
void printgrid(int coordArray[][SIZE2], int row, int col)
{
int i, j;
printf("\n");
for(i=0; i <= row - 1; i++){
for(j=0; j <= col - 1; j++){
if(coordArray[i][j] == 1){
printf("*");}
else
printf("-");}
printf("\n\n");
}
}