Hello DaniWeb users,

I hope one of you can assist me.

I did search and found nothing to help me.

So below is my code and I am hoping that someone can please tell me what on earth I am forgetting. I have been playing with this for quite some time now.

When I run it... my 20x20 becomes all @'s... which it shouldn't be doing that... So if there is any kind soul out there, please oh please get me headed in the right direction!

Thank you! :confused:

``````// Sample program for HW1 "Life" problem.

/* <At least, the following functions should be implemented>
[DONE?]void Display(......) -- will display the current generation map.
[DONE]void Readint(......)  -- will read the initial positions and the number of generations
you will generate.
[done?]void  NextGeneration(......)  -- will generate the next generation map using
[done?]CheckNeighbor function for every position.
[DONE]void  CopyMatrix(……)    -- will copy one matrix to another.
[DONE]void  ClearMatrix(……)  -- will clear a matrix.
*/

#include <iostream>
using namespace std;

const int N_ROW = 20;
const int N_COL = 20;

typedef	char MapType[N_ROW][N_COL];

void Display(int, int, MapType);
void NextGeneration(MapType&, MapType);
int CheckNeighbor(int, int, MapType&);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);

int CheckNeighbor(int Row, int Col, MapType& org){
int numN=0;
for(int K = Row-1; K <= Row+1; K++)
{
for(int J = Col-1; J <= Col+1; J++)
{
numN = numN + org[K][J];
}
}
numN = numN - org[Row][Col];
//cout << numN << "\n";
return numN;

}
void NextGeneration (MapType& orggen, MapType nextgen){
MapType New;
int Row, Col;
//1.	An organism is born in any empty cell having exactly three neighbors.
// if 3 neighbors = born
//2.	An organism dies from isolation if it has fewer than two neighbors.
// if <2 neighbors = death
//3.	An organism dies from overcrowding if it has more than three neighbors.
// if >3 neighbors = death
//4.	All other organisms survive to the next generation.
// else = alive

for (Row = 1; Row < N_ROW; Row = Row + 1)
for (Col = 1; Col < N_COL; Col = Col + 1)
{
if (orggen[Row][Col]==0)
if (CheckNeighbor(Row, Col, orggen) == 3)
New[Row][Col] = 1;   // Born
else
New[Row][Col] = 0;
else
if((CheckNeighbor(Row,Col,orggen) <2) || (CheckNeighbor(Row,Col,orggen) >3))
New[Row][Col] = 0; //Death
else
New[Row][Col] = 1;
}
for (Row = 1; Row < N_ROW; Row = Row + 1)
for (Col = 1; Col < N_COL; Col = Col + 1)
{
orggen[Row][Col] = nextgen[Row][Col];
}
}

void Display(int rows, int cols, MapType org){
for(int i = 0; i < N_ROW; i++)
{
for(int j = 0; j < N_COL; j++)
{
if(org[i][j]>0)
cout << '@';
else
cout<<' ';
}
cout << "\n";
}
}
void CopyMatrix(MapType org, MapType next){
for (int i=0;i<N_ROW;i++)
for (int j=0; j<N_COL;j++)
next[i][j]=org[i][j];
}

void ClearMatrix(MapType Map){
// To make the map blank
for (int i = 0; i < N_ROW; i++)
for (int j = 0; j <N_COL; j++)
Map[i][j] = ' ';
}

void Readint(int& noc, int& nog, MapType org)
{
int	i,j;
int	rowno, colno;
// To initialize the original map as blank
for (i = 0; i < N_ROW; i++)
for (j = 0; j <N_COL; j++)
org[i][j] = ' ';
// To read the number of the initial cells
cout<<"Please enter the number of initial cells: ";
cin>>noc;
// To read the initial cell position(s)
for (i = 0; i < noc; i++)
{
cout<<"The position of cell is (row, col): ";
cin>>rowno>>colno;
//org[rowno][colno] = '@';
}
// To read the number of generations
cout<<"How many generations do you want to generate? ";
cin >> nog;
}

int main(){
int	no_cells;
int	no_gens;
int i=0;
MapType	currmap, nextmap;
char yesno='n';
do {
ClearMatrix(currmap);
Display(i,no_cells, currmap);
cout<<"Press any key to continue!! "<<endl;
cin.get();
for(i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap, nextmap);
CopyMatrix(currmap, nextmap);
Display(i, no_cells, currmap);
cout<<"Press any key to continue!!" <<endl;
cin.get();
}
cout<<"Do you want to do it again? (Yes/No)";
cin>> yesno;}
while(yesno=='Y' || yesno =='y');
cout<<"Good Bye"<<endl;
return 0;
}``````
3
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by BBallAsh23

