The program should generate and display the next generation when you press retur
ID: 3634694 • Letter: T
Question
The program should generate and display the next generation when you press return.define a void function named generation that takes the array we call world,an 80-column by 22-row array of char ,which contains the initial configuration.the function scans the array and modifies the cells marking the cells with births and deaths in accord with the rules listed earlier(in the textbook )
There should be a function display that accepts the array world and displays the array on the screen.some sort of delay is appropriate between calls to generation and display.
The problems in on page 436 of the C++ eighth edition by Walter Savitch.
Explanation / Answer
please rate - thanks
get back to me if any problems
#include <iostream>
#include <ctime>
using namespace std;
void generation(char[][80]);
void display(char[][80]);
void initialize(char[][80]);
int check(int,int,char[][80]);
int main()
{srand(time(0));
char world[22][80];
initialize(world);
while(true)
{display(world);
getchar();
generation(world);
}
return 0;
}
int check(int i,int j,char world[][80])
{if(world[i][j]==' ')
return 0;
return 1;
}
void generation(char world[][80])
{int i,j,count;
for(i=0;i<22;i++)
for(j=0;j<80;j++)
{count=0;
if(i!=0)
{if(j!=0)
count+=check(i-1,j-1,world);
count+=check(i-1,j,world);
if(j!=79)
count+=check(i-1,j+1,world);
}
if(j!=0)
count+=check(i,j-1,world);
if(j!=79)
count+=check(i-1,j+1,world);
if(i!=21)
{count+=check(i+1,j-1,world);
count+=check(i+1,j,world);
count+=check(i+1,j+1,world);
}
if(world[i][j]=='*'&&(count<2||count>3))
world[i][j]=' ';
if(world[i][j]==' '&&count==3)
world[i][j]='*';
}
}
void display(char world[][80])
{int i,j;
for(i=0;i<22;i++)
{for(j=0;j<80;j++)
cout<<world[i][j];
cout<<endl;
}
}
void initialize(char world[][80])
{int i,j;
char val[]={' ','*'};
for(i=0;i<22;i++)
for(j=0;j<80;j++)
world[i][j]=val[rand()%2];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.