Question

Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue on into the next generation. Please help.

#include <iostream>

# include <cstdlib>

using namespace std;

const char ALIVE = '*';

const char DEAD = ' ';

void initialization(bool **world, int nrows, int ncols); //prompts and reads the alive cells to initialize the world

void generation(bool **world, bool **copy, int nrows, int ncols);

void display(bool **world, int nrows, int ncols);

int evolve(bool **world, int nrows, int ncols, int row, int col); //returns the number of alive neighboring cells

int main() {

bool **world, **copy

int nrows, ncols;

char next;

cout << "Enter world dimensions (rows and columns): ";

cin >> nrows >> ncols;

world = new bool*[nrows];

copy = new bool*[nrows];

for(int i=0; i<nrows; i++)

{

world[i] = new bool[ncols];

copy[i] = new bool[ncols];

}

initialization(world, nrows, ncols);

display(world, nrows, ncols);

}

while(true){

cout <, "next Generation or Quit (g/q): ";

cin >> next;

if(next=='g' || next =='G' || next=='q' || next=='Q')

break;

}

while(next=='g' || next =='G') {

generation(world, copy, nrows, ncols);

display(world, nrows, ncols);

while(true) {

cout << "next Generation or Quit (g/q): ";

cin >> next;

if(next =='g' || next=='G' || next=='q' || next=='Q') break;

}

}

for(int i=0; i<ncols; i++)

{

delete world[i];

delete copy[i];

}

delete world;

delete copy;

return 0;

}

void generation(bool **world, bool **copy, int nrows, int ncols)

{

int alive_matches;

for(int i=0; i<nrows; i++)

{

for int j=0; j<ncols; j++)

copy[i][j] = world[i][j];

}

for(int i=0; i<nrows; i++)

{

for(int j=0; j<ncols; j++)

{

alive_matches = evolve(copy, nrows, ncols, i, j);

if(world[i][j])

{

if(alive_matches < 2 || alive_matches > 3)

//determines potential death by lonliness or over crowding

world[i][j] = false;

}

else

{

if(alive_matches == 3) //determines birth of new cell

world[i][j] = true;

}

}

}

}

void initialization(bool **world, int nrows, int ncols)

{

int alive, row, col;

for(int i=0; i<nrows; i++)

{

for int j=0; j<ncols; j++)

world[i][j] = false;

}

cout << "Enter the number of initial alive cells: ";

cin >> alive;

Cout << "Enter the coordinates of alive cells: " << endl;

for(int i=0; i<alive;)

{

cin >> row >> col;

if((row >= 0) && (row < nrows) && (col >= 0) && (col < ncols) && (!world[row][col))

{

world[row][col] = true;

i++;

}

}

}

void display(bool **world, int nrows, int ncols)

{

for(int i=0; i<nrows; i++)

{

cout << endl; << "|";

for(int j=0; j<ncols; j++)

{

if(world[i][j])

cout << ALIVE << "|";

else

cout << DEAD << "|";

}

cout << endl; << string(ncols*5, '=');

}

cout << endl;

}

int evolve(bool **world, int nrows, int ncols, int row, int col)

{

int alive_neighbours = 0;

if(((row-1) >= 0) && world[row-1][col])

alive_neighbours++;

if(((row-1)>=0) && ((col+1) < ncols) && world[row-1][col+1])

alive_neighbours++;

if(((col-1)>=0) && world[row][col-1])

alive_neighbours++;

if(((col+1)<ncols) && world[row][col+1])

alive_neighbours++;

if(((row+1)<nrows) && ((col-1) >= 0) && world[row+1][col-1])

alive_neighbours++;

return alive_neighbours;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I tested the code lot of inputs with rows and columns different. It gives the perfect results. The only problem is, counting the neighbours. You consider only one diagonal neighbour. Take care of alive co-ordinates. You might exceed the memory boundaries while giving the row,col values.

EG:

nrows=4

ncols=2

row=4

col=1

gives segmentation error.

//input:

5
4
6
1
2
1
3
1
1
2
3
2
2
2
0
g
g
g
g
g
q

Enter world dimensions (rows and columns): Enter the number of initial alive cells: Enter the coordinates of alive cells:

| | | | |
====================
| |*|*|*|
====================
|*| |*|*|
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):
| | | | |
====================
| |*|*|*|
====================
| | |*|*|
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):
| | | | |
====================
| | |*|*|
====================
| |*|*|*|
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):
| | | | |
====================
| | |*|*|
====================
| |*| |*|
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):
| | | | |
====================
| | |*| |
====================
| | | | |
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):
| | | | |
====================
| | | | |
====================
| | | | |
====================
| | | | |
====================
| | | | |
====================
next Generation or Quit (g/q):

Add a comment
Know the answer?
Add Answer to:
Hello, I am trying to write this program and have received a "Segmentation Fault" error but...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Hello, I am working on a project for my C++ class, I have the vast majority...

    Hello, I am working on a project for my C++ class, I have the vast majority of the code figured out but am running into a few issues. Mainly updating each * to an X. The problem is as follows:(Airplane Seating Assignment) Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using...

    Need help with my ksmall program. I get an error saying segmentation dump. Thanks #include<iostream> using namespace std; int ksmall(int*, int, int , int); void swap(int*, int*); int main() {     int SIZE = 10;     int target;     int begining=0;     int ending=SIZE-1;     int *array1= new int[SIZE];     cout << "Enter 10 integers: " << endl;     for (int i=0; i<SIZE; i++)     {        cin>>array1[i];     }     cout << " What is the Kth smallest number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT