Counting charsin my 2d Array


I wrote a program like the game of life and i go through several generations. At the end of my program I want to count the number of * in the last generation(gen4).
I need help writing this function. My gen4 is of type bool. The cout at the en of my program should print the total number of * and the number of * in row 10 and column 10. Can anyone help me with this function: This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

#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 getTotal(int somearray[MAX] [MAX])


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" << get total<< 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;
}

//This is where i am having problems
//*****************************************************************************
 //***************************************************************************
int getTotal(int somearray[MAX] [MAX])
{

    int total=0;                          //used for summing the elements
       if(gen4 [MAX][MAX] =='*')
       {
       total=gen4[MAX][MAX]++;
       }      
     return total;  


Topic archived. No new replies allowed.