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

Part 1 Code: #include <iostream> #include <string> #include <cstdlib> #include <

ID: 3909447 • Letter: P

Question

Part 1 Code:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAX_ROW = 30;
const int MAX_COL = 60;


void displayMenu();
void setZeroArray(int arr[MAX_ROW][MAX_COL]);
void setInitialPatternArray(int arr[MAX_ROW][MAX_COL]);
void copyArray(int inp[MAX_ROW][MAX_COL], int loct[MAX_ROW][MAX_COL]);
void displayArray(int arr[MAX_ROW][MAX_COL]);

int main()
{
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
string choice;

choice = 'p';

srand(time(NULL));

while(choice == "P" || choice == "p")
{
displayMenu();
setZeroArray(tempArray);
setInitialPatternArray(tempArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);

cout << ">>";
cin >> choice;

system("cls");

if(!(choice == "P" || choice == "p" || choice == "Q" || choice == "q"))
{
cout<<"Invalid input"<<endl;

cout<<"[P]lay Press 'P' to play."<<endl;
cout<<"[Q]uit Press 'Q' to exit."<<endl;
cout<<">>";

cin>>choice;
}
if(choice == "Q" || choice == "q")
{
break;
}

}
}


void displayMenu()
{
cout << "[P]lay – Press 'P' to play." << endl;
cout << "[Q]uit – Press 'Q' to exit." << endl;
}

void setZeroArray(int arr[MAX_ROW][MAX_COL])
{
for(int iRow = 0; iRow < MAX_ROW; iRow++)
for(int jCol = 0; jCol < MAX_COL; jCol++)
arr[iRow][jCol] = 0;
}

void setInitialPatternArray(int arr[MAX_ROW][MAX_COL])
{
int row = rand() % (MAX_ROW - 6);
int col = rand() % (MAX_COL - 6);


for(int iRow = 0; iRow < 6; iRow++)
{
arr[row + iRow][col] = 1;
arr[row + iRow][col+6] = 1;
}


for(int iRow = 1; iRow <= 5; iRow++)
arr[row + 5][col + iRow] = 1;
}

void copyArray(int inp[MAX_ROW][MAX_COL], int loct[MAX_ROW][MAX_COL])
{
for(int iRow = 0; iRow < MAX_ROW; iRow++)
for(int jCol = 0; jCol < MAX_COL; jCol++)
loct[iRow][jCol] = inp[iRow][jCol];
}

void displayArray(int arr[MAX_ROW][MAX_COL])
{
for(int iRow = 0; iRow < MAX_ROW; iRow++)
{
for(int jCol = 0; jCol < MAX_COL; jCol++)
cout << arr[iRow][jCol];

cout << endl;
}
cout << endl;
}

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.

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 thecurrentArray 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.

Sample loop:

#include <iostream>
#include <conio.h> //kbhit()
#include <windows.h> //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;
}

***When declaring variables, array names, use a meaningful name; don't just use x, y, a, b, etc. Don't just use variables with single character.

***Declare all the variables inside of function(s) except constant variables.

***Declare all the arrays in the main() function.

***No object-oriented programming.

Explanation / Answer

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

const int MAX_ROW = 30;
const int MAX_COL = 60;
const int PATTERN_HEIGHT = 6;
const int PATTERN_WIDTH = 7;

void displayMenu();
void displayArray(const int array[MAX_ROW][MAX_COL]);
void copyArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]);
void setZeroArray(int array[MAX_ROW][MAX_COL]);
void setInitialPatternArray(int array[MAX_ROW][MAX_COL]);
void setNextGenArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]);

int main()
{
    char option;
    int currentArray[MAX_ROW][MAX_COL];
    int tempArray[MAX_ROW][MAX_COL];
  
    srand(time(NULL)); // Seed the number generator
  
    do
    {
        displayMenu();
        setZeroArray(tempArray);
        setInitialPatternArray(tempArray);
        copyArray(tempArray, currentArray);
        displayArray(currentArray);
        while(true)
        {
            system("cls");
            if(kbhit())
            {
                break;
            }
            else
            {
                setNextGenArray(currentArray, tempArray);
                copyArray(tempArray, currentArray);
                displayArray(currentArray);
                Sleep(1000);
            }
        }
        option = getch();
    }while(option != 'q');
  
    return 0;
}

void displayMenu()
{
    cout <<
        "[P]lay - Press 'P' to play. "
        "[Q]uit - Press 'Q' to exit. "
        " ";
}

void displayArray(const int array[MAX_ROW][MAX_COL])
{
    for(int coordX = 0; coordX < MAX_ROW; coordX++)
    {
        for(int coordY = 0; coordY < MAX_COL; coordY++)
        {
            cout << array[coordX][coordY];
        }
        cout << " ";
    }
}

void copyArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL])
{
    for(int coordX = 0; coordX < MAX_ROW; coordX++)
    {
        for(int coordY = 0; coordY < MAX_COL; coordY++)
        {
            toArray[coordX][coordY] = fromArray[coordX][coordY];
        }
    }
}

