ok, so here is a link to my project guidelines...
http://www.santarosa.edu/~ssarkar/cs10sm09/cis10/proj5.html
by the way, the deadline was extended to 08/07/09
and here is my code for my program... it compiles and counts perfectly, but for my project i need the program to go from generation 1 to generation 2 to generation 3, etc... using the previous generation. the text file containing the data to be read into the program has been attached, its called "lifedata.txt, and here is my code. I desperately need help as i need to turn this in in 10 hours. I have also added the output to the end of my code.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cassert>
using namespace std;
const int SIZE = 20;
int countNeighbors(bool life[][SIZE], int row, int col);
void printGrid1(bool [][SIZE]);
void printGrid2(bool [][SIZE]);
void readGrid(bool [][SIZE]);
void initGrid(bool life[][SIZE]);
void determineNextGen(bool [][SIZE], bool [][SIZE]);
int main()
{
bool life[SIZE][SIZE];
bool life2[SIZE][SIZE];
initGrid(life);
initGrid(life2);
//printGrid1(life);
readGrid(life);
printGrid2(life);
determineNextGen(life,life2);
system ("PAUSE");
return 0;
}
int countNeighbors(bool life[][SIZE], int row, int col)
{
int count = 0;
for (int rowCount = row-1; rowCount <= row+1; rowCount++){
for (int colCount = col-1; colCount <= col+1; colCount++){
if (rowCount >= 0 && rowCount < SIZE && colCount >=0 && colCount < SIZE){
if (life[rowCount][colCount]){
count++;
}
}
}
}
if (life[row][col]){ //because we counted life[row][col] as a neighbor
count--;
}
return count;
}
void determineNextGen(bool life[][SIZE], bool life2[][SIZE])
{
int count=0;
for(int row=0;row<SIZE;row++)
{
for(int col=0;col<SIZE;col++)
{
count=countNeighbors(life, row, col);
if(life[row][col] && (count <2 ||count>3))
{
life2[row][col]=true;
}
if(!life[row][col] && count==3)
{
life2[row][col]=true;
}
}
}
}
void printGrid1(bool life[][SIZE])
{
//print contents of array after initialization
cout << " 01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++)
{
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++)
{
cout<<life[row][col];
}
cout << endl;
}
}
void printGrid2 (bool life[][SIZE])
{
//print contents of two-dimensional array
cout << " 01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++)
{
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++)
{
if (life[row][col])
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
//total alive in row 10
int row=10;
int rowtotal=0;
for (int col = 0; col < SIZE; col++)
{
if(life[row][col])
rowtotal=rowtotal+1;
}
cout<<"total alive in row 10 is "<<rowtotal<<endl;
//total alive in col 10
int col=10;
int coltotal=0;
for (int row = 0; row < SIZE; row++)
{
if(life[row][col])
coltotal=coltotal+1;
}
cout<<"total alive in column 10 is "<<coltotal<<endl;
//total alive in grid
int totalalive=0;
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
if( life[row][col])
totalalive++;
}
}
cout<<"total alive cells is "<<totalalive<<endl;
}
void initGrid(bool life[][SIZE])
{
for(int row=0;row<SIZE;row++)
{
for(int col=0;col<SIZE;col++)
{
life[row][col]=0;
}
}
}
void readGrid(bool life[][SIZE])
{
ifstream inData;
string fileName;
int row, col;
cout<<"Please enter filename"<<endl;
cin>>fileName;
inData.open(fileName.c_str());
inData>>row>>col;
while(inData)
{
life[row][col]=true;
inData>>row>>col;
}
}
/* OUTPUT
Please enter filename
lifedata.txt
01234567890123456789
0* * * *
1 * * *
2 * * * *** *
3 * * ** * *
4* * * *
5 ** * ***
6 ** ** ** **
7 * * * *
8 * * * * *
9 ** **
10* * *
11 *** * ** * * *
12 *** *
13** * ** *****
14 * * * * * *
15 * * *
16 ** * * *
17 **
18 * * *
19* ** *
total alive in row 10 is 3
total alive in column 10 is 4
total alive cells is 100
Press any key to continue . . .
*/