User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,758 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,723 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 5497 | Replies: 33 | Solved
Reply
Join Date: Aug 2005
Location: Indiana
Posts: 57
Reputation: Loopah is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Loopah's Avatar
Loopah Loopah is offline Offline
Junior Poster in Training

Please point me in the right way for the ending of my C program.

  #1  
Nov 14th, 2005
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


#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"); 
  } 
} 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please point me in the right way for the ending of my C program.

  #2  
Nov 15th, 2005
I would like a little help on how to reset the grid to show the next generation though.
Like this?
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;
      }
   }
} 
Don't you need to implement a function occ to determine the number of occupied cells around cell[x,y] for the next generation, rather than resetting?
Reply With Quote  
Join Date: Aug 2005
Location: Indiana
Posts: 57
Reputation: Loopah is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Loopah's Avatar
Loopah Loopah is offline Offline
Junior Poster in Training

Re: Please point me in the right way for the ending of my C program.

  #3  
Nov 15th, 2005
Don't you need to implement a function occ to determine the number of occupied cells around cell[x,y] for the next generation, rather than resetting?

Eep, yes that is what I meant to ask. Any help on that? Sorry for the confusion.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please point me in the right way for the ending of my C program.

  #4  
Nov 15th, 2005
Something like this, I'd imagine.
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;
}
Untried, untested, etc.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Please point me in the right way for the ending of my C program.

  #5  
Nov 15th, 2005
Or something similar to this.
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;
}
Check the conditions as your Rows and Columns start from 1 and the arrays begin from 0.
Reply With Quote  
Join Date: Aug 2005
Location: Indiana
Posts: 57
Reputation: Loopah is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Loopah's Avatar
Loopah Loopah is offline Offline
Junior Poster in Training

Re: Please point me in the right way for the ending of my C program.

  #6  
Nov 15th, 2005
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Please point me in the right way for the ending of my C program.

  #7  
Nov 15th, 2005
Originally Posted by Loopah
Ok, so by implementing that... I would take the "neighbor" variable and do what with it?
Use it later...
Originally Posted by Loopah
That is the occ function and I know I somehow need to implement that to my "new generation" function.
Now that you can get a count of the number of heighbors...
Originally Posted by Loopah
Do I take the neighbor variable and like put it into the generation function?
You take the return value to determine what to do with each cell in the 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?
It counts the number of occupied neighbors. It takes care of nothing. It counts the neighbors.
Originally Posted by Loopah
I'm sorry, I just can't seem to understand the occ function's purpose.
Methinks the generate function that you'll need to write might make use of it, though.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Please point me in the right way for the ending of my C program.

  #8  
Nov 15th, 2005
Quoted from Wikipedia.
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.
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.
Reply With Quote  
Join Date: Aug 2005
Location: Indiana
Posts: 57
Reputation: Loopah is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Loopah's Avatar
Loopah Loopah is offline Offline
Junior Poster in Training

Re: Please point me in the right way for the ending of my C program.

  #9  
Nov 15th, 2005
Ok, awesome. I am up to the generate function and have all that coded up. I am just wondering now, what happens if an inputted coord is on the border of the grid? When it goes to do (row - 1) and isn't in the array anymore, is there a way to put this into the occ function?
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Please point me in the right way for the ending of my C program.

  #10  
Nov 15th, 2005
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 1:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC