c++ program(part 2 problem) (part 2 problem) 1. Use the global variables properl
ID: 3866699 • Letter: C
Question
c++ program(part 2 problem)
(part 2 problem)
1. Use the global variables properly:
const int MAX_ROW = 30;
const int MAX_COL = 60;
for example:
int currentArray[MAX_ROW][MAX_COL];
instead of int currentArray[30][60];
****In your program, replace '60' with MAX_COL and replace '30' with MAX_ROW.
****When declaring variables, array names, use a meaningful name; don't just use x, y, a, b, etc. Don't just use a single character. (-30 points)
2. Write the definition of the function setNextGenArray that creates a pattern of next generation (tempArray) based on the current generation (currentArray); modify the tempArray based on the currentArray by applying the rules of Game of Life.
Conway's Game of Life Rules:
- The neighbors of a given cell are the cells that touch it vertically, horizontally, or diagonally.
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
3. Create a loop to generate and next pattern automatically.
When executing your program, the following should happen:
a. Print the menu using the displayMenu function.
b. Initialize the tempArray using the setZeroArray function.
c. Set the ‘U’ pattern in the tempArray using the setInitialPatternArray function.
d. Copy the tempArray to the currentArray using the copyArray function.
e. Print the currentArray using the displayArray function.
f. When the user presses ‘P’, (loop the following three steps)
i. Generate a next pattern using the setNextGenArray function
ii. Copy the tempArray to the currentArray using the copyArray function.
iii. Print the currentArray using the displayArray function.
g. When the user presses ‘Q’, it will terminate the program.
if useful (loop):
#include
#include //kbhit()
#include //Sleep()
using namespace std;
int main()
{
char ans;
bool t;
do
{
int i = 0;
while (1)
{
if (t = kbhit())
{
break;
}
else
{
system("cls");
cout << "Hello" << endl;
cout << i;
i++;
Sleep(1000);
}
}
ans = getch();
}while (ans != 'q');
return 0;
}
OUTPUTS:
**************************************************************************************
aftewr pressing [P]lay.
[P]lay Press 'p' to play. [Q]uit = Press 'q' to quit. 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000011111110000000000000000000000000000000000Explanation / Answer
As per I understand Your Question , you want the answer for the part two of this problem.
So here it is.
Note:- I have used printf for printing out of instinct , so i you want just change it with cout , and
everything works perfectly in both cases.
#include <iostream>
using namespace std;
/* I am testing and demonstrating the code for a smaller example but it works in the same
way for all sizes as long as they fit in datatype. */
const int MAX_ROW = 3;
const int MAX_COL = 3;
int currentArray[MAX_ROW][MAX_COL] = {0};
// Initializing the array with all zeroe's.
/* Let say that the current conddition has elements at i == j
so it currentArray will look something like this. */
int countLives(int a,int b)
{
int m = 0,n=0,count = 0;
int i , j,count_i,count_j,value = 0;
if (a-1 < 0)
{
i = 0;
count_i = a + 1;
}
else if(a+1 > MAX_ROW)
{
count_i = MAX_ROW;
i = a-1;
}
else
{
i = a-1;
count_i = a + 1;
}
if (b-1 < 0)
{
j = 0;
count_j = b + 1;
}
else if(b+1 > MAX_COL)
{
count_j = MAX_COL;
j = b-1;
}
else
{
j = b-1;
count_j = b + 1;
}
// printf("%d %d ",i,j);
// printf("%d %d ",count_i,count_j );
for(m=i;m<=count_i;m++)
{
for(n=j;n<=count_j;n++)
{
// printf("%d",currentArray[m][n]);
if(currentArray[m][n] == 1){
if(m == a && n == b)
count = 1;
else
value++;
}
}
// printf(" ");
}
return value;
}
void setNextGenArray()
{
int tempArray[MAX_ROW][MAX_COL] = {0};
int lives;
for(int i = 0;i<MAX_ROW;i++)
{
for(int j =0 ; j<MAX_COL;j++)
{
lives = countLives(i,j);
if (lives == 2)
{
tempArray[i][j] = 1;
}
}
}
// Printing the temporary array
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
printf("%d",tempArray[i][j]);
}
printf(" ");
}
}
int main()
{
int i , j;
for(i=0;i<MAX_ROW;i++)
{
for(j=0;j<MAX_COL;j++)
{
if(i == j)
currentArray[i][j] = 1;
}
}
setNextGenArray();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.