Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

I am having trouble with this program. This program should be implementing the classic Conway's Game of Life. I keep receiving a segment fault error

I am having trouble with this program. This program should be implementing the classic Conway's Game of Life. I keep receiving a "segment fault" error and cannot find the culprit for this error. Any help is appreciated. Thank you in advance. Also, this must compile in Linux. Thank you.

#include #include #include #include

#include "life.h"

using namespace std;

//GLOBAL VARIABLES

int ROWS = 0, //stores the number of rows in the grid COLUMNS = 0; //stores the number of rows in the grid

int first_row = 0, first_column = 0, last_row = 0, last_column = 0, **ngrid;

char **grid, **tempgrid;

//This function reads input file for subsequent prosessing (add high level //description of your implementation logic) void populateWorld (const char * file) { int start_size = 1, count = 0;

char *column_count, *temp;

fstream fin("glider_gun_fight.txt", ios::in);

if (!fin) { cout << endl << endl << "***Program Terminated.***" << endl << endl << "Input file failed to open." << endl; }

column_count = new char [start_size];

while( isalnum(fin.peek()) ) { fin >> column_count[count]; ++count;

temp = new char [count + 1];

for ( int i = 0; i < count; ++i ) temp[i] = column_count[i];

delete [] column_count;

column_count = temp; }

COLUMNS = count;

grid = new char *[start_size];

for ( int i = 0; i < start_size; ++i ) grid[i] = new char [COLUMNS];

for ( int i = 0; i < start_size; ++i ) for ( int j = 0; j < COLUMNS; ++i ) grid[i][j] = column_count[j]; while( !fin.eof() ) { if( fin.eof() ) break;

++ROWS;

tempgrid = new char *[ROWS + 1];

for ( int i = 0; i < ( ROWS + 1 ); ++i ) tempgrid[i] = new char[COLUMNS];

for ( int i = 0; i < ROWS; ++i ) for ( int j = 0; j < COLUMNS; ++j ) tempgrid[i][j] = grid[i][j];

for ( int i = ROWS; i < ( ROWS + 1 ); ++i ) for ( int j = 0; j < COLUMNS; ++j ) fin >> tempgrid[i][j];

for ( int i = 0; i < ROWS; ++i ) delete [] grid[i];

delete [] grid; delete [] temp;

grid = tempgrid; }

fin.close(); }

//This function outputs the grid for current generation (add high level //description of your implementation logic) void showWorld () { changeGrid();

for ( int i = 0; i < ROWS; ++i ) { for ( int j = 0; j < COLUMNS; ++j ) cout << grid[i][j];

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

//This function creats new geneneration grid from the old generation grid //(add high level description of your implementation logic) void iterateGeneration () { getNeighborCount(); changeGrid();

for ( int i = 0; i < ROWS; ++i ) { for ( int j = 0; j < COLUMNS; ++j) { if ( grid[i][j] == '1' && ngrid[i][j] < 2 ) grid[i][j] = '0'; else if ( grid[i][j] == '1' && ngrid[i][j] > 3 ) grid[i][j] = '0'; else if ( grid[i][j] == '1' && ( ngrid[i][j] == 2 || ngrid[i][j] == 3 )) grid[i][j] = '1'; else if ( grid[i][j] == '0' && ngrid[i][j] == 3 ) grid[i][j] = '1'; } } for ( int i = 0; i < ROWS; ++i) delete [] ngrid[i];

delete [] ngrid; ngrid = 0; } /**************************************************************************************** changeGrid(): This function calls findParameters() to get data from the grid that is used and changes the grid to the correct size. ****************************************************************************************/ void changeGrid() { int temp_row = 0, temp_column = 0;

findParameters();

temp_row = ( last_row - first_row + 1 ); temp_column = ( last_column - first_column + 1 );

tempgrid = new char *[temp_row];

for ( int i = 0; i < temp_row; ++i ) tempgrid[i] = new char [temp_column];

for ( int i = 0; i < temp_row; ++i ) for ( int j = 0; j < temp_column; ++j) tempgrid[i][j] = '0';

for ( int i = 0; i < temp_row; ++i ) for ( int j = 0; j < temp_column; ++j) tempgrid[i][j] = grid[( first_row - 1 ) + i][( first_column - 1 ) + j];

for ( int i = 0; i < ROWS; ++i ) delete [] grid[i];

delete [] grid;

grid = tempgrid;

ROWS = temp_row; COLUMNS = temp_column;

addLayer(); }

/**************************************************************************************** addLayer() ****************************************************************************************/ void addLayer() { ROWS += 2; COLUMNS +=2;

tempgrid = new char *[ROWS]; for ( int i = 0; i < ROWS; ++i ) tempgrid[i] = new char[COLUMNS];

for ( int i = 0; i < ROWS; ++i ) for ( int j = 0; j < COLUMNS; ++j ) tempgrid[i][j] = '0';

for ( int i = 0; i < ( ROWS - 2 ); ++i ) for ( int j= 0; j < (COLUMNS - 2); ++j ) tempgrid[i+1][j+1] = grid [i][j];

for ( int i = 0; i < (ROWS - 2); ++i ) delete [] grid[i];

delete [] grid; grid = tempgrid; }

/**************************************************************************************** findParameters ****************************************************************************************/ void findParameters() { for ( int i = 0; i < ROWS; ++i ) { for ( int j = 0; j < COLUMNS; ++j ) { if ( grid[i][j] == '1' ) { first_row = i + 1; break; } } if ( first_row != 0 ) break; } for ( int i = 0; i < ROWS; ++i ) { for ( int j = 0; j < COLUMNS; ++j ) { if ( grid[i][j] == '1' ) { last_row = i + 1; break; } } } for ( int j = 0; j < COLUMNS; ++j ) { for ( int i = 0; i < ROWS; ++i ) { if ( grid[i][j] == '1' ) { first_column = ( j + 1 ); break; } } if ( first_column != 0 ) break; } for ( int j = 0; j < COLUMNS; ++j ) { for ( int i = 0; i < ROWS; ++i ) { if ( grid[i][j] == '1' ) { last_column = j + 1; break; } } } }

/**************************************************************************************** getNeighborCount() ****************************************************************************************/ void getNeighborCount() { int ncount = 0, temp_row = ROWS, temp_col = COLUMNS;

ngrid = new int *[ROWS];

for ( int i = 0; i < ROWS; ++i ) ngrid[i] = new int [COLUMNS];

for ( int i = 0; i < ROWS; ++i ) for ( int j = 0; j < COLUMNS; ++j ) ngrid[i][j] = 0;

addLayer();

for ( int i = 1; i < temp_row; ++i ) { for ( int j = 1; j < temp_col; ++j ) { if ( grid[i-1][j-1] == '1' ) ncount++; if ( grid[i-1][j] == '1' ) ncount++; if ( grid[i-1][j+1] == '1' ) ncount++; if ( grid[i][j-1] == '1' ) ncount++; if ( grid[i][j+1] == '1' ) ncount++; if ( grid[i+1][j-1] == '1' ) ncount++; if ( grid[i+1][j] == '1' ) ncount++; if ( grid[i+1][j+1] == '1') ncount++;

ngrid[(i-1)][(j-1)] = ncount; ncount = 0; } } }

life.h

//This header file provides the prototypes of the function definitions //for the project.

#ifndef life_h #define life_h

#include #include #include

using namespace std;

void populateWorld(const char * file); void showWorld(); void iterateGeneration(); void changeGrid(); void addLayer(); void findParameters(); void getNeighborCount();

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions