I have been working this program out with a very basic description of what it should do for days... Once I thought I figured out the right way to do it, and the compiler showed no errors, I tried to run it and it gave me some garbled symbols I didn't understand, and then after working on it for a bit longer, all it said was segmentation fault and ended. Can somebody please tell me what I'm doing wrong? Here are the instructions for the assignment followed by my code:

John H. Conway invented a game called Life to model the process of birth, survival and death. The idea is that organisms require others in order to survive and procreate, but that overcrowding results in death.

To implement Life simulation, we can use a two-dimensional array, with dimensions 20 x 20:
char Life [20][20];
Do NOT declare the array as global; declare it inside main and pass it to functions.

Each cell in the array holds either ‘*’ or is blank. The star represents the presence of an organism.

The simulation begins with an initial generation. You are to ask a user for a number of organisms they would like to create in the first generation, and position them randomly.
You also will ask the user for a number of generations to simulate.

Three rules govern the transition from one generation to the next:
1. Birth rule: An organism is born into an empty cell that has exactly three living neighbors.
2. Survival rule: An organism with either two or three living neighbors survives from one generation to the next.
3. Death rule: An organism with four or more neighbors dies from overcrowding. An organism with fewer than two neighbors dies from loneliness.

A neighbor of a cell is any cell that touches that cell. Thus, any cell not along the edges has exactly 8 neighbors.

Print each generation to the screen, including the initial generation.

PLEASE HELP!!!

#include <stdio.h>
#include <stdlib.h>

   void header(void);
   void survivalRule(char [][20], int, int);
   void birthRule(char [][20], int, int);
   void deathRule(char [][20], int, int);
       
    int main(void)
   {
      char Life[20][20];
      int orgs, gens;
      int i, a, b, row, col;
      int count = 0;
      int x = 19;
      int y = 19;
      char ans;
       
      header();
  
      do{
         printf("\nPlease enter the number of organisms you would like to live in the first generation: ");
         scanf("%i", &orgs);
      
         srand( (unsigned)time( NULL ) );
              
         for(i = 0; i<orgs; i++)
         {
            row = rand();
            row %= 20;
            col = rand();
            col %= 20;
            Life[row][col] == '*';
         }
        
         for(row = 0; row<20; row++)
         {
            for(col = 0; col<20; col++)
            {
               if(Life[row][col] != '*')
                   Life[row][col] == ' ';
            }
         }
              
         printf("\nNow enter the number of generations you would like to simulate: ");
         scanf("%i", &gens);
              
         for(row = 0; row<20; row++)
         {
              for(col = 0; col<20; col++)
              {
                  printf("%s", Life[row][col]);
              }
              puts(" ");
          }
            
         do{                
            birthRule(Life, x, y);
            survivalRule(Life, x, y);
            deathRule(Life, x, y);
                for(row = 0; row<20; row++)
             {
                  for(col = 0; col<20; col++)
                  {
                      printf("%s", Life[row][col]);
                  }
                  puts(" ");
             }
            count++;
         }while(count<gens);
          
         printf("Would you like to simulate another generation? y or n:\n");
          
         scanf("%c", &ans);
              
      }while( ans != 'n' && ans != 'N');
       
      return 0;
   }
            
                
    void header(void) /*function for program header*/
   {
      printf("\n\t..Welcome to the Game of Life..\n");
      printf("Follow the instructions to start your own simulation of life!\n\n");
   }
    
    void survivalRule(char Life[][20], int x, int y)
   {
      int row, col;
      int neighbors = 0;
      for(row = 1; row<19; row++)
      {
         for(col = 1; col<19; col++)
         {
            if(Life[row][col] == '*')
            {
               if(Life[row - 1][col - 1] == '*')
                  ++neighbors;
               if(Life[row - 1][col] == '*')
                  ++neighbors;
               if(Life[row - 1][col + 1] == '*')
                  ++neighbors;
               if(Life[row][col - 1] == '*')
                  ++neighbors;
               if(Life[row][col + 1] == '*')
                  ++neighbors;
               if(Life[row + 1][col - 1] == '*')
                  ++neighbors;
               if(Life[row + 1][col] == '*')
                  ++neighbors;
               if(Life[row + 1][col + 1] == '*')
                  ++neighbors;
               if(neighbors == 2 || neighbors == 3)
               {
                  Life[row][col] == '*';
               }
            }
         }
      }
      return;
   }
    
    void birthRule(char Life[][20], int x, int y)
   {
      int row, col;
      int neighbors = 0;
      for(row = 1; row<19; row++)
      {
         for(col = 1; col<19; col++)
         {
            if(&Life[row][col]== " ")
            {
               if(Life[row - 1][col - 1] == '*')
                  neighbors++;
               if(Life[row - 1][col] == '*')
                  neighbors++;
               if(Life[row - 1][col + 1] == '*')
                  neighbors++;
               if(Life[row][col - 1] == '*')
                  neighbors++;
               if(Life[row][col + 1] == '*')
                  neighbors++;
               if(Life[row + 1][col - 1] == '*')
                  neighbors++;
               if(Life[row + 1][col] == '*')
                  neighbors++;
               if(Life[row + 1][col + 1] == '*')
                  neighbors++;
               if(neighbors == 3)
               {
                   Life[row][col] == '*';
               }
            }
         }
      }
  
      return;
   }
    
    void deathRule(char Life[][20], int x, int y)
   {
      int row, col;
      int neighbors = 0;
      for(row = 1; row<19; row++)
      {
         for(col = 1; col<19; col++)
         {
            if(Life[row][col] == '*')
            {
               if(Life[row - 1][col - 1] == '*')
                  neighbors++;
               if(Life[row - 1][col] == '*')
                  neighbors++;
               if(Life[row - 1][col + 1] == '*')
                  neighbors++;
               if(Life[row][col - 1] == '*')
                  neighbors++;
               if(Life[row][col + 1] == '*')
                  neighbors++;
               if(Life[row + 1][col - 1] == '*')
                  neighbors++;
               if(Life[row + 1][col] == '*')
                  neighbors++;
               if(Life[row + 1][col + 1] == '*')
                  neighbors++;
               if(neighbors < 2 || neighbors > 4)
               {
                  Life[row][col] == ' ';
               }
            }
         }
      }
      return;
   }

Recommended Answers

All 3 Replies

On lines 33, 41, 122, etc., you have put two "==" (a test for equality), instead of just one equal sign, (for assignment).

Turn on your warnings on your compiler, and you'll be told all this and more.

Variables x and y are never used in survivalRule(), birthRule(), etc.

Life[][] is an array of char's, so don't print strings. Add a sleep(1) or more, between the display of generations, so you can see what's going on.

What it REALLY needs is a board display that doesn't scroll on the screen. You can do that using the header file <conio.h> with gotoxy(), in Turbo C, or use the Windows API and use Gotoxy(), along with <windows.h> header.

thank you for your help!!

hey could u post our finished program again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.