Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

/* please add notes thank you */ John H. Conway (Scientific American, October 19

ID: 3842756 • Letter: #

Question

/* please add notes thank you */ John H. Conway (Scientific American, October 1970, p. 120) invented a game called Life to model the process of birth, survival, and death. The idea is that organisms require others to survive and procreate but that overcrowding results in death. Use a two dimensional char array whose dimensions are 10 times 10. Each cell in the array holds either a * or a blank. The star represents the presence of an organism the blank its absence. The game starts with an initial generation, which consists of a mix of stars and blanks. There are three rules to the game: 1. Birth Rule: An organism is born into an empty cell that has exactly three 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" is a cell that touch that cell. A cell that does not lie along any edge has exactly eight neighbors. For example, in the below, A has 8 X neighbors. Because of the rules, it is relatively easy for an organism along the edge to die and relatively hard for either an edge organism to survive or a new organism to be born into an edge. Write a program that reads the initial generation into the array life from a text file and then produces N new generations, N>20. Print each generation.

Explanation / Answer

The code is as follows:

//Assuming the initial generation data is taken from file input.txt

#include<stdio.h>

char life[10][10];

void check_death(i,j){
     /*-------------------Corner locations-------------*/

     if (i == 0 && j==0){
        count = 0;
        if (life[i][j+1] == '*')
           count++;
        if (life[i+1][j+1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (count < 2)
           life[i][j] = ' '; //It will die
     }
     if (i == 0 && j==9){
        count = 0;
        if (life[i][j-1] == '*')
           count++;
        if (life[i+1][j-1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (count < 2)
           life[i][j] = ' '; //It will die
     }
     if (i == 9 && j==9){
        count = 0;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j-1] == '*')
           count++;
        if (life[i][j-1] == '*')
           count++;
        if (count < 2)
           life[i][j] = ' '; //It will die
     }
     if (i == 9 && j==0){
        count = 0;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j+1] == '*')
           count++;
        if (life[i][j+1] == '*')
           count++;
        if (count < 2)
           life[i][j] = ' '; //It will die
     }

     /*----------------------boundary rows-------------------------------*/
     if (i == 0 && j > 0 && j < 9){
        count = 0;
        if (life[i][j+1] == '*')
           count++;
        if (life[i+1][j+1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (life[i-1][j-1] == '*')
           count++;
        if (life[i][j-1] == '*')
           count++;
        if (count < 2 or count >= 4)
           life[i][j] = ' '; //It will die
     }
     if (i > 0 && i < 9 && j==9){
        count = 0;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j-1] == '*')
           count++;
        if (life[i][j-1] == '*')
           count++;
        if (life[i+1][j-1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (count < 2 || count >= 4)
           life[i][j] = ' '; //It will die
     }
     if (i == 9 && j > 0 && j < 9){
        count = 0;
        if (life[i][j+1] == '*')
           count++;
        if (life[i-1][j+1] == '*')
           count++;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j-1] == '*')
           count++;
        if (life[i][j-1] == '*')
           count++;
        if (count < 2 || count >= 4)
           life[i][j] = ' '; //It will die
     }
     if (i > 0 && i < 9 && j == 0){
        count = 0;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j+1] == '*')
           count++;
        if (life[i][j+1] == '*')
           count++;
        if (life[i+1][j+1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (count < 2 || count >= 4)
           life[i][j] = ' '; //It will die
     }
     /*-------------------------Internal cell-----------------------------*/
    if (i > 0 && i < 9 && j > 0 && j < 9){
        count = 0;
        if (life[i-1][j] == '*')
           count++;
        if (life[i-1][j+1] == '*')
           count++;
        if (life[i][j+1] == '*')
           count++;
        if (life[i+1][j+1] == '*')
           count++;
        if (life[i+1][j] == '*')
           count++;
        if (life[i+1][j-1] == '*')
           count++;
        if (life[i][j-1] == '*')
           count++;
        if (life[i-1][j-1] == '*')
           count++;
        if (count < 2 || count >= 4)
           life[i][j] = ' '; //It will die
     }
}
void main(){

   int numofgen;
   char ch;

   FILE *fp;
  
   fp = fopen("Input.txt", "r");
   if (fp == NULL){
      printf("Error in opening the file ");
      return;
   }
   i = 0;
   j = 0;
   while (fscanf(fp,"%c", &ch) != EOF){
       life[i][j] = ch;
       j++;
       if (j == 10){
           i++;
           j = 0;
       }
       if (i == 10)
          break;  
   }
   numofgen = 0;
   while (numofgen < 22){
      for (int i = 0; i<10; i++){
         for (int j =0; j<10; j++){
             if (life[i][j] == '*')
                check_death(i,j);
         }
      }
      for (int i = 0; i<10; i++){
         for (int j =0; j<10; j++){
             printf("%c %c", life[i][j], ' ');
         }
         printf(" ");
      }
      numofgen++;
   }
  
}