Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi im resubmitting a program I did last year I cant figure out whats the error. ///Main.ccp #include #include #include boolmatrix.h #include using namespace std;

Hi im resubmitting a program I did last year I cant figure out whats the error.

///Main.ccp

#include

#include

#include "boolmatrix.h"

#include

using namespace std;

static const int NUM_ROWS = 20;

static const int NUM_COLS = 20;

void firstgen(boolMatrix& generation);

void getNextGen(boolMatrix& generation, int liveNeighbors, int row, int col);

void fateDeadCell(boolMatrix& generation, int liveNeighbors, int row, int col);

void fateLiveCell(boolMatrix& generation, int liveNeighbors, int row, int col);

void checkOutBounds(int row, int col, int& liveNeighbors);

void printResults(boolMatrix& generation);

int tallyLiveNeighbors(const boolMatrix& generation, int& liveNeighbors, int row, int col);

int main(int numGen,int liveNeighbors,int row,int col)

{

boolMatrix generation;

cout << "How many generations in total?: ";

cin >> numGen;

firstgen(generation);

generation.print();

cout << endl << "Total alive in row 10 = " << generation.rowCount(10) << endl;

cout << "Total alive in col 10 = " << generation.colCount(10) << endl;

cout << "Total alive = " << generation.totalCount() << endl << endl;

for (int count = 1; count < numGen; count++)

{

getNextGen(generation, liveNeighbors, row, col);

printResults(generation);

}

system("pause");

return 0;

}

void firstgen(boolMatrix& gen)

{

ifstream infile("input.txt");

assert(infile);

int row, col;

infile >> row >> col;

while (infile) {

gen.set(row, col, true);

infile >> row >> col;

}

infile.close();

}

void getNextGen(boolMatrix& gen, int liveNeigh, int row, int col)

{

liveNeigh = 0;

for (int row = 0; row < NUM_ROWS; row++)

{

for (int col = 0; col < NUM_COLS; col++)

{

if (gen.get(row, col) == false) {

fateDeadCell(gen, liveNeigh, row, col);

}

else if (gen.get(row, col) == true) {

fateLiveCell(gen, liveNeigh, row, col);

}

}

}

}

int tallyLiveNeighbors(boolMatrix& gen, int& liveNeigh, int row, int col)

{

if (gen.get(row - 1, col - 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row - 1, col) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row - 1, col + 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row, col - 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row, col + 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row + 1, col - 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row + 1, col) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

else if (gen.get(row + 1, col + 1) == true) {

liveNeigh++;

checkOutBounds(row, col, liveNeigh);

}

return liveNeigh;

}

void fateDeadCell(boolMatrix& gen, int liveNeigh, int row, int col)

{

tallyLiveNeighbors(gen, liveNeigh, row, col);

if (liveNeigh == 3) {

gen.set(row, col, true);

}

else if ((liveNeigh < 3) || (liveNeigh > 3)) {

gen.set(row, col, false);

}

}

void fateLiveCell(boolMatrix& gen, int liveNeigh, int row, int col)

{

tallyLiveNeighbors(gen, liveNeigh, row, col);

if ((liveNeigh <= 1) || (liveNeigh >= 4)) {

gen.set(row, col, false);

}

else if ((liveNeigh == 2) || (liveNeigh == 3)) {

gen.set(row, col, true);

}

}

void checkOutBounds(int row, int col, int& liveNeigh)

{

if (((row - 1) < 0) || ((row + 1) > NUM_ROWS) || ((col - 1)< 0) || ((col + 1) < NUM_COLS))

{

liveNeigh--;

}

}

void printResults(boolMatrix& gen)

{

gen.print();

cout << "Total alive in row 10 = " << gen.rowCount(10) << endl;

cout << "Total alive in col 10 = " << gen.colCount(10) << endl;

cout << "Total alive = " << gen.totalCount() << endl << endl;

}

///boolmatrix.ccp

#include #include "boolmatrix.h" #include

int boolMatrix::totalCount(){

int total = 0;

for (int row = 0; row < NUM_ROWS; row++) {

for (int col = 0; col < NUM_COLS; col++) {

if (matrix1[row][col] == true) {

total++;

}

}

}

return total;

}

using namespace std;

int boolMatrix::neighborCount(int row, int col)const {

int total = 0;

int initialrow = row - 1;

if (initialrow <= 0)

initialrow = 0;

int lastrow = row + 1;

if (lastrow <= 0)

lastrow = 0;

int initialcol = 0;

if (initialcol <= 0)

initialcol = 0;

int lastcol = col + 1;

if (lastcol <= 0)

lastcol = 0;

for (int i = initialrow; i <= lastrow; i++) {

for (int j = initialcol; j <= lastcol; j++) {

if (i == row && j == col)

continue;

if (get(row, col) == true)

total++;

}

}

return total;

}

int boolMatrix::rowCount(int row)const{

assert(row >= 0 && row < NUM_ROWS);

int count = 0;

for (int col = 0;col < NUM_COLS;col++) {

if (matrix1[row][col]== true)

count++;

}

return count;

}

int boolMatrix::colCount(int col)const {

assert(col >= 0 && col < NUM_COLS);

int count = 0;

for (int row = 0;row < NUM_ROWS;row++) {

if (matrix1[row][col]== true)

count++;

}

return count;

}

bool boolMatrix::get(int row,int col)const{

assert(row >= 0 && row < NUM_ROWS);

assert(col >= 0 && row < NUM_COLS);

return matrix1[row][col];

}

void boolMatrix::set(int row,int col,bool set ){

assert(row >= 0 && row < NUM_ROWS);

assert(col >= 0 && row < NUM_COLS);

matrix1[row][col] = set;

}

boolMatrix::boolMatrix() {

for (int row = 0; row < NUM_ROWS; row++) {

for (int col = 0; col < NUM_COLS; col++) {

matrix1[row][col] = false;

}

}

}

void boolMatrix::print() {

for (int row = 0; row < NUM_ROWS; row++){

cout << endl;

for (int col = 0; col < NUM_COLS; col++) {

if (matrix1[row][col])

cout << "*";

else cout << " ";

}

}

}

///boolmatrix.h

class boolMatrix {

public:

boolMatrix();

int totalCount();

int neighborCount(int row, int col)const;

int colCount(int col)const;

int rowCount(int row)const;

bool get(int row, int col)const;

void set(int row,int col,bool set );

void print();

static const int NUM_ROWS = 20;

static const int NUM_COLS = 20;

private:

bool matrix1[NUM_ROWS][NUM_COLS];

};

//////////////Output I get

How many generations in total ? : 5

* * * * * * * * * * *** * * * ** * * * * * * ** * *** ** ** ** ** * * * * * * * * * ** ** * * * *** * ** * * * *** * ** * ** ***** * * * * * * * * * ** * * * ** * * * * ** * Total alive in row 10 = 3 Total alive in col 10 = 4 Total alive = 100

////Correct output

 01234567890123456789 0 1 * * 2 * * *** * 3 * **** * 4 * * * ** 5 * * ***** * 6 ** * * *** * 7 * *** * * 8 **** 9 * ****** 10* * * 11 * * ** * 12** **** *** 13 * *** ** * 14 * * * *** 15 ** *** ** ** 16 * * ** 17 18 19 Total alive in row 10 = 3 Total alive in col 10 = 1 Total alive = 95

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ai And The Lottery Defying Odds With Intelligent Prediction

Authors: Gary Covella Ph D

1st Edition

B0CND1ZB98, 979-8223302568

More Books

Students also viewed these Databases questions

Question

Complexity of linear search is O ( n ) . Your answer: True False

Answered: 1 week ago