1 Replies - 929 Views - Last Post: 09 December 2010 - 05:23 PM Rate Topic: -----

#1 dasker  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-November 10

Counting the number of cells in my array

Posted 09 December 2010 - 03:13 PM

I need some help I created a program thatis basically the game of life. It goes through 5 generations and after the last generation I want to count up the number of * asterisks in (gen4).
I created each Gen function as a type bool. I do not know how to change that to an INt to give me the total.
I also want to count the total astericks * in row [10] and column[10]. any help would be appreciated.



#include<iostream>
#include<cstdlib>
#include<fstream>
#include <iomanip>
 

using namespace std;

const int MAX = 20; 
bool oldArray[MAX][MAX]; 
bool newArray[MAX][MAX]; 
bool gen1[MAX][MAX];
bool gen2[MAX][MAX];					   // i can use these to do each new generation but i would like to simplify 
bool gen3[MAX][MAX];					  // if at all possible
bool gen4[MAX][MAX];
bool gen5[MAX][MAX];

void display(bool Array[][20]);
int countneighbors(bool Array[][20],int row, int col);
void clearArray (bool image [MAX][MAX]);
bool setArray(bool oldArray[20][20],int x, int y);
void generation(bool Array[][20], bool newArray[][20]);



int main()
{

	ifstream in;
	in.open("mylife.txt");
	if (!in)
		cout << "failed to open file";
	else 
	{
		clearArray(oldArray); 
		while (in)
		{
			int row, col;
			in >> row >> col;
			if (in)
				setArray(oldArray,row,col);
		}
	}
	cout<<" original grid:"<< endl<<endl;
	display(oldArray); 
	
	cout << "The grid after 1 generations have passed:" << endl<<endl; 
	generation(oldArray,newArray); 
	display(newArray); 
 

	cout << "The grid after 2 generations have passed:" << endl<<endl; 
	generation(newArray,gen1);
    display(gen1);
    

	cout << "The grid after 3 generations have passed:" << endl<<endl; 
	generation(gen1,gen2);
    display(gen2);
    
    cout << "The grid after 4 generations have passed:" << endl<<endl; 
	generation(gen2,gen3);
    display(gen3);
    

	cout << "The grid after 5 generations have passed:" << endl<<endl; 
	generation(gen3,gen4);
    display(gen4);
	
	cout<< "The number of living cells in the entire board" << endl;
	cout<<"The number of living cells in row 10  " <<endl; 
	cout<< " The number of living cells in column 10" << endl;
	in.close();

 system("pause");
	return 0;
}

//**************************************************************************
 void clearArray(bool image[MAX][MAX])
	{
		// Clear the array
		for (int row = 0; row < MAX; row++)
			for (int col = 0; col < MAX; col++)
				image[row][col] = false;
	}

//****************************************************************************
bool setArray(bool oldArray[20][20], int x, int y)
{
	  oldArray[x][y] = true; 

	  return true; 
}

//****************************************************************************
void generation(bool Array[][20], bool newArray[][20])
{ 
	for (int row = 0; row < MAX ; row++)
	{
			for (int col = 0; col < MAX ; col++)
			{	
				int neighbors = countneighbors(Array, row, col);

				if(Array[row][col] && neighbors == 2)
					newArray[row][col] = true; 
				if(neighbors == 3)
					newArray[row][col] = true; 
				if(neighbors < 2)
					newArray[row][col] = false;
				if(neighbors > 3)
					newArray[row][col] = false;
			}
	}
}

//********************************************************************************
void display(bool oldArray[20][20])
{

     int horizontal = 0;
   //prints horizontal reference numbers, x-axis
   cout<<"  ";
   for (int ref = 0; ref < MAX; ref++)
   {   
       
      cout<< horizontal;
      horizontal++;
      if (horizontal == 10) horizontal = 0;
   }
     cout<< endl;
   
	for (int row = 0; row < MAX; row++)
		{
             if (row< 10)
             cout<<" ";
             cout<< row;
			for (int col = 0; col < MAX; col++)
			{
				if (oldArray[row][col])
					cout << 'X';
				else
					cout <<" ";
			}
			cout << endl;
		}
		cout << endl;
}
//*************************************************************************
  int countneighbors( bool life[][20], int row, int col )
{
  int neighbors=0;

  if ( col > 0 && life[row][col-1] == true )
         neighbors++;
  if ( col < 19 && life[row][col+1] == true )
         neighbors++;

  if (row > 0) {
    if ( col > 0 && life[row-1][col-1] == true )
         neighbors++;
   if ( col < 19 && life[row-1][col+1] == true )
         neighbors++;
   if ( life[row-1][col] == true )
         neighbors++;
  }

  if (row < 19) {
    if ( col > 0 && life[row+1][col-1] == true )
         neighbors++;
    if ( col < 19 && life[row+1][col+1] == true )
         neighbors++;
    if ( life[row+1][col] == true )
         neighbors++;
  }

 return neighbors;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Counting the number of cells in my array

#2 dasker  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 10-November 10

Re: Counting the number of cells in my array

Posted 09 December 2010 - 05:23 PM

I added this function. But i am confused on how I tell it to count each ( * ) in the last generation. The parameter that I was trying to pass was total but I do not have this set up to count the *. Not sure how to set this up.


[code

//***************************************************************************
int getTotal(int somearray[][MAX])
{

int total=0; //used for summing the elements
if(somearray[][MAX]== true)
total++;
return total;
}



[/code]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1