void setZeroArray(int array[MAX_ROW][MAX_COL])
{
    for(int coordX = 0; coordX < MAX_ROW; coordX++)
    {
        for(int coordY = 0; coordY < MAX_COL; coordY++)
        {
            array[coordX][coordY] = 0;
        }
    }
}

void setInitialPatternArray(int array[MAX_ROW][MAX_COL])
{
    int startRow = rand() % (MAX_ROW - PATTERN_HEIGHT);
    int startCol = rand() % (MAX_COL - PATTERN_WIDTH);
    // Set the two vertical lines
    for(int len = 0; len < PATTERN_HEIGHT; len++)
    {
        array[startRow + len][startCol] = 1;
        array[startRow + len][startCol + (PATTERN_WIDTH - 1)] = 1; // For the second side of the pattern
    }
    // Set the horizontal line at the bottom
    for(int len = 0; len < PATTERN_WIDTH; len++)
    {
        array[startRow + (PATTERN_HEIGHT - 1)][startCol + len] = 1;
    }
}

void setNextGenArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL])
{
    for(int coordX = 0; coordX < MAX_ROW; coordX++)
    {
        for(int coordY = 0; coordY < MAX_COL; coordY++)
        {
            int alive = 0;
            // Top-left corner
            if((coordX == 0) && (coordY == 0))
            {
                if(fromArray[0][1] == 1)
                    alive++;
                if(fromArray[1][1] == 1)
                    alive++;
                if(fromArray[1][0] == 1)
                    alive++;
            }
            // Top-right corner
            else if((coordX == 0) && (coordY == (MAX_COL - 1)))
            {
                if(fromArray[0][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[1][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[1][MAX_COL - 1] == 1)
                    alive++;
            }
            // Bottom-left corner
            else if((coordX == (MAX_ROW - 1)) && (coordY == 0))
            {
                if(fromArray[MAX_ROW - 1][1] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][1] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][0] == 1)
                    alive++;
            }
            // Bottom-right corner
            else if((coordX == (MAX_ROW - 1)) && (coordY == (MAX_COL - 1)))
            {
                if(fromArray[MAX_ROW - 1][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][MAX_COL - 1] == 1)
                    alive++;
            }
            // Top edge
            else if(coordX == 0)
            {
                if(fromArray[0][coordY - 1] == 1)
                    alive++;
                if(fromArray[1][coordY - 1] == 1)
                    alive++;
                if(fromArray[1][coordY] == 1)
                    alive++;
                if(fromArray[1][coordY + 1] == 1)
                    alive++;
                if(fromArray[0][coordY + 1] == 1)
                    alive++;
            }
            // Bottom edge
            else if(coordX == (MAX_ROW - 1))
            {
                if(fromArray[MAX_ROW - 1][coordY - 1] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][coordY - 1] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][coordY] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 2][coordY + 1] == 1)
                    alive++;
                if(fromArray[MAX_ROW - 1][coordY + 1] == 1)
                    alive++;
            }
            // Left edge
            else if(coordY == 0)
            {
                if(fromArray[coordX - 1][0] == 1)
                    alive++;
                if(fromArray[coordX - 1][1] == 1)
                    alive++;
                if(fromArray[coordX][1] == 1)
                    alive++;
                if(fromArray[coordX + 1][1] == 1)
                    alive++;
                if(fromArray[coordX + 1][0] == 1)
                    alive++;
            }
            // Right edge
            else if(coordY == (MAX_COL - 1))
            {
                if(fromArray[coordX - 1][MAX_COL - 1] == 1)
                    alive++;
                if(fromArray[coordX - 1][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[coordX][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[coordX + 1][MAX_COL - 2] == 1)
                    alive++;
                if(fromArray[coordX + 1][MAX_COL - 1] == 1)
                    alive++;
            }
            // Away from all edges
            else
            {
                if(fromArray[coordX - 1][coordY - 1] == 1)
                    alive++;
                if(fromArray[coordX - 1][coordY] == 1)
                    alive++;
                if(fromArray[coordX - 1][coordY + 1] == 1)
                    alive++;
                if(fromArray[coordX][coordY + 1] == 1)
                    alive++;
                if(fromArray[coordX + 1][coordY + 1] == 1)
                    alive++;
                if(fromArray[coordX + 1][coordY] == 1)
                    alive++;
                if(fromArray[coordX + 1][coordY - 1] == 1)
                    alive++;
                if(fromArray[coordX][coordY - 1] == 1)
                    alive++;
            }
            // Check rules
            if(fromArray[coordX][coordY] == 1)
            {
                if((alive == 2) || (alive == 3))
                {
                    toArray[coordX][coordY] = 1;
                }
                // Died from overpopulation/underpopulation
                else
                {
                    toArray[coordX][coordY] = 0;
                }
            }
            else
            {
                // Born from reproduction
                if(alive == 3)
                {
                    toArray[coordX][coordY] = 1;
                }
                else
                {
                    toArray[coordX][coordY] = 0;
                }
            }
        }
    }
}