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; } |
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):
Hello, I am trying to write this program and have received a "Segmentation Fault" error but...
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...
how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled). I also have a piece of algorithmn below..but am unsure of how I can use it in the code: //Algorithm...
This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...
Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...
I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() { ///main is complete, nothing to do here char board[ROWS][COLS]; bool playerToggle =...
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 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 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 <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...
I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std; bool DeclareResults(char ch, char gameboard[][3]); bool FilledBoard(char gameboard[][3]); void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() { //declare...