You need to look closely at what you're doing in the NextGeneration( ) function. You pass in nextgen array, but don't use it to calculate the changes. Then you go and copy the blank nextgen array over the original.

Be consistent in writing 0 or 1 to the arrays, not @. Leave the display function to interpret them.

``````// Sample program for HW1 "Life" problem.

/* <At least, the following functions should be implemented>
[DONE?]void Display(......) -- will display the current generation map.
[DONE]void Readint(......) -- will read the initial positions and the number of generations
you will generate.
[done?]void NextGeneration(......) -- will generate the next generation map using
[done?]CheckNeighbor function for every position.
[DONE]void CopyMatrix(……) -- will copy one matrix to another.
[DONE]void ClearMatrix(……) -- will clear a matrix.
*/

#include <iostream>
using namespace std;

const int N_ROW = 20;
const int N_COL = 20;

typedef char MapType[N_ROW][N_COL];

void Display(MapType);
void NextGeneration(MapType);
int CheckNeighbor(int, int, MapType);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);

int CheckNeighbor(int Row, int Col, MapType org){
int numN=0;
for(int i = Row-1; i <= Row+1; i++)
for(int j = Col-1; j <= Col+1; j++)
{
if (org[i][j]==1)
numN++;
}
return numN;
}
void NextGeneration(MapType orggen){
int Row, Col, numN;
MapType New;
for (Row = 1; Row < N_ROW-1; Row++)
for (Col = 1; Col < N_COL-1; Col++)
numN = CheckNeighbor(Row,Col,orggen);
if (numN == 3)
New[Row][Col] = 1; // Born OR Stays Alive
else if((numN <2) || (numN >3))
New[Row][Col] = 0; //Death OR Stays Blank
else
New[Row][Col] = orggen[Row][Col]; //Stays the Same
}

void Display(MapType org){
for(int i = 0; i < N_ROW; i++)
for(int j = 0; j < N_COL; j++)
{
if(org[i][j]==1)
cout << '@';
else
cout<<' ';
}
cout << "\n";
}
void CopyMatrix(MapType org, MapType next){
for (int i=0;i<N_ROW;i++)
for (int j=0; j<N_COL;j++)
org[i][j]=next[i][j];
}

void ClearMatrix(MapType org){
// To make the map blank
for (int i = 0; i < N_ROW; i++)
for (int j = 0; j < N_COL; j++)
org[i][j] = 0;
}

void Readint(int& noc, int& nog, MapType org)
{
int rowno, colno;
// To read the number of the initial cells
cout<<"Please enter the number of initial cells: ";
cin>>noc;
// To read the initial cell position(s)
for (int i = 0; i < noc; i++)
{
cout<<"The position of cell is (row, col): ";
cin>>rowno>>colno;
if((rowno < 1) || (rowno > 19))
{cout<< "Invalid position, enter new position. ";
org[rowno][colno] = 0;
cin >> rowno>>colno;}
if((colno < 1) || (colno > 19))
{cout<< "Invalid position, enter new position. ";
org[rowno][colno] = 0;
cin >> rowno>>colno;}
org[rowno][colno] = 1;
}
// To read the number of generations
cout<<"How many generations do you want to generate? ";
cin >> nog;
}

int main(){
int no_cells;
int no_gens;
int i=0;
MapType currmap, nextmap;
char yesno='n';
do {
ClearMatrix(currmap);
Display(currmap);
system("Pause");
for(i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap);
CopyMatrix(currmap, nextmap);
Display(nextmap);
system("Pause");
}
cout<<"Do you want to do it again? (Y/N)";
cin>> yesno;}
while(yesno=='Y' || yesno =='y');
cout<<"Good Bye"<<endl;
return 0;
}``````

So, does it work or not? If not, what do you see happening/not happening? What did you change?

And fixing the indenting to be at least as good as your first post would be helpful.

No it doesn't work.

Sorry I just took it from my email. I was working on it at work as well.

It is getting worse. I am about to give up on life. Ha.

Before it was displaying better... but now it is going crazy.

``````#include <iostream>
using namespace std;

const int N_ROW = 20;
const int N_COL = 20;

typedef char MapType[N_ROW][N_COL];

void Display(MapType);
void NextGeneration(MapType, MapType);
int CheckNeighbor(int, int, MapType);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);

int CheckNeighbor(int Row, int Col, MapType org){
int numN=0;
for(int i = Row-1; i <= Row+1; i++)
{
for(int j = Col-1; j <= Col+1; j++)
{
if (org[i][j]==1)
numN++;
}
}
return numN;
}
void NextGeneration(MapType orggen, MapType New){
int Row, Col, numN;
for (Row = 1; Row < N_ROW-1; Row++)
{
for (Col = 1; Col < N_COL-1; Col++)
numN = CheckNeighbor(Row,Col,orggen);
if (numN == 3)
New[Row][Col] = 1; // Born OR Stays Alive
else if((numN <2) || (numN >3))
New[Row][Col] = 0; //Death OR Stays Blank
else
New[Row][Col] = orggen[Row][Col]; //Stays the Same
}
}

void Display(MapType org){
for(int i = 0; i < N_ROW; i++)
{
for(int j = 0; j < N_COL; j++)
{
if(org[i][j]==1)
cout << '@';
else
cout<<' ';
}
cout << "\n";
}
}
void CopyMatrix(MapType org, MapType next){
for (int i=0;i<N_ROW;i++)
{
for (int j=0; j<N_COL;j++)
org[i][j]=next[i][j];
}
}

void ClearMatrix(MapType org){
for (int i = 0; i < N_ROW; i++)
{
for (int j = 0; j < N_COL; j++)
org[i][j] = 0;
}
}

void Readint(int& noc, int& nog, MapType org)
{
int rowno, colno;
cout<<"Please enter the number of initial cells: ";
cin>>noc;
// To read the initial cell position(s)
for (int i = 0; i < noc; i++){
do{
cout<<"The position of cell is (row, col): ";
cin>>rowno>>colno;
org[rowno][colno]=1;}
while(((rowno < 1) || (rowno > 19)) && ((colno < 1) || (colno > 19)));}

// To read the number of generations
cout<<"How many generations do you want to generate? ";
cin >> nog;
}

int main(){
int no_cells, no_gens;
int i=0;
MapType currmap, nextmap;
char yesno='n';
do {
ClearMatrix(currmap);
Display(currmap);
system("Pause");
for(i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap, nextmap);
CopyMatrix(currmap, nextmap);
Display(nextmap);
system("Pause");
}
cout<<"Do you want to do it again? (Y/N)";
cin>> yesno;}
while(yesno=='Y' || yesno =='y');
cout<<"Good Bye"<<endl;
return 0;
}``````

Alright... so I think it is getting there but something is still going wrong and I can't determine where...

``````#include <iostream>
using namespace std;

const int N_ROW = 30;
const int N_COL = 30;

typedef char MapType[N_ROW][N_COL];

void Display(MapType);
void NextGeneration(MapType, MapType);
int CheckNeighbor(int, int, MapType);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);

int CheckNeighbor(int Row, int Col, MapType org){
int numN=0;
if (org[Row-1][Col-1] == 1)
numN++;
if ( org[Row-1][Col] == 1)
numN++;
if (org[Row-1][Col+1] == 1)
numN++;
if (org[Row][Col-1] == 1)
numN++;
if ( org[Row][Col+1] == 1)
numN++;
if (org[Row-1][Col-1] == 1)
numN++;
if(org[Row+1][Col] == 1)
numN++;
if (org[Row+1][Col+1] == 1)
numN++;
return numN;
}
void NextGeneration(MapType orggen, MapType New){
for (int r=0; r<N_ROW; r++)
{
for (int c=0; c<N_COL; c++)
{
int numN = CheckNeighbor(r,c,orggen);
if (numN == 3)
New[r][c] = 1; // Born OR Stays Alive
else if((numN <2) || (numN >3))
New[r][c] = 0; //Death OR Stays Blank
else
New[r][c] = orggen[r][c]; //Stays the Same
for(int r=0; r<N_ROW; r++)
for(int c=0;c<N_COL;c++)
orggen[r][c] = New[r][c];
}
}
}

void Display(MapType org){
for(int i = 0; i < N_ROW; i++)
{
for(int j = 0; j < N_COL; j++)
{
if(org[i][j]==1)
cout << '@';
else
cout<<' ';
}
cout << "\n";
}
}
void CopyMatrix(MapType org, MapType next){
for (int i=0;i<N_ROW;i++)
{
for (int j=0; j<N_COL;j++)
next[i][j]=org[i][j];
}
}

void ClearMatrix(MapType org){
for (int i = 0; i < N_ROW; i++)
{
for (int j = 0; j < N_COL; j++)
org[i][j] = 0;
}
}

void Readint(int& noc, int& nog, MapType org){
int rowno, colno;
cout<<"Please enter the number of initial cells: ";
cin>>noc;
// To read the initial cell position(s)
for (int i = 0; i < noc; i++)
do{
cout<<"The position of cell is (row, col): ";
cin>>rowno>>colno;
org[rowno][colno]=1;}
while(((rowno < 1) || (rowno > 29)) && ((colno < 1) || (colno > 29)));

cout<<"How many generations do you want to generate? ";
cin >> nog;
}

int main(){
int no_cells, no_gens;
int i=0;
MapType currmap, nextmap;
char yesno='n';
do {
ClearMatrix(currmap);
Display(currmap);
system("Pause");
for(i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap, nextmap);
CopyMatrix(currmap, nextmap);
Display(nextmap);
system("Pause");
}
cout<<"Do you want to do it again? (Y/N)";
cin>> yesno;}
while(yesno=='Y' || yesno =='y');
cout<<"Good Bye"<<endl;
return 0;
}``````

I got an endless loop of this:

``The position of cell is (row, col):``

What am I supposed to get?

SO IT WORKS... It helps when the neighborcheck doesn't check itself twice... haha.

``````#include <iostream>
using namespace std;

const int SIZE = 30;
typedef char MapType[SIZE][SIZE];

void Display(int, MapType);
void NextGeneration(MapType, MapType);
int CheckNeighbor(MapType,int, int);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);

int CheckNeighbor(MapType org,int Row, int Col){
int numN=0;
if (org[Row-1][Col-1] == '@')
numN++;
if (org[Row-1][Col] == '@')
numN++;
if (org[Row-1][Col+1] == '@')
numN++;
if (org[Row][Col-1] == '@')
numN++;
if (org[Row][Col+1] == '@')
numN++;
if (org[Row+1][Col-1] == '@')
numN++;
if(org[Row+1][Col] == '@')
numN++;
if (org[Row+1][Col+1] == '@')
numN++;
return numN;
};
void NextGeneration(MapType org, MapType New){
int numN;
for (int Row = 0; Row < SIZE-1; Row++)
for (int Col = 0; Col < SIZE-1; Col++)
{
numN = CheckNeighbor(org,Row,Col);
if (numN == 3)
New[Row][Col] = '@';
else if(numN < 2)
New[Row][Col] = ' ';
else if(numN > 3)
New[Row][Col]=' ';
else
New[Row][Col] = org[Row][Col];
}
};

void Display(int nog, MapType org){
int cells=0;

cout << endl << "Generation " << nog << ":" << endl;
for(int Row = 0; Row < SIZE; Row++)
{
for(int Col = 0; Col < SIZE; Col++)
{
cout << org[Row][Col] << " ";
if(org[Row][Col] == '@')
cells++;
}
cout << endl;
}
cout << "This generation has " << cells << " living cells." << endl;
};
void CopyMatrix(MapType org, MapType New){
for (int i=0; i<SIZE;i++)
for (int j=0; j<SIZE;j++)
org[i][j]=New[i][j];
};

void ClearMatrix(MapType org){
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
org[i][j] = ' ';
};

int rowno, colno, noc;

do{
cout<<"Please enter the number of initial cells, no more than 15 : ";
cin>>noc;
} while((noc < 1)||(noc > 15));

for(int i = 1; i <= noc; i++)
{
cout << "Enter the position of cell " << i <<endl;;
do {
cout << "Enter Row (1-29): ";
cin >> rowno;
} while((rowno < 1)||(rowno > SIZE-1));

do {
cout << "Enter Col (1-29): ";
cin >> colno;
} while((colno < 1)||(colno > SIZE-1));
org[rowno][colno] = '@';
}

cout<<"How many generations do you want to generate? ";
cin >> nog;
};

int main(){
int no_gens;
MapType currmap, nextmap;
char yesno;

do {
ClearMatrix(currmap);
Display(1, currmap);
system("Pause");
for(int i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap, nextmap);
CopyMatrix(currmap, nextmap);
Display(i, currmap);
system("Pause");
}
cout<<"Do you want to do it again? (Y/N)";
cin>> yesno;}
while(yesno=='Y' || yesno =='y');
system("Pause");
return 0;
}``````