Hello everyone. I have an assignment in a C++ class and I feel like I am very close but I keep erring out on a segmentation fault. I am not sure where the error is but any help would be appreciated.
#include<iostream>
#include<vector>
#include<unistd.h> // linux / Mac OS X
// #include<windows.h> // Windows
#include<cstdlib>
usingnamespace std;
// You can change the size of your world matrix in your entire code
// just by changing the values of ROWS and COLS here.
#define ROWS 21
#define COLS 80
// You can change the characters used to represent DEAD and ALIVE cells here
#define DEAD ' '
#define ALIVE '*'
void generation(vector< vector<char> > &world, vector< vector<char> > &world_copy);
void display(vector< vector<char> > &world);
int main()
{
vector< vector<char> > world(ROWS, vector<char>(COLS, DEAD));
vector< vector<char> > copy(ROWS, vector<char>(COLS, DEAD));
// set some cells of world to ALIVE for initial configuration
//Hard code the initial conditions.
world[1][1] = world[1][2] = world[1][3] = ALIVE;
while(true)
{
// clear the screen and display the world
system("clear"); // linux / Mac OS X
// system("cls"); // Windows
display(world);
// wait for some time
usleep(800000); // linux / Mac OS X
//sleep(800) //windows
// update the world
generation(world, copy);
}
return 0;
}
void generation(vector< vector<char> > &world, vector< vector<char> > &world_copy)
{
// copy the contents of world into world_copy
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLS;j++)
{
world_copy[i][j]=world[i][j];
}
}
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
// look at world_copy[i][j]'s neighbors and count ones that are alive
// update world[i][j] based on it
// Be careful when dealing with cells on the boundary since they
// do not have eight neighbors
int count=0;
if(i==0) //Checking the First Row
{
if(j==0) //Checking Top Left Corner
{
if(world_copy[i][j+1]=='*') //Check the Right
{
count++;
}
if(world_copy[i+1][j+1]=='*') //Check Bottom Right Corner
{
count++;
}
if(world_copy[i+1][j]=='*') //Check Bottom
{
count++;
}
if(world_copy[i+1][COLS]=='*')//Check the Bottom Left
{
count++;
}
if(world_copy[i][COLS]=='*') //Check the Left
{
count++;
}
if(world_copy[ROWS][COLS]=='*') //Check the Top Left Corner
{
count++;
}
if(world_copy[ROWS][j]=='*') //Check the Top
{
count++;
}
if(world_copy[ROWS][j+1]=='*') //Check the Top Right
{
count++;
}
}
if(j==COLS) //Checking Top Right Corner
{
//I have this part filled in and I'm sure it is right
}
else
{
//I have this part filled
}
}
if((i!=0)||(i!=ROWS))
{
if(j==0)
{
//I have this filled
}
if(j==COLS)
{
//I have this filled
}
else
{
//This part is filled
}
}
if(i==ROWS)
{
if(j==0)
{
if(world_copy[i][j+1]=='*') //Check the Right
{
count++;
}
if(world_copy[0][j+1]=='*') //Check Bottom Right Corner
{
count++;
}
if(world_copy[0][j]=='*') //Check Bottom
{
count++;
}
if(world_copy[0][COLS]=='*')//Check the Bottom Left
{
count++;
}
if(world_copy[i][COLS]=='*') //Check the Left
{
count++;
}
if(world_copy[i-1][COLS]=='*') //Check the Top Left Corner
{
count++;
}
if(world_copy[i-1][j]=='*') //Check the Top
{
count++;
}
if(world_copy[i-1][j+1]=='*') //Check the Top Right
{
count++;
}
}
if(j==COLS)
{
if(world_copy[i][0]=='*') //Check the Right
{
count++;
}
if(world_copy[0][0]=='*') //Check Bottom Right Corner
{
count++;
}
if(world_copy[0][j]=='*') //Check Bottom
{
count++;
}
if(world_copy[0][j-1]=='*')//Check the Bottom Left
{
count++;
}
if(world_copy[i][j-1]=='*') //Check the Left
{
count++;
}
if(world_copy[i-1][j-1]=='*') //Check the Top Left Corner
{
count++;
}
if(world_copy[i-1][j]=='*') //Check the Top
{
count++;
}
if(world_copy[i-1][0]=='*') //Check the Top Right
{
count++;
}
}
else
{
if(world_copy[i][j+1]=='*') //Check the Right
{
count++;
}
if(world_copy[0][j+1]=='*') //Check Bottom Right Corner
{
count++;
}
if(world_copy[0][j]=='*') //Check Bottom
{
count++;
}
if(world_copy[0][j-1]=='*')//Check the Bottom Left
{
count++;
}
if(world_copy[i][j-1]=='*') //Check the Left
{
count++;
}
if(world_copy[i-1][j-1]=='*') //Check the Top Left Corner
{
count++;
}
if(world_copy[i-1][j]=='*') //Check the Top
{
count++;
}
if(world_copy[i-1][j+1]=='*') //Check the Top Right
{
count++;
}
}
}
if(count==3 && world[i][j]==' ')
{
world_copy[i][j]='*';
}
else
{
if((count==2 || count ==3) && world[i][j]=='*')
{
world_copy[i][j]='*';
}
else
{
world_copy[i][j]=' ';
}
}
}
}
}
void display(vector< vector<char> > &world)
{
// display the 2D matrix
// You can add more code to 'beautify' the display of the matrix
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
cout << world[i][j];
}
cout << endl;
}
}
Thank you for catching that. I have made everything in bound now. However, I get this message in the output saying "TERM environment variable not set." (what I don't want) along with my output (which i want)