Please support our C advertiser: Programming Forums
![]() |
Heya everyone. I am a first-year student and doing a project for my C class. It is to do with John Conway's "Game of Life" as I am sure you are all aware of what that is. Here is the things needed. And after that is the code that I have. I have done everything up to printing the original config that the user gives. I would like a little help on how to reset the grid to show the next generation though. Thanks so much.
Write a C program that:
* Reads coordinates entered by the user until a -1 is entered. Be sure to check each pair for validity assuming all inputs are integers. If a user enters an invalid value, ask the user to enter both coordinates again. If an invalid value is read froma file, terminate the program with an error message.
* Asks the user how many generations to calculate.
* Prints the initial configuration using an "*" for occupied cells and a "-" for unoccupied cells.
* Calculate and print successive generations.
* Print the generation number above the grid, print the statistics stating how many cells were "born" and how many cells "died" during this generation, then print the grid.
* Make sure that your prompts and outputs use EXACTLY the same format as the example.
You are to write at least 4 functions (note that I didn't specify the parameters or return values of these functions):
* initconfig( ) which reads in the initial configuration
* generate( ) which creates and prints a new generation of organisms
* occ( ) which returns the number of occupied cells around cell[x,y]
* printgrid( ) which prints a grid
Write a C program that:
* Reads coordinates entered by the user until a -1 is entered. Be sure to check each pair for validity assuming all inputs are integers. If a user enters an invalid value, ask the user to enter both coordinates again. If an invalid value is read froma file, terminate the program with an error message.
* Asks the user how many generations to calculate.
* Prints the initial configuration using an "*" for occupied cells and a "-" for unoccupied cells.
* Calculate and print successive generations.
* Print the generation number above the grid, print the statistics stating how many cells were "born" and how many cells "died" during this generation, then print the grid.
* Make sure that your prompts and outputs use EXACTLY the same format as the example.
You are to write at least 4 functions (note that I didn't specify the parameters or return values of these functions):
* initconfig( ) which reads in the initial configuration
* generate( ) which creates and prints a new generation of organisms
* occ( ) which returns the number of occupied cells around cell[x,y]
* printgrid( ) which prints a grid
#include <stdio.h>
#define SIZE 10
#define SIZE2 15
void initconfig(int [][SIZE2], int, 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);
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);
}
}
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");
}
} •
•
•
•
I would like a little help on how to reset the grid to show the next generation though.
void resetgrid(int coordArray[][SIZE2], int row, int col)
{
int i, j;
for ( i = 0; i < row; i++ )
{
for ( j = 0; j < col; j++ )
{
coordArray[i][j] == 0;
}
}
} occ to determine the number of occupied cells around cell[x,y] for the next generation, rather than resetting? High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Something like this, I'd imagine.
Untried, untested, etc.
int occ(int coordArray[][SIZE2], int row, int col, int r, int c)
{
/*
* +---------+-------+---------+
* | r-1,c-1 | r-1,c | r-1,c+1 |
* +---------+-------+---------+
* | r,c-1 | [r,c] | r,c+1 |
* +---------+-------+---------+
* | r+1,c-1 | r+1,c | r+1,c+1 |
* +---------+-------+---------+
*/
int neighbors = 0;
if ( r - 1 >= 0 && c - 1 >= 0 && coordArray[r - 1][c - 1] )
{
++neighbors;
}
if ( r - 1 >= 0 && c >= 0 && coordArray[r - 1][ c ] )
{
++neighbors;
}
if ( r - 1 >= 0 && c + 1 < col && coordArray[r - 1][c + 1] )
{
++neighbors;
}
if ( r >= 0 && c - 1 >= 0 && coordArray[ r ][c - 1] )
{
++neighbors;
}
if ( r >= 0 && c + 1 < col && coordArray[ r ][c + 1] )
{
++neighbors;
}
if ( r + 1 < row && c - 1 >= 0 && coordArray[r + 1][c - 1] )
{
++neighbors;
}
if ( r + 1 < row && c >= 0 && coordArray[r + 1][ c ] )
{
++neighbors;
}
if ( r + 1 < row && c + 1 < col && coordArray[r + 1][c + 1] )
{
++neighbors;
}
return neighbors;
} High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
Or something similar to this.
Check the conditions as your Rows and Columns start from 1 and the arrays begin from 0.
int occ( int Grid[ ROW ][ COLUMN ], int x, int y )
{
int neighbourCount = 0 ;
for ( int i = max( 0, x - 1 ) ; i < min( COLUMN, x + 1 ); i++ )
{
for ( int j = max( 0, y - 1 ) ; j < min( ROW, y + 1 ); j++ )
{
if ( ( x == i ) & ( y == j ) )
{
continue;
}
if ( Grid[ i ][ j ] == 1 )
{
neighbourCount++ ;
}
}
}
return neighbourCount;
} Ok, so by implementing that... I would take the "neighbor" variable and do what with it? That is the occ function and I know I somehow need to implement that to my "new generation" function. Do I take the neighbor variable and like put it into the generation function? And for the unoccupied coords, if the coords around it have 3 occupied, then it turns occupied.. Does that occ function take care of that as well? I'm sorry, I just can't seem to understand the occ function's purpose.
•
•
•
•
Originally Posted by Loopah
Ok, so by implementing that... I would take the "neighbor" variable and do what with it?
•
•
•
•
Originally Posted by Loopah
That is the occ function and I know I somehow need to implement that to my "new generation" function.
•
•
•
•
Originally Posted by Loopah
Do I take the neighbor variable and like put it into the generation function?
generate function.•
•
•
•
Originally Posted by Loopah
And for the unoccupied coords, if the coords around it have 3 occupied, then it turns occupied.. Does that occ function take care of that as well?
•
•
•
•
Originally Posted by Loopah
I'm sorry, I just can't seem to understand the occ function's purpose.
generate function that you'll need to write might make use of it, though. High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
Quoted from Wikipedia.
Inside the generate( ) function, use occ to calculate the number of neighbours for all the cells in the grid, and according to the rules determine if the cell is dead or alive.
Because all the births and deaths occur simultaneously, dont update the original cell. Make a temporary array of the same dimensions and update the cell corresponding to the one in the original array.
After you have finished doing that to all the cells in the original array, copy the temp array into the original one.
•
•
•
•
1. Any live cell with fewer than two neighbors dies of loneliness.
2. Any live cell with more than three neighbors dies of crowding.
3. Any dead cell with exactly three neighbors comes to life.
4. Any live cell with two or three neighbors lives, unchanged, to the next generation.
It is important to understand that all births and deaths occur simultaneously. Together they constitute a single generation or, we could call it, a "tick" in the complete "life history" of the initial configuration.
•
•
•
•
I'm sorry, I just can't seem to understand the occ function's purpose.
Because all the births and deaths occur simultaneously, dont update the original cell. Make a temporary array of the same dimensions and update the cell corresponding to the one in the original array.
After you have finished doing that to all the cells in the original array, copy the temp array into the original one.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 102
Look at my occ function. The max and min functions take care of that.
int occ( int Grid[ ROW ][ COLUMN ], int x, int y )
{
int neighbourCount = 0 ;
for ( int i = max( 0, x - 1 ) ; i < min( COLUMN - 1, x + 1 ); i++ )
{
for ( int j = max( 0, y - 1 ) ; j < min( ROW - 1, y + 1 ); j++ )
{
if ( ( x == i ) & ( y == j ) )
{
continue;
}
if ( Grid[ i ][ j ] == 1 )
{
neighbourCount++ ;
}
}
}
return neighbourCount;
}![]() |
Similar Threads
Other Threads in the C Forum
Other Threads in the C Forum
- Previous Thread: Can this program be written?
- Next Thread: Program to send packet
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode