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 Readint(int&, int&, MapType);
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);
Readint(no_cells,no_gens, 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;
}