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

The Game of Life : PROGRAM IN C++ Task: Simulate the growth of a biological popu

ID: 3602579 • Letter: T

Question

The Game of Life : PROGRAM IN C++

Task: Simulate the growth of a biological population. The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. Our implementation of the game will use a 2-dimensional array with 12 rows and 12 columns. Each cell of the array will hold either a 1 (an organism is present) or a 0 (no organism there). You will calculate 4 generations of the organisms’ birth, survival, and death.

An organism’s neighbors affect its survival. Each organism has 8 adjoining cells where its neighbors may live, as shown in this grid:

The neighbors for cell X would be located in the 8 shaded cells surrounding it. In the situation illustrated, X has 3 neighbors.

Rules for the game: * Birth: An organism will be born in each empty location that has exactly 3 neighbors.

* Death: An organism with 4 or more organisms as neighbors will die from overcrowding.

An organism with 1 or 0 neighbors will die of loneliness.

* Survival: An organism with 2 or 3 neighbors will survive to the next generation.

You will not have to process the border cells (i.e., rows 0 and 11, and columns 0 and 11) of the game, but the border cells’ contents will affect the internal cells. Assume that border cells are infertile regions where organisms can neither survive nor be born.

The population configuration as shown below will be the initial contents of one array. For each generation, calculate the results of the first array’s births, deaths, and survivals, and store those results in a second array. After printing the results, copy the second array’s data back into the first one (replacing the original data), and repeat the process to calculate the next generation.

Input: The first array will be initialized as follows:

int life[12][12] =

{ {0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,1,0,1,0,1,0,0,0},

{0,0,0,1,0,1,0,1,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,1,0,1,0,1,0,0,0},

{0,0,0,1,0,1,0,1,0,0,0,0},

{0,1,1,1,1,1,1,1,1,1,1,0},

{0,0,0,0,1,1,1,1,0,0,0,0},

{0,0,0,0,1,0,1,0,1,0,0,0},

{0,0,0,1,0,1,0,1,0,0,0,0},

{0,0,0,0,1,0,1,0,1,0,0,0},

{0,0,0,0,0,0,0,0,0,0,0,0} };

Output:

You will print out the initial situation and the results of each of 4 generations of growth. Send all the print output to a single external file. Provide header information (including name and date) in your printed version, and include row numbers and column numbers. Line up the columns. The initial generation would look something like this:

Game of Life

---------------

Original Configuration:

0 1 2 3 4 5 6 7 8 9 10 11

0 0 0 0 0 0 0 0 0 0 0 0 0

1 0 0 0 0 1 0 1 0 1 0 0 0

2 0 0 0 1 0 1 0 1 0 0 0 0

3 0 0 0 0 0 0 0 0 0 0 0 0

4 0 0 0 0 1 0 1 0 1 0 0 0

5 0 0 0 1 0 1 0 1 0 0 0 0

6 0 1 1 1 1 1 1 1 1 1 1 0

7 0 0 0 0 1 1 1 1 0 0 0 0

8 0 0 0 0 1 0 1 0 1 0 0 0

9 0 0 0 1 0 1 0 1 0 0 0 0

10 0 0 0 0 1 0 1 0 1 0 0 0

11 0 0 0 0 0 0 0 0 0 0 0 0

0 0 1 1 X 0 0 1 0

Explanation / Answer

Given below is the c++ code for the question. The output is generated into a file. open the output file and check. In case of any issues, post a comment, I will help. Please don't forget to rate the answer if it helped. Thank you.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX_ROW = 12;
const int MAX_COL = 12;

int count_neighbours(int gen[12][12], int row, int col);
int is_in_range(int row, int col);
void next_generation(int gen[12][12], int next[12][12]);
void print(ofstream &out, string heading, int gen[12][12]);
void copy(int dst[12][12], int src[12][12]);
int main()
{

int life[12][12] =
{ {0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0} };
  
string filename;
cout << "Enter output filename: ";
cin >> filename;
  
ofstream out(filename.c_str());
if(!out.is_open())
{
cout << "Could not open output file " << filename << endl;
return 1;
}
  
  
print(out, "Initial configuration:", life);
  
for(int i = 1; i <= 4; i ++)
{
  
int next[12][12];
next_generation(life, next);
out << "________________________________" << endl << endl;
  
print(out, "Generation " + to_string(i) , next);
copy(life, next);//for next iteration, update the starting generations
}
out.close();
cout << "output written to output file " << filename << endl;
  
}

