#include <stdio.h> #include <iostream> #include <string> #include <fstream> usin
ID: 3773559 • Letter: #
Question
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int ROWS = 22;
const int COLS = 80;
const int BOARD_ROWS(20);
const int BOARD_COLS(78);
const char LIVE = 'X'; //life cells
const char DEAD = '.'; //dead cells
char NewBoard[ROWS][COLS];
char quit;
bool MakeArray(string filename, char board[][COLS]);
void GameBoard(char board[][COLS]);
void SwitchBoard (char board[][COLS]);
void NextState(char board[][COLS]);
int main()
{
char board [ROWS][COLS];
string filename;
cout<<"Enter the filename: ";
cin>>filename;
if (MakeArray(filename, board))
{
cout << "File not found. ";
cin>>quit;
return 1;
}
cout<<endl;
for (int l = 0; l<20; l++)
{
NextState(board);
GameBoard(board);
SwitchBoard(board);
}
char q;
cin >> q;
return 0;
}
bool MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile) return true;
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
return false;
}
void GameBoard (char board[][COLS])
{
for (int r=1; r<=ROWS-2; r++)
{
for (int c=1; c<=COLS-2; c++)
{
cout<<board[r][c];
}
cout<<endl;
}
cout<<endl;
}
void NextState (char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
int LiveCnt=0;
if (board[r-1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r][c+1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r+1][c+1]==LIVE)
{
LiveCnt++;
}
NewBoard[r][c] = DEAD;
if (board[r][c] == LIVE && LiveCnt < 2)
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==LIVE && (LiveCnt==2
{
NewBoard[r][c]=LIVE;
}
else if (board[r][c]==LIVE && LiveCnt>3 )
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==DEAD &&
{
NewBoard[r][c]=LIVE;
}
}
}
}
void SwitchBoard(char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
board[r][c]=NewBoard[r][c];
}
}
}
Hello, I keep getting the following errors: In function 'void NextState(char (*)[80])': 218:1: error: expected ')' before '{' token 224:1: error: expected ')' before 'else' 240:1: error: expected primary-expression before '}' token
g and shas outputs a bar graph like the one in that display except that your nd th will output the bars vertically rather than horizontally. A two-di array may be useful. 12. Write a program that accepts input like the program in Displayz e mensonal Though not a "game in any traditional sense, it providestLife vior that is specified with only a few rules. This project asks you tbeha. program that allows you to specify an initial configuration.Th tite lows the rules of LIFE to show the continuing behavior of the coni 13. The mathematician John Horton Conway invented the Game asks you to write LIFE is an organism that lives in a discrete, two-dimensional w this world is actually unlimited, we don't have that luxury, so orld. w so we restri the array to so characters wide by 22 character positions high. Ifw restico access to a larger screen, by all means use it. we This world is an array with each cell capable of holding one Generations mark the passing of time. Each generation brings births and to the LIFE community. The births and deaths follow the following set of nales We define each cell to have eight neighbor cells. The neighbors of a are the cells directly above, below, to the right, to the left, diagonal above to the right and left and diagonally below to the right and le If an occupied cell has zero or one neighbors, it dies of loneliness. If a occupied cell has more than three neighbors, it dies of ou diagonal ercrouding n If an empty cell has exactly three occupied neighbor cells, there is a bim of a new cell to replace the empty cell Births and deaths eration. A cell dying for whatever reason may help cause birh newbom cell cannot prevent the death of another, say, by reducing the local population are instantaneous and occur at the changes of g hu resurrect a cell that is dying, nor will a cell's de Nats Some growe from relatively small starting config ions Others move across the region. It is recommended that foness auExplanation / Answer
You forgot/missed closing some of the conditional statements properly, and therefore, its expecting that closing braces. Here is the modified working code for you. If you need any refinements, just get back to me.
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int ROWS = 22;
const int COLS = 80;
const int BOARD_ROWS(20);
const int BOARD_COLS(78);
const char LIVE = 'X'; //life cells
const char DEAD = '.'; //dead cells
char NewBoard[ROWS][COLS];
char quit;
bool MakeArray(string filename, char board[][COLS]);
void GameBoard(char board[][COLS]);
void SwitchBoard (char board[][COLS]);
void NextState(char board[][COLS]);
int main()
{
char board [ROWS][COLS];
string filename;
cout<<"Enter the filename: ";
cin>>filename;
if (MakeArray(filename, board))
{
cout << "File not found. ";
cin>>quit;
return 1;
}
cout<<endl;
for (int l = 0; l<20; l++)
{
NextState(board);
GameBoard(board);
SwitchBoard(board);
}
char q;
cin >> q;
return 0;
}
bool MakeArray(string filename, char board[][COLS])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile) return true;
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
return false;
}
void GameBoard (char board[][COLS])
{
for (int r=1; r<=ROWS-2; r++)
{
for (int c=1; c<=COLS-2; c++)
{
cout<<board[r][c];
}
cout<<endl;
}
cout<<endl;
}
void NextState (char board[][COLS])
{
for(int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
int LiveCnt=0;
if (board[r-1][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r-1][c]==LIVE)
{
LiveCnt++;
}
if(board[r-1][c+1]==LIVE)
{
LiveCnt++;
}
if(board[r][c-1]==LIVE)
{
LiveCnt++;
}
if (board[r][c+1]==LIVE)
{
LiveCnt++;
}
if(board[r+1][c-1]==LIVE)
{
LiveCnt++;
}
if(board[r+1][c+1]==LIVE)
{
LiveCnt++;
}
NewBoard[r][c] = DEAD;
if (board[r][c] == LIVE && LiveCnt < 2)
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==LIVE && LiveCnt==2)
{
NewBoard[r][c]=LIVE;
}
else if (board[r][c]==LIVE && LiveCnt>3 )
{
NewBoard[r][c]=DEAD;
}
else if (board[r][c]==DEAD && LiveCnt==3)
{
NewBoard[r][c]=LIVE;
}
}
}
}
void SwitchBoard(char board[][COLS])
{
for (int r=0; r<ROWS; r++)
{
for (int c=0; c<COLS; c++)
{
board[r][c]=NewBoard[r][c];
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.