// // main.cpp // GameOfLife // // PROGRAM CRASHES // NO OUPUT #include <iostrea
ID: 3726162 • Letter: #
Question
//
// main.cpp
// GameOfLife
//
// PROGRAM CRASHES
// NO OUPUT
#include <iostream>
#include <fstream>
using namespace std;
// Print the grid
void print(int cells[12][12], std::ostream &myfile)
{
myfile << " 0 1 2 3 4 5 6 7 8 9 10 11" << endl;
for(int i = 0; i < 12; i++)
{
myfile << i << " ";
for(int j; j < 12; j++)
{
myfile << cells[i][j] << "";
}
myfile << endl;
}
}
// Check if i and j are valid indices for grid
bool isValid(int i, int j)
{
if(i >= 0 && i < 12 && j >= 0 && j < 12)
return true;
else
return false;
}
// Find no of live neighbors
int findActiveNeighbors(int cells[12][12], int i, int j)
{
int activeNeighbors = 0;
if(isValid(i - 1, j - 1) && cells[i - 1][j - 1] == 1)
activeNeighbors++;
if(isValid(i - 1, j) && cells[i - 1][j] == 1)
activeNeighbors++;
if(isValid(i - 1, j + 1) && cells[i - 1][j + 1] == 1)
activeNeighbors++;
if(isValid(i, j - 1) && cells[i][j - 1] == 1)
activeNeighbors++;
if(isValid(i, j + 1) && cells[i][j + 1] == 1)
activeNeighbors++;
if(isValid(i + 1, j - 1) && cells[i + 1][j - 1] == 1)
activeNeighbors++;
if(isValid(i + 1, j) && cells[i + 1][j] == 1)
activeNeighbors++;
if(isValid(i + 1, j + 1) && cells[i + 1][j + 1] == 1)
activeNeighbors++;
return activeNeighbors;
}
// This method simulates a round; it changes the original array
void simulate(int cells[12][12])
{
int newCells[12][12];
// Not processing border cells
for(int i = 1; i < 11; i++)
{
for(int j = 1; j < 11; j++)
{
// Find no of live neighbors
int n = findActiveNeighbors(cells, i, j);
if(cells[i][j] == 1) // if cell is live
{
if(n == 0 || n == 1)
{
newCells[i][j] = 0; // mark for deletion
}
else if(n >= 4)
{
newCells[i][j] = 0; // mark for deletion
}
else
{
newCells[i][j] = cells[i][j];
}
}
else if (n == 3)
{
newCells[i][j] = 1; // mark for generation
}
else
{
newCells[i][j] = cells[i][j];
}
}
}
// Restoring values into original array
for(int i = 1; i < 11; i++)
{
for(int j = 1; j < 11; j++)
{
cells[i][j] = newCells[i][j];
}
}
}
int main()
{
ofstream myfile;
myfile.open ("Output.txt");
int life[12][12] =
{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
myfile << " Game of Life ------------" << endl;
int round = 1;
myfile << " Original configuration " << endl;
print(life, myfile);
while(round <= 4)
{
myfile << " Round " << round << endl;
simulate(life);
print(life, myfile);
round++;
}
return 0;
}
Explanation / Answer
Given below is the fixed code. Please do rate the answer if it was helpful. Thank you
//
// main.cpp
// GameOfLife
//
#include <iostream>
#include <fstream>
using namespace std;
// Print the grid
void print(int cells[12][12], std::ostream &myfile)
{
myfile << " 0 1 2 3 4 5 6 7 8 9 10 11" << endl;
for(int i = 0; i < 12; i++)
{
myfile << i << " ";
for(int j = 0; j < 12; j++)
{
myfile << cells[i][j] << "";
}
myfile << endl;
}
}
// Check if i and j are valid indices for grid
bool isValid(int i, int j)
{
if(i >= 0 && i < 12 && j >= 0 && j < 12)
return true;
else
return false;
}
// Find no of live neighbors
int findActiveNeighbors(int cells[12][12], int i, int j)
{
int activeNeighbors = 0;
for(int r = i - 1; r <= i + 1; r++)
for(int c = j - 1; c <= j+1; c++)
{
if(isValid(r, c) && !(r == i && c ==j )) //exclude the current cell i, j for which we calculate neighbours
{
if(cells[r][c] == 1)
activeNeighbors++;
}
}
return activeNeighbors;
}
// This method simulates a round; it changes the original array
void simulate(int cells[12][12])
{
int newCells[12][12] = {0};
// Not processing border cells
for(int i = 1; i < 11; i++)
{
for(int j = 1; j < 11; j++)
{
// Find no of live neighbors
int n = findActiveNeighbors(cells, i, j);
if(cells[i][j] == 1) // if cell is live
{
if(n < 2 || n > 3) //lesser than 2 or more than 3 neighbors
{
newCells[i][j] = 0; // mark for deletion
}
else
newCells[i][j] = 1;
}
else //dead cell
{
if(n == 3) // dead cell with 3 neighbors comes to life
newCells[i][j] = 1;
else
newCells[i][j] = 0;
}
}
}
// Restoring values into original array
for(int i = 1; i < 11; i++)
{
for(int j = 1; j < 11; j++)
{
cells[i][j] = newCells[i][j];
}
}
}
int main()
{
ofstream myfile;
myfile.open ("Output.txt");
if(myfile.fail())
{
cout << "Error opening file for output" << endl;
return 1;
}
int life[12][12] =
{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
myfile << " Game of Life ------------" << endl;
int round = 1;
myfile << " Original configuration " << endl;
print(life, myfile);
while(round <= 4)
{
myfile << " Round " << round << endl;
simulate(life);
print(life, myfile);
round++;
}
myfile.close();
cout << "Please check Output.txt" << endl;
return 0;
}
output
======
Game of Life
------------
Original configuration
0 1 2 3 4 5 6 7 8 9 10 11
0 000000000000
1 000010101000
2 000101010000
3 000000000000
4 000010101000
5 000101010000
6 011111111110
7 000011110000
8 000010101000
9 000101010000
10 000010101000
11 000000000000
Round 1
0 1 2 3 4 5 6 7 8 9 10 11
0 000000000000
1 000011110000
2 000011110000
3 000011110000
4 000011110000
5 000000000000
6 001000000100
7 001000000000
8 000100001000
9 000100001000
10 000011110000
11 000000000000
Round 2
0 1 2 3 4 5 6 7 8 9 10 11
0 000000000000
1 000010010000
2 000100001000
3 000100001000
4 000010010000
5 000001100000
6 000000000000
7 001100000000
8 001100000000
9 000101101000
10 000011110000
11 000000000000
Round 3
0 1 2 3 4 5 6 7 8 9 10 11
0 000000000000
1 000000000000
2 000110011000
3 000110011000
4 000011110000
5 000001100000
6 000000000000
7 001100000000
8 000000000000
9 001100000000
10 000010010000
11 000000000000
Round 4
0 1 2 3 4 5 6 7 8 9 10 11
0 000000000000
1 000000000000
2 000110011000
3 000000000000
4 000100001000
5 000010010000
6 000000000000
7 000000000000
8 000000000000
9 000100000000
10 000100000000
11 000000000000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.