Like Tree2Likes

Game of life program

This is a discussion on Game of life program within the C Programming forums, part of the General Programming Boards category; Hi, Again a question from the book(Problem solving and program design in C) I solved the question.But output not as ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    Game of life program

    Hi,
    Again a question from the book(Problem solving and program design in C)
    I solved the question.But output not as I want to be.I mean it doesn't work as game of life
    Code:
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #define ROWS 25
    #define COLS 25
    void game_of_life(char arr[][COLS+2]);
    int checkx(char arr[][COLS+2], int row, int col);
    int main(void)
    {
    	char array[ROWS+2][COLS+2],n_array[ROWS+2][COLS+2];
    	int i, j, r_integer[ROWS+2][COLS+2], num_gen;
    	for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    			r_integer[j][i] = (rand()%2);
    		}
    	}
    	for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<2;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(n_array);
    		for(i=0;i<ROWS+2;i++){
    			for(j=0;j<COLS+2;j++){
    				n_array[j][i]=array[j][i];
    			}
    		}
    		for(i=0;i<ROWS+2;i++){
    			for(j=0;j<COLS+2;j++){
    				printf("%c", array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    void game_of_life(char arr[][COLS+2])
    {
    	int i, j, num_neighbors=0;
    	for(i=1;i<COLS+2;i++){     
    		for(j=1;j<ROWS+2;j++){
    			if ((arr[j][i])=='X'){
    				num_neighbors=checkx(arr, j, i);
    				if (num_neighbors <= 3 || num_neighbors >= 2)
    					arr[j][i]='X';
    				if (num_neighbors >= 4)  
    					arr[j][i]=' ';
    				else
    					arr[j][i]='X';
                }
    			else {
    				num_neighbors=checkx(arr, j, i);
                    if(num_neighbors == 3)   
    					arr[j][i]='X';
                    else
    					arr[j][i]=' ';
                  }
              }
          }
    }
    		
    int checkx(char arr[][COLS+2],int row, int col)
    {
          int num_neighbors = 0;
          if ((arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((arr[row-1][col])=='X')
              num_neighbors++;
          if ((arr[row-1][col-1])=='X')
    		  num_neighbors++;
          if ((arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((arr[row+1][col-1])=='X')
              num_neighbors++;
          if ((arr[row+1][col])=='X')
              num_neighbors++;
          if ((arr[row+1][col+1])=='X')
              num_neighbors++;
          return(num_neighbors);
    }
    Output is not changing

    X XXXX XX X XX X XX XXXX XXX X XXXX X X X X X XXX X X XXX X X X X X XX X X XX XXX X X XXX X XXXXXXXXXX XX X XX X XXX X X X X XXXX X X X XXXXX XX XX X XXXXX XX X XXX XXX X X X X XX X XXX X XX X XXX X X X XX XXXX X XXXX XXX X XX X X XX X XX XXXXX XX XX X X X XX X X XXX X XXX X X XXXX XX X X X X XXX X X XX XXXXX X X XX X X XXXX X XX XXX X XX XX XXXXX XXXX XX X XX X XXXXX X XX X X X X X XXXX X X XX XX XX X XXXXX XXX XXXXX X XX X X XXXX XXXXX XXX XXXXX XXXXXX XX XX X XXX XXXX XXX XXX XX XXX X X X X X X XX XXX X X X XXXXXX XX X XX XXX
    Generation: 1
    X XXXX XX X XX X XX XXXX XXX X XXXX X X X X X XXX X X XXX X X X X X XX X X XX XXX X X XXX X XXXXXXXXXX XX X XX X XXX X X X X XXXX X X X XXXXX XX XX X XXXXX XX X XXX XXX X X X X XX X XXX X XX X XXX X X X XX XXXX X XXXX XXX X XX X X XX X XX XXXXX XX XX X X X XX X X XXX X XXX X X XXXX XX X X X X XXX X X XX XXXXX X X XX X X XXXX X XX XXX X XX XX XXXXX XXXX XX X XX X XXXXX X XX X X X X X XXXX X X XX XX XX X XXXXX XXX XXXXX X XX X X XXXX XXXXX XXX XXXXX XXXXXX XX XX X XXX XXXX XXX XXX XX XXX X X X X X X XX XXX X X X XXXXXX XX X XX XXX
    Generation: 2
    X XXXX XX X XX X XX XXXX XXX X XXXX X X X X X XXX X X XXX X X X X X XX X X XX XXX X X XXX X XXXXXXXXXX XX X XX X XXX X X X X XXXX X X X XXXXX XX XX X XXXXX XX X XXX XXX X X X X XX X XXX X XX X XXX X X X XX XXXX X XXXX XXX X XX X X XX X XX XXXXX XX XX X X X XX X X XXX X XXX X X XXXX XX X X X X XXX X X XX XXXXX X X XX X X XXXX X XX XXX X XX XX XXXXX XXXX XX X XX X XXXXX X XX X X X X X XXXX X X XX XX XX X XXXXX XXX XXXXX X XX X X XXXX XXXXX XXX XXXXX XXXXXX XX XX X XXX XXXX XXX XXX XX XXX X X X X X X XX XXX X X X XXXXXX XX X XX XXX

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Part of the problem could be your random number generator...
    First you're using it without calling srand() to initialize a new sequence... thus you will always get the same sequence.
    Second some random number generators when used with %2 either give you 0 and 1 alternately or have a strong bias to 1.

    Try something like this...
    Code:
    #include <time.h>
    
    int main (void)
      { srand(time(NULL));  // do this only once
     
    // to generate 0 or 1
    x = (rand() % 100) > 50 ? 1 : 0;

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Look at when you change each array.

    In particular, you need to create a new array completely before making any changes to the old one.
    That is, life/death is calculated on the old (and unmodified) array. You're changing it as you go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Code:
    if (num_neighbors <= 3 || num_neighbors >= 2)
    That line looks suspicious. Is it not true for all values of num_neighbors?

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by CommonTater View Post
    Part of the problem could be your random number generator...
    First you're using it without calling srand() to initialize a new sequence... thus you will always get the same sequence.
    Second some random number generators when used with %2 either give you 0 and 1 alternately or have a strong bias to 1.

    Try something like this...
    Code:
    #include <time.h>
    
    int main (void)
      { srand(time(NULL));  // do this only once
     
    // to generate 0 or 1
    x = (rand() % 100) > 50 ? 1 : 0;
    Nothing changed.Generations are same;
    @Salem
    Look at when you change each array.

    In particular, you need to create a new array completely before making any changes to the old one.
    That is, life/death is calculated on the old (and unmodified) array. You're changing it as go
    I'm calling the game_of_life with a new array (n_array), and adding values of n_array to old array
    Code:
    for(num_gen=0;num_gen<2;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(n_array);
    		for(i=0;i<ROWS+2;i++){
    			for(j=0;j<COLS+2;j++){
    				n_array[j][i]=array[j][i];
    			}

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > game_of_life(n_array);
    This generates a new array, with no reference to the old array.

    > n_array[j][i]=array[j][i];
    Then this trashes what you just created.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by Salem View Post
    > game_of_life(n_array);
    This generates a new array, with no reference to the old array.

    > n_array[j][i]=array[j][i];
    Then this trashes what you just created.
    Ok I got it.But again, it isn't working right.
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    #define ROWS 25
    #define COLS 25
    void game_of_life(char o_arr[][COLS], char n_array[][COLS]);
    int checkx(char arr[][COLS],int row, int col);
    int main(void)
    {
    	srand(time(NULL));
    	char array[ROWS][COLS],n_array[ROWS][COLS];
    	int i, j, r_integer[ROWS][COLS], num_gen;
    	for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    			r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0;
    		}
    	}
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<2;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(array,n_array);
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				array[j][i]=n_array[j][i];
    			}
    		}
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				printf("%c", array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    void game_of_life(char o_arr[][COLS], char n_array[][COLS])
    {
    	int i, j, num_neighbors=0;
    	for(i=1;i<COLS;i++){     
    		for(j=1;j<ROWS;j++){
    			if ((o_arr[j][i])=='X'){
    				num_neighbors=checkx(o_arr, j, i);
    				if (num_neighbors <= 3 || num_neighbors >= 2)
    					n_array[j][i]='X';
    				else if (num_neighbors >= 4)  
    					n_array[j][i]=' ';
    				else
    					n_array[j][i]='X';
                }
    			else {
    				num_neighbors=checkx(o_arr, j, i);
                    if(num_neighbors == 3)   
    					n_array[j][i]='X';
                    else
    					n_array[j][i]=' ';
                  }
              }
          }
    }
    		
    int checkx(char arr[][COLS],int row, int col)
    {
          int num_neighbors = 0;
          if ((arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((arr[row-1][col])=='X')
              num_neighbors++;
          if ((arr[row-1][col-1])=='X')
    		  num_neighbors++;
          if ((arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((arr[row+1][col-1])=='X')
              num_neighbors++;
          if ((arr[row+1][col])=='X')
              num_neighbors++;
          if ((arr[row+1][col+1])=='X')
              num_neighbors++;
          return(num_neighbors);
    }
    mustafa@tosbag:~/Documents/My_C_Programs/Programming_Projects/CHAPTER8$ ./16
    X XXX X X XX XXX X XXX X XX X X XXXX XXXXXX X X XX X X XXXX XXX XXX XX XX XX XXX XX XX XXXX XXXX X X XX X X XX X X XX X XX X X XX X X X XX XXXXX X XXXX XX X X X X X XX XXX XXX X XXXXXXXXX XXXX XXX XX X XX X X X X XX XXX XX X X X XX X X XX XXXXX XXX X XX X X X XXX X XXX X XX X XXX XXXXX X X X XXX X XX X XX X XXXXX XX X X X XX X X X X X X X X XX X XXXX X X X X XXXX XX XXXXX X XXXX XX XX X X X XXXXXX XX XX XXXXXX X X X X X X X XXXX XX XX X X X X
    Generation: 1
    T�|z���`�&� X X XXX XX X XXXX XXXXXXX X XX X XX X X XXXX XXX XXX XXX XX XX XXX XX XX XXXX XXX X XX XX X X XX X X X XX XXXX XX XX X X X XX X X XX XXXXX XXXXXXX XX X XX XX XX X X XXX XXXXXXXX XX XXXXXXXXX XXX XXXX XXXX XX X X X XX X XXXX XXX XXXX X XXXXXX X XX XXXXXXXXX X XX X X X XX X XXX XX XX X XX XXXXX X X X X XXX X XX XX X XXX X XXXXXX XXXXX X X XXX X XX XXX X X X XX X XXX XX XXX X XXXX XXXXXXX X X XXXXX XX XX X X X XXXXXX XX XX XXXXXX X X X X X X X XXX XX XXXX X XXXXX X X
    Generation: 2
    T�|z���`�&� XX XXXXXXXX X XXXXXXXXXXXX X XX XX XX X X XXXX XXX XXXX XXX XX XX XXX XX XX XXXX XXXXX XX XXXXX X XX X XX X XX XXXXXXX XX X X X XX X X X XX XXXXXXX XXXXXXX XXXX XXXXX XX X X XXX XXXXXXXX XX XXXXXXXXX XXX XXXX XXXX XX X X X XX XX XXXX XXX XXXX X XXXXXX X X XX XXXXXXXXXXX XX XX X X XXX X XXX XX XX X XX XXXXX X X X X X XXX X XX X XXXX XXXX X XXXXXX XXXXX XX X XXX X XX XXX X X X XX X XXXXXX XXX X XXXX XXXXXXXXX X XXXXX XX XX X X XXXXXXXX XX XX XXXXXX XX X X X X X X X XXX XXXXXXXXXX XXXXX XX X
    Last edited by paranoidgnu; 05-04-2011 at 06:50 AM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Did you read my post?

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by mike65535 View Post
    Did you read my post?
    Yes, sorry.
    And the final version of my code is:
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    #define ROWS 25
    #define COLS 25
    void game_of_life(char o_arr[][COLS], char n_array[][COLS]);
    int checkx(char arr[][COLS],int row, int col);
    int main(void)
    {
    	char array[ROWS][COLS],n_array[ROWS][COLS];
    	srand(time(NULL));
    	int i, j, r_integer[ROWS][COLS], num_gen;
    	for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    			r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0;
    		}
    	}
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<2;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(array,n_array);
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				array[j][i]=n_array[j][i];
    			}
    		}
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				printf("%c", array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    void game_of_life(char o_arr[][COLS], char n_array[][COLS])
    {
    	int i, j, num_neighbors=0;
    	for(i=0;i<COLS;i++){     
    		for(j=0;j<ROWS;j++){
    			if ((o_arr[j][i])=='X'){
    				num_neighbors=checkx(o_arr, j, i);
    				if (num_neighbors >= 4)  
    					n_array[j][i]=' ';
    				else if( num_neighbors <4 && num_neighbors >= 2)
    					n_array[j][i]='X';
                }
    			else {
    				num_neighbors=checkx(o_arr, j, i);
                    if(num_neighbors == 3)   
    					n_array[j][i]='X';
                    else if ( num_neighbors < 2)
    					n_array[j][i]=' ';
                  }
              }
          }
    }
    		
    int checkx(char arr[][COLS],int row, int col)
    {
          int num_neighbors = 0;
          if ((arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((arr[row-1][col])=='X')
              num_neighbors++;
          if ((arr[row-1][col-1])=='X')
    		  num_neighbors++;
          if ((arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((arr[row+1][col-1])=='X')
              num_neighbors++;
          if ((arr[row+1][col])=='X')
              num_neighbors++;
          if ((arr[row+1][col+1])=='X')
              num_neighbors++;
          return(num_neighbors);
    }
    And its output is
    X XXXXXX XXX X XXXXXX X X XX X XXXXX X X X XXXXXX XX XX X XXXXXX X X XX XX X XXX X X XXXXXX X XX X X XXX XXXXX XX X XXXXXXXXX XX XX XXX X X X X XX X X XX XXXX X X X X X X X XX XX X XX X XX XXX X X XX XX X X X X X X XX X X XX XX XXX XXXXX XXXXXX XXX XXXXXXX X X X XX XX X X X X X XXXX XXXX XXX X X XX XXXXXXX X XX XX XX XXX X XXXXXXXX XX X X X XX X XXXXX XXX X XX X X X XX XX XX XX XX X XX XXXXX XXX X X X XXXXX X X X X X XXX XX XXXXX X X X XX XX XX X XX XXXXXX XX X X
    Generation: 1
    X �X XXXXX XXXX XM X � ��XXX X XX� D� � � XX �XX�XXXF XX� X XX XXXXX X� X XX Xg �� XX Ľ X �XXB�X X X| � ��X�XXX X X0 X/X�X ���XXX X|fX f X8q�XXX XXXX XX X X ��X� XXX X' i � X�� � X X �X X XXX X X X XD� XX2J D� �3� XXX� X�X �XXXXXXXJXXX5 XX XGX �� �� XXXNX�XX� X /XXXU�XXXX XXX � X� X/XX XXXX XX�f� � X XXX X޿X XX qXXXX XXX XXXXi XX X�X 8q Xg� � XXX X XX r �XX
    Generation: 2
    XX X XX XXXXX X X X �XXXX X XXX� XX XXX X X �XX�XXXXF XX� XXX XXXXXX X X X XX X XXXX � X X � X �XXXXX X X X| �X�X X X X 0X X/XXX ���XXX XX|fX XX8X�XXX XXXXXXXXXX � XX X X X X X X XX� X XXXX �X XXX X XXXX X XD�X X2 �XX� XXX� XXX �XXXXX XJXX 5 XXX XGX X�� � XX XNX�XXX� X /X U�XXXX XXXX � X�X XXX XX XX XXXXXf X X X X X޿XXXX qXXX XXXXXX X X XXXX�X 8X g� XXXXX X X X XXX

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    C'mon, guy. So many things wrong. You should test with a known pattern that you can verify by eye before going ahead with an entire program with zillions of errors.
    In main...
    Code:
    for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    This will go beyond the edge of the array.
    - You should print a "\n" for each row so that the output looks good.
    - inside game_of_life() you must use loops like this:
    Code:
    	for(i = 1; i < COLS - 1; i++){ 
    		for(j = 1; j < ROWS - 1; j++){
    ... because you can not use the edges due to the way checkx() works
    - the logic for number of neighbors and whether there's a birth, death, or no change needs to be corrected.
    - there is an error inside checkx for one of the cell checks.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by nonoob View Post
    C'mon, guy. So many things wrong. You should test with a known pattern that you can verify by eye before going ahead with an entire program with zillions of errors.
    In main...
    Code:
    for(i=0;i<ROWS+2;i++){
    		for(j=0;j<COLS+2;j++){
    This will go beyond the edge of the array.
    - You should print a "\n" for each row so that the output looks good.
    - inside game_of_life() you must use loops like this:
    Code:
    	for(i = 1; i < COLS - 1; i++){ 
    		for(j = 1; j < ROWS - 1; j++){
    ... because you can not use the edges due to the way checkx() works
    - the logic for number of neighbors and whether there's a birth, death, or no change needs to be corrected.
    - there is an error inside checkx for one of the cell checks.
    Hi, I'm new at programming,anyway...
    I changed the logic for number of neighbors:
    Code:
    for(i=1;i<COLS-1;i++){     
    		for(j=1;j<ROWS-1;j++){
    			if ((o_arr[j][i])=='X'){
    				num_neighbors = checkx(o_arr, j, i);
    				if( num_neighbors <4 && num_neighbors >= 2)
    					n_array[j][i]='X';
    				else
    					n_array[j][i]=' ';
                }
    			else {
    				num_neighbors=checkx(o_arr, j, i);
    				if (num_neighbors == 3)  
    					n_array[j][i]='X';                
    				else 
    					n_array[j][i]=' ';
                  }
              }
          }
    Is it true now?

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Yes that part of the logic is correct now.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by nonoob View Post
    Yes that part of the logic is correct now.
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    #define ROWS 25
    #define COLS 25
    void game_of_life(char o_arr[][COLS], char n_array[][COLS]);
    int checkx(char arr[][COLS],int row, int col);
    int main(void)
    {
    	char array[ROWS][COLS],n_array[ROWS][COLS];
    	srand(time(NULL));
    	int i, j, r_integer[ROWS][COLS], num_gen;
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0;
    		}
    	}
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<2;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(array,n_array);
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				array[j][i]=n_array[j][i];
    			}
    		}
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				printf("%c", n_array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    void game_of_life(char o_arr[][COLS], char n_array[][COLS])
    {
    	int i, j, num_neighbors=0;
    	for(i=0;i<COLS;i++){     
    		for(j=0;j<ROWS;j++){
    			if ((o_arr[j][i])=='X'){
    				num_neighbors = checkx(o_arr, j+1, i+1);
    				if( num_neighbors <4 && num_neighbors >= 2)
    					n_array[j][i]='X';
    				else if (num_neighbors >=4 || num_neighbors < 2)
    					n_array[j][i]=' ';
                }
    			else {
    				num_neighbors=checkx(o_arr, j+1, i+1);
    				if (num_neighbors == 3)  
    					n_array[j][i]='X';                
    				else 
    					n_array[j][i]=' ';
                  }
              }
          }
    }
    		
    int checkx(char arr[][COLS],int row, int col)
    {
          int num_neighbors = 0;
          if ((arr[row-1][col-1])=='X')
              num_neighbors++;
          if ((arr[row-1][col])=='X')
              num_neighbors++;
          if ((arr[row][col-1])=='X')
              num_neighbors++;
          if ((arr[row][col+1])=='X')
              num_neighbors++;
          if ((arr[row+1][col-1])=='X')
              num_neighbors++;
          if ((arr[row+1][col])=='X')
              num_neighbors++;
          if ((arr[row+1][col+1])=='X')
              num_neighbors++;
          return(num_neighbors);
    }
    I corrected some places
    And output :
    Code:
    mustafa@tosbag:~/Documents/My_C_Programs/Programming_Projects/CHAPTER8$ ./16
    X    X   X X XXXXXX X X X XX     XXXXX X X      X  X XXXX  XX    XX   X     XXX  X   XX  X  XX X  XX    X XXX   X X X X  X X  X X  XXXXXX X XX  XXX XX X        XX  XXX X  X  X XXX    XXX X X  XXXX X X X XX       X   X    XX XX X X XXXX    XX   X  XXX  XXX   XXXX  XXX   XX XXXXX XX      XXX XX XXXXX X X  XXXX X X   XXX  X  X X X  X   X X    XXX  X X   X X X X X    X X  XX   XXX XXX  XXXXX    XXXX  X X XX X  XXX  XX  X       XXX XX XXX X  X X X X     XX  XX  XX  X XX  XXX XX  XXXX X XXX X  XX  XXX   XXXXX XX X  X X    XXXXX  XX  X XX   XXXXXX XX    XXX  XXXX  XXXX XXXX  XX    XX XXX X  X    X    X  X  X X  XX X  XX XXXX
    Generation: 1
    X XXXX X  XX     XX X X    X X  X   XXXXX      XX         XXX     X X X    X      X   X     X  X        XX    XXX  X XX X XX X           X    X  X  XXX X     XX XX   XXXX X  XX  X  X  XX  XX X  X    X   XX X XX  XX X       XX X       X X           XX     XXX   XXX   X        X X    X  X XX        X X XX   X    X     XX XX XX   X X     X        XX    X X XXX       XXXX XX        XX     XX  XX  X   XX           X X  XX      X          XX X          X      X   X    XX    X XXX     X  XX X X  XX  XX X          X      X   X XX    XX   X         XX    X XXX        X  X   X  XX   XX X     X X   X X      X    X     X  X X XX 
    Generation: 2
    X  XXX  X XX   XXXX X X    X   XX X XXXXXX X   X         X  X    X X  X               X    XX  X X      X    X XX        X X X       XX X   XX XX   XXXXX   X  X  X     XX X  XX  XX XX  X X X        XX   X XXXX X XX X      XXX X     X  X X         XX           X XX   X       XXXX      XXXX     XXXX   X XXX X    X   X X   X  X X         X    X   XX    X XX X      XX   X X         X     X X XXX      XX         X   XXXX      XX         X X X  X       X      X XXX   X X  XXX  X         XX X     XX X XX  X              X X          X             XX    X  XX         X X  XX   XX  XXXX     X X     X   X      X X    X XX   X
    Last edited by paranoidgnu; 05-04-2011 at 08:43 AM.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    You need to first fix the output so it prints an array that looks good. And/or post in CODE tags

    Is it supposed to be a 25-by-25 array? Doesn't seem to be that way. I edited in Notepad and see only 18.5 rows. ;-)

    Code:
    X X X X XXXXXX X X X XX X
    XXXX X X X X XXXX XX XX X
     XXX X XX X XX X XX X XXX
     X X X X X X X X XXXXXX X
     XX XXX XX X XX XXX X X X
     XXX XXX X X XXXX X X X X
    X X X XX XX X X XXXX XX X
     XXX XXX XXXX XXX XX XXXX
    X XX XXX XX XXXXX X X XXX
    X X X XXX X X X X X X X X
    XX X X X X X X X X X XX X
    XX XXX XXXXX XXXX X X XX 
    X XXX XX X XXX XX XXX X X
     X X X XX XX XX X XX XXX 
    XX XXXX X XXX X XX XXX XX
    XXX XX X X X XXXXX XX X X
    X XXXXXX XX XXX XXXX XXXX
     XXXX XX XX XXX X X X X X
     X X XX X XX XXXX
    Last edited by mike65535; 05-04-2011 at 08:34 AM.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by mike65535 View Post
    You need to first fix the output so it prints an array that looks good. And/or post in CODE tags

    Is it supposed to be a 25-by-25 array? Doesn't seem to be that way. I edited in Notepad and see only 18.5 rows. ;-)

    Code:
    X X X X XXXXXX X X X XX X
    XXXX X X X X XXXX XX XX X
     XXX X XX X XX X XX X XXX
     X X X X X X X X XXXXXX X
     XX XXX XX X XX XXX X X X
     XXX XXX X X XXXX X X X X
    X X X XX XX X X XXXX XX X
     XXX XXX XXXX XXX XX XXXX
    X XX XXX XX XXXXX X X XXX
    X X X XXX X X X X X X X X
    XX X X X X X X X X X XX X
    XX XXX XXXXX XXXX X X XX 
    X XXX XX X XXX XX XXX X X
     X X X XX XX XX X XX XXX 
    XX XXXX X XXX X XX XXX XX
    XXX XX X X X XXXXX XX X X
    X XXXXXX XX XXX XXXX XXXX
     XXXX XX XX XXX X X X X X
     X X XX X XX XXXX
    I guess, you add a printf("\n"); like this;
    Code:
    int main(void)
    {
    	char array[ROWS][COLS],n_array[ROWS][COLS];
    	srand(time(NULL));
    	int i, j, r_integer[ROWS][COLS], num_gen;
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			r_integer[j][i] = (rand() % 100) > 50 ? 1 : 0;
    		}
    	}
    	for(i=0;i<ROWS;i++){
    		for(j=0;j<COLS;j++){
    			  if(r_integer[j][i] == 0)
                      array[j][i]=' ';
                  else
    				  array[j][i]='X';            
    			}
    		}			
    	for(i=0;i<ROWS;i++){
    		printf("\n");
    		for(j=0;j<COLS;j++){
    			printf("%c", array[j][i]);
    		}
    	}
    	for(num_gen=0;num_gen<4;num_gen++){
    		printf("\nGeneration: %d\n", num_gen+1);
    		game_of_life(array,n_array);
    		for(i=0;i<ROWS;i++){
    			for(j=0;j<COLS;j++){
    				array[j][i]=n_array[j][i];
    			}
    		}
    		for(i=0;i<ROWS;i++){
    			printf("\n");
    			for(j=0;j<COLS;j++){
    				printf("%c", n_array[j][i]);
    			}
    		}
    	}
    	return(0);
    }
    Yes, How can we create 25X25 output.
    Last edited by paranoidgnu; 05-04-2011 at 09:09 AM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  2. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 02:30 PM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  4. C++ Game of Life Program
    By rayrayj52 in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 03:58 PM
  5. Game of Life Program Help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2002, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21