void copy(int dst[12][12], int src[12][12])
{
for(int i = 0; i < 12; i ++)
for(int j = 0; j < 12; j++)
dst[i][j] = src[i][j];
}
int count_neighbours(int gen[12][12], int row, int col)
{
int count = 0;
for(int r = row - 1; r <= row + 1; r++)
{
for(int c = col - 1; c <= col + 1; c++)
{
if(r == row && c == col) continue; //skip the current cell, don't count it
if(is_in_range(r, c) && gen[r][c] == 1)
count++;
}
}
return count;
}
//check if the row and col values are with in range i.e 0 - MAX_ROW and 0 - MAX_COL
int is_in_range(int row, int col)
{
return (row >=0 && row < MAX_COL && col >= 0 && col <= MAX_COL);
}
void next_generation(int gen[12][12], int next[12][12])
{
//set 0 for the top and bottom rows
for(int i = 0; i < MAX_COL; i++)
{
next[0][i] = 0;
next[MAX_ROW- 1][i] = 0;
}
//set 0 for the left and right most columns
for(int i = 0; i < MAX_ROW; i++)
{
next[i][0] = 0;
next[i][MAX_COL- 1] = 0;
}
  
for(int i = 1; i < MAX_ROW - 1; i++)
{
for(int j = 1; j < MAX_COL - 1; j++)
{
int count = count_neighbours(gen, i, j);
if(gen[i][j] == 1) //living
{
if(count < 2 || count > 3)
next[i][j] = 1;
else
next[i][j] = 0;
}
else
{
if(count == 3) //non living with exact 3 neighbors
next[i][j] = 1;
else
next[i][j] = 0;
}
  
}
}
  
}

void print(ofstream &out, string heading, int gen[12][12])
{
  
out << heading << endl;
out << "-------------------" << endl;
out << setw(3) << " ";
for(int i = 0; i < MAX_COL; i++)
out << setw(3) << i;
  
for(int i = 0; i < MAX_ROW; i++)
{
out << endl;
out << setw(2) << i << " ";
for(int j = 0; j < MAX_COL; j++)
out << setw(3) << gen[i][j];
}
  
out << endl;
}

output file

Initial configuration:
-------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 0 1 0 0 0
2 0 0 0 1 0 1 0 1 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 1 0 1 0 1 0 0 0
5 0 0 0 1 0 1 0 1 0 0 0 0
6 0 1 1 1 1 1 1 1 1 1 1 0
7 0 0 0 0 1 1 1 1 0 0 0 0
8 0 0 0 0 1 0 1 0 1 0 0 0
9 0 0 0 1 0 1 0 1 0 0 0 0
10 0 0 0 0 1 0 1 0 1 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0
________________________________

Generation 1
-------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 1 1 0 0 0
2 0 0 0 1 1 0 1 0 0 0 0 0
3 0 0 0 0 1 1 1 1 0 0 0 0
4 0 0 0 0 0 1 0 1 1 0 0 0
5 0 0 0 1 0 1 0 1 0 0 0 0
6 0 1 0 1 1 1 1 1 1 0 1 0
7 0 0 1 0 1 1 1 1 0 0 0 0
8 0 0 0 1 1 0 1 0 0 0 0 0
9 0 0 0 0 0 1 0 1 1 0 0 0
10 0 0 0 0 0 1 0 1 1 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0
________________________________

Generation 2
-------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 0 1 0 0 0
2 0 0 0 0 1 0 1 0 1 0 0 0
3 0 0 0 1 1 1 1 1 1 0 0 0
4 0 0 0 0 0 1 0 1 0 0 0 0
5 0 0 1 0 0 1 0 1 0 1 0 0
6 0 1 0 1 1 1 1 1 0 0 1 0
7 0 0 0 0 1 1 1 1 1 0 0 0
8 0 0 0 0 1 0 1 0 1 0 0 0
9 0 0 0 0 0 0 0 1 0 0 0 0
10 0 0 0 0 0 1 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0
________________________________

Generation 3
-------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 0 1 0 0 0
2 0 0 0 0 1 0 1 0 0 1 0 0
3 0 0 0 0 1 1 1 1 0 0 0 0
4 0 0 0 1 0 1 0 1 0 0 0 0
5 0 0 0 1 0 1 0 0 0 1 0 0
6 0 1 1 0 1 1 1 1 0 1 1 0
7 0 0 0 0 1 1 1 1 0 1 0 0
8 0 0 0 0 0 0 1 0 0 0 0 0
9 0 0 0 0 0 1 1 0 0 0 0 0
10 0 0 0 0 0 1 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0
________________________________

Generation 4
-------------------
0 1 2 3 4 5 6 7 8 9 10 11
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 1 0 1 1 1 0 0 0
2 0 0 0 1 0 0 1 0 1 1 0 0
3 0 0 0 1 1 1 1 0 1 0 0 0
4 0 0 0 0 0 1 0 0 1 0 0 0
5 0 0 0 0 0 1 0 1 0 0 1 0
6 0 1 0 0 1 1 1 0 0 0 0 0
7 0 0 0 1 0 1 1 1 0 0 1 0
8 0 0 0 0 1 0 1 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 1 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 0 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote