Segmentation Fault in Game of Life program

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1058 Views - Last Post: 29 February 2008 - 05:35 PM Rate Topic: -----

#1 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Segmentation Fault in Game of Life program

Posted 29 February 2008 - 11:18 AM

OK, My assignment is to write a basic version of The Game of Life. I've written part of it and I wanted to check and see if everything worked so far, but when I ran the program it got past the first step then spit out "Segmentation fault." I don't know what that mean or how to fix it, I'm just a programing noob.

The program only successfully asks for the size of the cell, then prints out the error.
Here's my code so far:
 
#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

// function prototypes
void initialize_array(char array[MAX_SIZE][MAX_SIZE], int size);
// prototype for function to output the array
// prototype for function to grow the array
bool is_alive(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col);
// prototype for function to count the number of live neighbors of cell(i,j)

int main()
{
  // declare variables, including array to hold cell array
  int n, k, x, y;
  char A[100][100];

  // ask for cell array size
  cout << "Enter the size of the cell: ";
  cin >> n;

  // check that cell array size is within minimum and maximum bounds
  // if not, persistently ask for cell array size
  // until number is within bounds
	while (n < 5 || n > 100) {
	  cout << "Cell size must be between 5 and 100."<<endl;
	  cout << "Re-enter cell size";
	  cin >> n;
	}

  //  initialize_array(array, size);
  for (int p=1; p<= n*n; p++) {
	A[p-1][p-1] = '.';
	  }

  // ask for number of seeds
  cout << "Input number of seeds:";
  cin >> k;

  // ask for row and column of each seed
  // check that row and column is within the cell array
  // if not, persistently ask for the row and column
  // until row and column are within the cell array
  // set the cell at the given row and column to '*' (baby)
  for (int s=1; s<= k; s++) {
	cout <<"Enter row and column for seed "<<s;
	cin >>y;
	cin >>x;

	if (y<0 || y>n-1 || x<0 || x>n-1) {	
	  while (y<0 || y>n-1 || x<0 || x>n-1) {
	cout << "coordinates are out side of specified array."<<endl;
	cout << "Re-enter row and columb of seed "<<s;
	cin >>y;
	cin >>x;
	  }
	}

	else {
	  A[y][x] = '*';
	}
  }

  // call the function to output the initial array
  for (int row=0; row<n; row++) {
	for (int col=0; col<n; col++) {
	  cout << A[row][col] << " ";
	}
	cout << endl;
  }



Sorry if the comments get in the way, they were in the starter file we were given.
Also if you see any other problems besides the immediate please tell me, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation Fault in Game of Life program

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 11:21 AM

int main should return an int.

Try adding a return value at the end of the main function (code block)
Was This Post Helpful? 0
  • +
  • -

#3 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 11:31 AM

View Postno2pencil, on 29 Feb, 2008 - 11:21 AM, said:

int main should return an int.

Try adding a return value at the end of the main function (code block)


The return value is further down in the starter file. I didn't include the whole thing because the rest is just comments on steps I haven't reached.
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 11:55 AM

 //  initialize_array(array, size);
  for (int p=1; p<= n*n; p++) {
    A[p-1][p-1] = '.';
      }


Your error should be here. Let's say the user enters 92 then, at the for loop condition p will increment from 1 to (92 * 92) = 8464 and that is really out of bounds of the maximum index of the array.

I suggest accepting value of n between 5 and 10 if you want to use n*n
OR

 //  initialize_array(array, size);
  for (int p=1; p<= n; p++) {
    A[p-1][p-1] = '.';
      }


This post has been edited by letthecolorsrumble: 29 February 2008 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 12:20 PM

View Postletthecolorsrumble, on 29 Feb, 2008 - 11:55 AM, said:

 //  initialize_array(array, size);
  for (int p=1; p<= n*n; p++) {
    A[p-1][p-1] = '.';
      }


Your error should be here. Let's say the user enters 92 then, at the for loop condition p will increment from 1 to (92 * 92) = 8464 and that is really out of bounds of the maximum index of the array.

I suggest accepting value of n between 5 and 10 if you want to use n*n
OR

 //  initialize_array(array, size);
  for (int p=1; p<= n; p++) {
    A[p-1][p-1] = '.';
      }



That nailed it, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 01:34 PM

Now I have another problem, that will most likely be just as easy to fix.

My new code is this:
#include <iostream>
using namespace std;

const int MAX_SIZE = 100;

// function prototypes
void initialize_array(char array[MAX_SIZE][MAX_SIZE], int size);
// prototype for function to output the array
// prototype for function to grow the array
bool is_alive(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col);
// prototype for function to count the number of live neighbors of cell(i,j)

int main()
{
  // declare variables, including array to hold cell array
  int n, k, x, y, simsteps, step, nbr[100][100];
  char A[100][100];

  // ask for cell array size
  cout << "Enter the size of the cell: ";
  cin >> n;

  // check that cell array size is within minimum and maximum bounds
  // if not, persistently ask for cell array size
  // until number is within bounds
	while (n < 5 || n > 100) {
	  cout << "Cell size must be between 5 and 100."<<endl;
	  cout << "Re-enter cell size";
	  cin >> n;
	}

  //  initialize_array(array, size);
	for (int row=1; row<=n; row++) {
	  for (int col=1; col<=n; col++) {
	A[row-1][col-1]='.';
	  }
	}

  // ask for number of seeds
  cout << "Input number of seeds:";
  cin >> k;

  // ask for row and column of each seed
  // check that row and column is within the cell array
  // if not, persistently ask for the row and column
  // until row and column are within the cell array
  // set the cell at the given row and column to '*' (baby)
  for (int s=1; s<= k; s++) {
	cout <<"Enter row and column for seed "<<s<<": ";
	cin >>y;
	cin >>x;

	if (y<0 || y>n-1 || x<0 || x>n-1) {	
	  while (y<0 || y>n-1 || x<0 || x>n-1) {
	cout << "coordinates are out side of specified array."<<endl;
	cout << "Re-enter row and columb of seed "<<s;
	cin >>y;
	cin >>x;
	  }
	}

	else {
	  A[y][x] = '*';
	}
  }

  // call the function to output the initial array
  for (int row=0; row<n; row++) {
	for (int col=0; col<n; col++) {
	  cout << A[row][col] << " ";
	}
	cout << endl;
  }

  // ask for the number of simulation steps
  cout << "Enter number of simulation steps: ";
  cin >>simsteps;

  // for each simulation step, call the function to grow the array
  while (step<=simsteps) {
	int nbr=0;
	for (int i=0; i<=n-1; i++) {
	  for (int j=0; j<=n-1; j++) {
	if (A[i+1][j-1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i+1][j]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i+1][j+1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i][j-1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i][j+1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i-1][j-1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i-1][j]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i-1][j+1]='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	  }
	}

	for (int i=0; i<=n-1; i++) {
	  for (int j=0; j<=n-1; j++) {
	if (A[i][j]='*') {
	  A[i][j]='@';
	}
	if (A[i][j]='.' && nbr[i][j]=3) {
	  A[i][j]='*';
	}
	if (nbr[i][j]<=1 || nbr[i][j]>=4) {
	  A[i][j]= '.';
	}
	  }
	 }

	for (int row=0; row<n; row++) {
	  for (int col=0; col<n; col++) {
	cout << A[row][col] << " ";
	  }
	  cout << endl;
	}
	step++;
  }

  // ask the user "Do you wish to continue?"
  // if yes, then ask again for the number of simulation steps
  // and continue the simulation for that number of steps
  // repeat this process until the user answers no
  // to the question "Do you wish to continue?"


  return 0;
}



When I try to compile this, I get the error message "invalid types `int[int]' for array subscript"; for the series of if statements that check the neighbors of A[i][j] (look for the 8 if statements about 2/3 down). This error message is in reference to the array nbr[i][j] (which stores the number of neighbors each point (i,j) has), but I initialized this array with the other variables so I don't understand what is wrong with it.

Also I realize that i probably did that section of code the hard way, I'd be glad to hear your advice on more efficient coding as well as your help on my particular problem.

Thanks for your help in advance. :)

This post has been edited by nosaj4268: 29 February 2008 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 02:25 PM

First: Incorrect if statements

if (A[i+1][j]='*') should be if (A[i+1][j]=='*')


second: in the for-loop i=0 & j=0 for the first iteration; so this----> if (A[i-1][j-1]=='*') will be like if(A[-1][-1]=='*')
which is not possible as array indexes are never negative.

This post has been edited by letthecolorsrumble: 29 February 2008 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#8 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 03:26 PM

View Postletthecolorsrumble, on 29 Feb, 2008 - 02:25 PM, said:

second: in the for-loop i=0 & j=0 for the first iteration; so this----> if (A[i-1][j-1]=='*') will be like if(A[-1][-1]=='*')
which is not possible as array indexes are never negative.


Right, so I need some conditional statements in those if statements so they aren't checking coordinates that are out of bounds. What would be the best way to do that?
some kind of while loop that would contain the if statements that basically said "while (i+1, i-1, j+1, j-1 =! out of bounds)". But what represents "out of bounds"? The null character, /0?

This post has been edited by nosaj4268: 29 February 2008 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 03:46 PM

For a array of size 100 , 0 is the lower bound and 99 is the upper bound.
Was This Post Helpful? 0
  • +
  • -

#10 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 03:58 PM

right, I know that; but if I'm checking on of the locations on the outer edge of the array, how do I keep it from checking the locations outside of the array.

Lets say for instance I have the array:
OOOOOO
XOOOOO
OOXOOO
OOXOOO
OOOOOO

If my program is set to check the number of neighbors that are "X"s for each coordinate in that array, then it will work fine on the inner coordinates, but on the outer points it will try to check the value for coordinates that don't exist. So what do I put in my code so it won't try and check coordinates that are non-existent?
Was This Post Helpful? 0
  • +
  • -

#11 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 04:14 PM

if i=0 then do not check if statements where A[i-1] is present and if j=0 do not check if statements where A[j-1] is present. If i=n-1 i.e. max. do not check if statements where A[i+1] is present and if j=n-1 do not check if statements where A[j+1] is present.

for the rest of the cases check all the if statements....

so it is more like a switch-case thing now
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4891
  • View blog
  • Posts: 11,286
  • Joined: 16-October 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 04:22 PM

You have a number of function prototypes at the begining of your program, but no implementations, even though you've already had to do an array init.

The answer to your question is to implement the is_alive function. Just return any out of bounds calls as false and the problem solves itself. e.g.

bool is_alive(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col) {
   if (row<0) || (row>=size) { return false; }
...
   return (array[row][col]==LIVE_CHAR);
}



No, this is not complete code. It should be a starting point. You'd do well to define a something like LIVE and EMPTY cell values somewhere for consistency.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#13 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 04:52 PM

View Postbaavgai, on 29 Feb, 2008 - 04:22 PM, said:

You have a number of function prototypes at the begining of your program, but no implementations, even though you've already had to do an array init.

The answer to your question is to implement the is_alive function. Just return any out of bounds calls as false and the problem solves itself. e.g.

bool is_alive(char array[MAX_SIZE][MAX_SIZE], int size, int row, int col) {
   if (row<0) || (row>=size) { return false; }
...
   return (array[row][col]==LIVE_CHAR);
}



No, this is not complete code. It should be a starting point. You'd do well to define a something like LIVE and EMPTY cell values somewhere for consistency.

Hope this helps.

The function prototypes are there because they were given to us in the seed file of our homework assignment, but since I'm not so handy with functions I haven't used them.
Was This Post Helpful? 0
  • +
  • -

#14 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 05:02 PM

View Postletthecolorsrumble, on 29 Feb, 2008 - 04:14 PM, said:

if i=0 then do not check if statements where A[i-1] is present and if j=0 do not check if statements where A[j-1] is present. If i=n-1 i.e. max. do not check if statements where A[i+1] is present and if j=n-1 do not check if statements where A[j+1] is present.

for the rest of the cases check all the if statements....

so it is more like a switch-case thing now


So, without employing switch statements; would my rewritten if statements look like this?:
if (j=!0) {
	  if (A[i+1][j-1]=='*'){
		nbr[i][j]=nbr[i][j]+1;
	  }
	  if (A[i][j-1]=='*'){
		nbr[i][j]=nbr[i][j]+1;
	  }
	}
	if (A[i+1][j]=='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i+1][j+1]=='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (A[i][j+1]=='*'){
	  nbr[i][j]=nbr[i][j]+1;
	}
	if (i=!0) {
	  if (j=!0) {
			if (A[i-1][j-1]=='*'){
		  nbr[i][j]=nbr[i][j]+1;
		}
	  }
	  if (A[i-1][j]=='*'){
		nbr[i][j]=nbr[i][j]+1;
	  }
	  if (A[i-1][j+1]=='*'){
		nbr[i][j]=nbr[i][j]+1;
	  }
	}


I only ask because I can't check to see if this works, seeing how I'm not on campus and therefore don't have access to the c++ software.
Was This Post Helpful? 0
  • +
  • -

#15 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Segmentation Fault in Game of Life program

Posted 29 February 2008 - 05:30 PM

Yeah it seems to be correct, (j!=0) and not (j=!0). You still need to apply, for (j==n-1) and (i==n-1) too.

There is Visual Studio C++ 2005/2008 Express Edition available free for download and installation if you wish to have it on your personal computer.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2