Thread: issues working with two-dimensional array

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    21

    issues working with two-dimensional array

    Hi--I'm trying to implement Conway's Game of Life on a 10x10 board (with borders), and I'm having a bit of trouble. At this point, it seems most likely that said trouble lies in either my display function or the for loop I use to call it in main, so here's the code in question:
    Code:
    void display_life(char life[12][12]){
    	int row, column, status;
    	char enter;
    	printf("\n\n");
    	for(row=0; row<=11; row++){
    		for(column=0; column<=11; column++){
    			printf("&#37;c", life[row][column]);
    			if(column==11){
    				printf("\n");
    			}//if column==11
    		}//for column
    	}//for row
    	do{
    		printf("\nPress the Enter key to continue.");
    		status=scanf("%c", &enter);
    	}while(status!=1);
    } //displays the life board and waits for the user to press the Enter key.
    
    int main(){
    	char life[12][12];
    	int count, status;
    	init_life(life);
    	display_life(life);
    	for(count=1; count<9; count++){
    		update_life(life);
    	}//for(count=0; count<10; count++)*/
    	return 0;
    }//main
    Now, as far as I can tell, display_life should wait for the user to press return before main moves on to the for loop, but this is the output I get on running the program:

    Code:
    teyla:~ quasigreat$ ~quasigreat/a.out
    Please enter the file name>> Life1.dat
    
    
    |__________|
    |XXXXXXXXXX|
    |          |
    |XXXXXXXXXX|
    |XXXXXXXXXX|
    |          |
    |XXXXXXXXXX|
    |XXXXXXXXXX|
    |XXXXXXXXXX|
    |          |
    |XXXXXXXXXX|
    |__________|
    
    Press the Enter key to continue.
    
    |__________|
    |X         |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |          |
    |__________|
    
    Press the Enter key to continue.
    To clarify, it displays the second board without waiting for keyboard input. Any help would be much appreciated. (I might even offer to sacrifice my firstborn. You never know.)
    Last edited by quasigreat; 05-17-2008 at 12:02 AM. Reason: clarification

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Please enter the file name>> Life1.dat
    You probably have a newline left in the input buffer after the user enters the file name, which is subsequently read by this call to scanf:
    > status=scanf("%c", &enter);
    One solution is to add the following code after the user enters the file name:
    Code:
    while (getchar() != '\n');
    Another solution is to use fgets() to read the file name entered by the user.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    21
    Thank you! It's fixed now.

    I wonder if I might trouble you further to take a look at my update_life function, as it doesn't seem to be working. It's meant to count the number of Xs in neighboring cells and apply the rules accordingly, but I'm getting the output shown in my original post--one sad little X in the upper left corner. I've separated out the block that applies the rules and highlighted the comments, in an attempt to make it a little easier to read:

    Code:
    void update_life(char life[12][12]){ 
    	char new_life[12][12];
    	int row, column, count;
    	for(row=1; row<=10; row++){
    		for(column=1; column<=10; column++){
    			if(life[row-1][column-1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor up and to the left
    			if(life[row][column-1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor to the left
    			if(life[row+1][column-1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor down and to the left
    			if(life[row-1][column]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor above
    			if(life[row+1][column]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor below
    			if(life[row-1][column+1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor above and to the right
    			if(life[row][column+1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor to the right
    			if(life[row+1][column+1]=='X'){
    				count++;
    			} else {
    				count=count;
    			}//check neighbor below and to the right
    			
    			if(life[row][column]==' ' && count==3){
    				new_life[row][column]='X'; //birth
    			} else if(life[row][column]=='X' && count>=2 && count<=3){
    				new_life[row][column]='X'; //survival
    			} else if(life[row][column]=='X' && (count<2 || count>3)){
    				new_life[row][column]=' '; //death
    			} else {
    				new_life[row][column]=life[row][column];
    			}
    			
    		}//for column
    	}//for row
    	for(row=1; row<=10; row++){
    		for(column=1; column<=10; column++){
    			life[row][column]=new_life[row][column];
    		}//for column
    	}//for row
    	display_life(life);
    }
    Last edited by quasigreat; 05-17-2008 at 01:09 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It's meant to count the number of Xs in neighboring cells and apply the rules accordingly,
    One problem I see is you never initialize count to 0 at the start of the if() checks. Also, while it doesn't do any harm, all of the:
    Code:
    } else {
    	count=count;
    }
    statements are unnecessary, as count will by default keep it previous value.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1) You need to initialize count to zero, inside the for loop, before you start counting for any new square.

    2) You need to understand and correct your descriptions. This is incorrect:
    Code:
    }//check neighbor up and to the left
    			if(life[row][column-1]=='X'){
    				count++;
    			} else {
    				count=count;
    Here, you are not checking "up and to the left". You are checking to 9:00 o'clock (just to the left).

    There is no need for the count = count, line. Somehow, someway, that should just intuitively strike you as silly.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    21
    Thank you both very much, it's working perfectly now.

    The comments refer to the previous if statement, i.e. "check neighbor up and to the left" refers to if(life[row-1][column-1]=='X'). And count=count did strike me as a little silly, but earlier gcc had given me an error message that for some reason I interpreted as complaining about the lack of elses after my ifs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Array Issues
    By GCNDoug in forum C Programming
    Replies: 12
    Last Post: 04-16-2007, 12:27 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM