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

write a program to implement the simulation of life described in scientific amer

ID: 651332 • Letter: W

Question

write a program to implement the simulation of life described in scientific american, using 2 dimensional array of size 30 rows by 30 columns.
1. initial set of cells are marked "alive" by the user. this is generation 0. program will ask the user to input a set of row and column values to let user determine which cells are "alive". user will be prompted to enter row and column coordinates of live cells unit a-1 is entered. once a-1 is entered (user has no other alive cells to set) generation 0 will be displayed.
2. cells change for each succeeding generation by following rules: a) a living cell dies of overcrowding in the next generation if it currently has 4 or more living neighbors. b) a living cell dies of loneliness in the next generarion if it currently has only 0 or 1 living neighbors. c) empty cell becomes a "birth" cell (become alive) in the next geration if it has exactly 3 living neighbors. d) all other cells remain unchanged.
3. the new generation becomes the current generation and is displayed
4. after displauing each new generation, ask the user if they wish to continue to the next generation or stop at this point.

Explanation / Answer

// LifeSimulation.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdio.h"

#include "conio.h"

#include "iostream"

#include "string"

using namespace std;

char arr[30][30];

int gen;

void drawcells()

{

cout << " Generation : " << gen;

for(int i=0;i<30;i++)

{

cout << endl;

for(int j=0;j<30;j++)

cout << "|" << arr[i][j] << "|";

}

}

void nextgeneration()

{

int count=0;

gen++;

for(int i=0;i<30;i++)

{

for(int j=0;j<30;j++)

{

if (i>0)

{

if (arr[i-1][j] == 'A')

count++;

if (j>0)

if (arr[i-1][j-1] == 'A')

count++;

if (j<29)

if (arr[i-1][j+1] == 'A')

count++;

}

if (i<29)

{

if (arr[i+1][j] == 'A')

count++;

if (j>0)

if (arr[i+1][j-1] == 'A')

count++;

if (j<29)

if (arr[i+1][j+1] == 'A')

count++;

}

if (j>0)

if (arr[i][j-1] == 'A')

count++;

if (j<29)

if (arr[i][j+1] == 'A')

count++;

if (arr[i][j] == 'A')

{

if (count >= 4)

arr[i][j] = ' ';

if (count <=1)

arr[i][j] = ' ';

}

if (arr[i][j] == ' ')

{

if (count == 3)

arr[i][j]='A';

}

}

}

drawcells();

char ans;

cout << "Do you wish to move to the next generation : ";

cin >> ans;

if(ans=='Y')

nextgeneration();

}

void main()

{

int row,col,pos;

char ans;

gen=0;

string rowcol;

cout << "Please enter a set of row and column.(eg 3,4) : ";

getline(cin,rowcol);

while (rowcol != "a-1")

{

pos = rowcol.find(",");

row = stoi(rowcol.substr(0,pos));

col = stoi(rowcol.substr(pos+1));

arr[row][col] = 'A';

cout << "Please enter a set of row and column.(eg 3,4) : ";

getline(cin,rowcol);

}

drawcells();

cout << "Do you wish to move to the next generation : ";

cin >> ans;

if(ans=='Y')

nextgeneration();

}

?

// LifeSimulation.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stdio.h"

#include "conio.h"

#include "iostream"

#include "string"

using namespace std;

char arr[30][30];

int gen;

void drawcells()

{

cout << " Generation : " << gen;

for(int i=0;i<30;i++)

{

cout << endl;

for(int j=0;j<30;j++)

cout << "|" << arr[i][j] << "|";

}

}

void nextgeneration()

{

int count=0;

gen++;

for(int i=0;i<30;i++)

{

for(int j=0;j<30;j++)

{

if (i>0)

{

if (arr[i-1][j] == 'A')

count++;

if (j>0)

if (arr[i-1][j-1] == 'A')

count++;

if (j<29)

if (arr[i-1][j+1] == 'A')

count++;

}

if (i<29)

{

if (arr[i+1][j] == 'A')

count++;

if (j>0)

if (arr[i+1][j-1] == 'A')

count++;

if (j<29)

if (arr[i+1][j+1] == 'A')

count++;

}

if (j>0)

if (arr[i][j-1] == 'A')

count++;

if (j<29)

if (arr[i][j+1] == 'A')

count++;

if (arr[i][j] == 'A')

{

if (count >= 4)

arr[i][j] = ' ';

if (count <=1)

arr[i][j] = ' ';

}

if (arr[i][j] == ' ')

{

if (count == 3)

arr[i][j]='A';

}

}

}

drawcells();

char ans;

cout << "Do you wish to move to the next generation : ";

cin >> ans;

if(ans=='Y')

nextgeneration();

}

void main()

{

int row,col,pos;

char ans;

gen=0;

string rowcol;

cout << "Please enter a set of row and column.(eg 3,4) : ";

getline(cin,rowcol);

while (rowcol != "a-1")

{

pos = rowcol.find(",");

row = stoi(rowcol.substr(0,pos));

col = stoi(rowcol.substr(pos+1));

arr[row][col] = 'A';

cout << "Please enter a set of row and column.(eg 3,4) : ";

getline(cin,rowcol);

}

drawcells();

cout << "Do you wish to move to the next generation : ";

cin >> ans;

if(ans=='Y')

nextgeneration();

}

?