Question & Answer: I am having a problem. Please, help me. I want the output to loop infinitely until I type 'q'……

I am having a problem. Please, help me.

I want the output to loop infinitely until I type ‘q’.

So, When I press ‘p’, the output should loop infitely, but the output should pause for one second, and When I press ‘q’, the program should just disappear.

This is the code that I have done, and I do not know how to loop it.

#include <iostream>
#include <stdlib.h>
using namespace std;

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

void displayMenu()
{
cout<<“[P]lay Press ‘P’ to play. [Q]uit Press ‘Q’ to exit.”<<endl;
}
void setZeroArray (int (&tempArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
tempArray[i][j] = 0;
}
}
}
void copyArray(int tempArray[MAX_ROW][MAX_COL], int (&currentArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
currentArray[i][j] = tempArray[i][j];
}
}
}
void displayArray (int arr[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void setInitialPatternArray(int (&tempArray)[MAX_ROW][MAX_COL])
{
int temprow = rand() % 10 + 1;
int tempcol = rand() % 10 + 1;

for(int i=temprow;i<temprow+6;i++)
{
tempArray[i][tempcol] = 1;
tempArray[i][tempcol+6] = 1;
}
for(int i=tempcol+1;i<tempcol+7;i++)
{
tempArray[temprow+5][i] = 1;
}
}

int countLiveNeighbour(int (&currentArray)[MAX_ROW][MAX_COL],int i,int j)
{
int count=0;
if(i!=0)
{
if(currentArray[i-1][j]==1)
count++;
}

if(j!=0)
{
if(currentArray[i][j-1]==1)
count++;
}

if(i!=MAX_ROW-1)
{
if(currentArray[i+1][j]==1)
count++;

}

if(j!=MAX_COL-1)
{
if(currentArray[i][j+1]==1)
count++;

}

if(i!=0&&j!=0)
{
if(currentArray[i-1][j-1]==1)
count++;
}

if(i!=0&&j!=MAX_COL-1)
{
if(currentArray[i-1][j+1]==1)
count++;
}

if(i!=MAX_ROW-1&&j!=0)
{
if(currentArray[i+1][j-1]==1)
count++;
}

if(i!=MAX_ROW-1&&j!=MAX_COL-1)
{
if(currentArray[i+1][j+1])
count++;
}

return count;

}
void setNextGenArray(int (&tempArray)[MAX_ROW][MAX_COL],int (&currentArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
if(currentArray[i][j]==1)
{
if(countLiveNeighbour(currentArray,i,j)<2||countLiveNeighbour(currentArray,i,j)>3)
{
tempArray[i][j]=0;
}
else
{
tempArray[i][j]=1;
}

}
else
{
if(countLiveNeighbour(currentArray,i,j)==3)
{
tempArray[i][j]=1;
}
else
{
tempArray[i][j]=0;
}

}
}
}
}
int main()
{
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
string choice;
displayMenu();
setZeroArray(currentArray);
setZeroArray(tempArray);
setInitialPatternArray(tempArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);

while(true)
{
cin>>choice;
if(choice == “Q” || choice == “q”)
{
break;
}
if (choice == “P” || choice == “p”)
{
system (“cls”);
setNextGenArray(tempArray,currentArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);
}
}
return 0;
}

Expert Answer

 I made changes in Code , Have a look..It will wait for every 3 seconds for next output

// Example program
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
const int MAX_COL = 60;
const int MAX_ROW = 30;
void displayMenu()
{
cout<<“[P]lay Press ‘P’ to play. [Q]uit Press ‘Q’ to exit.”<<endl;
}
void setZeroArray (int (&tempArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
tempArray[i][j] = 0;
}
}
}
void copyArray(int tempArray[MAX_ROW][MAX_COL], int (&currentArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
currentArray[i][j] = tempArray[i][j];
}
}
}
void displayArray (int arr[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void setInitialPatternArray(int (&tempArray)[MAX_ROW][MAX_COL])
{
int temprow = rand() % 10 + 1;
int tempcol = rand() % 10 + 1;
for(int i=temprow;i<temprow+6;i++)
{
tempArray[i][tempcol] = 1;
tempArray[i][tempcol+6] = 1;
}
for(int i=tempcol+1;i<tempcol+7;i++)
{
tempArray[temprow+5][i] = 1;
}
}
int countLiveNeighbour(int (&currentArray)[MAX_ROW][MAX_COL],int i,int j)
{
int count=0;
if(i!=0)
{
if(currentArray[i-1][j]==1)
count++;
}
if(j!=0)
{
if(currentArray[i][j-1]==1)
count++;
}
if(i!=MAX_ROW-1)
{
if(currentArray[i+1][j]==1)
count++;
}
if(j!=MAX_COL-1)
{
if(currentArray[i][j+1]==1)
count++;
}
if(i!=0&&j!=0)
{
if(currentArray[i-1][j-1]==1)
count++;
}
if(i!=0&&j!=MAX_COL-1)
{
if(currentArray[i-1][j+1]==1)
count++;
}
if(i!=MAX_ROW-1&&j!=0)
{
if(currentArray[i+1][j-1]==1)
count++;
}
if(i!=MAX_ROW-1&&j!=MAX_COL-1)
{
if(currentArray[i+1][j+1])
count++;
}
return count;
}
void setNextGenArray(int (&tempArray)[MAX_ROW][MAX_COL],int (&currentArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
if(currentArray[i][j]==1)
{
if(countLiveNeighbour(currentArray,i,j)<2||countLiveNeighbour(currentArray,i,j)>3)
{
tempArray[i][j]=0;
}
else
{
tempArray[i][j]=1;
}
}
else
{
if(countLiveNeighbour(currentArray,i,j)==3)
{
tempArray[i][j]=1;
}
else
{
tempArray[i][j]=0;
}
}
}
}
}
int main()
{
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
string choice;
displayMenu();
setZeroArray(currentArray);
setZeroArray(tempArray);
setInitialPatternArray(tempArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);
cin>>choice;
if (choice == “P” || choice == “p”)
{
while(true){
system (“cls”);
setNextGenArray(tempArray,currentArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);
sleep(3);

}

}
return 0;
}

================================================================
See Output

Still stressed from student homework?
Get quality assistance from academic writers!