Need code to print out X's and blank cells instead of 1's and 0's.

I am currently working on an assignment which tasks me with editing a simple gaming program that spits out 1's and 0's depending on the value of its neighboring numbers. Like the title says, one of my requirements is to make it so instead of printing out 1's and 0's as it usually does, I need the program to print out X's and blank cells, respectively. I am a bit lost on how to accomplish this, can someone point me in the right direction?

Here is the code so far:

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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <Windows.h>

using namespace std;

const int WIDTH = 49;
const int HEIGHT = 20;

// fills the arra/board with 0's and 1's (alive and dead cells)
void generate(int b[WIDTH][HEIGHT]){
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
			
b[j][i] = rand()%2;
}
}
}

// prints the array/board
void print(int b[WIDTH][HEIGHT]){

for(int i = 0; i < HEIGHT; i++){
		

for(int j = 0; j < WIDTH; j++)
{
	
cout << b[j][i];

			
}
cout << "\n";
}
}

// rules for updating a cell's status
int update_rules(int old, int sum){
if((sum < 5) || (sum > 6))
return 0;  // a cell with <2 or >3 neighbors 'dies'
if(sum == 6)
return 1;  // a cell with 3 neighbors 'lives'
if(sum == 5)
return old; // a cell with 2 neighbors doesn't change
}

// Count the number of neighbors for each cell
void update(int b[WIDTH][HEIGHT]){
int neighbori, neighborj;
int sum;
int temp[WIDTH][HEIGHT];  // temporary array
for(int i = 0; i < HEIGHT; i++){   // loop through every cell on the 'board'
for(int j = 0; j < WIDTH; j++){  // nested loops because its a 2d array
sum = 0;
for(int di = -1; di <= 1; di++){    // loop through all neighbors
for(int dj = -1; dj <= 1; dj++){
neighbori = i + di;
neighborj = j + dj;
if(neighbori < 0)      // makes sure you don't exceed the array bounds
neighbori = HEIGHT + neighbori;
if(neighborj < 0)
neighborj = WIDTH + neighborj;
neighbori = neighbori % HEIGHT;
neighborj = neighborj % WIDTH;
if(neighbori != i || neighborj != j)  // make sure a cell doesn't count itself
sum += b[neighborj][neighbori];   // add up the neighbors
}
}
// store the new value of the cell in the temporary array
temp[j][i] = update_rules(b[j][i],sum);  
}
}
// when done counting have cells switch to their new value.
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
b[j][i] = temp[j][i];
}
}
}

int main() {
srand(time(NULL)); // sets the random number seed
int board[WIDTH][HEIGHT];   // create a 2D array
generate(board);// fill the array with random values
print(board);               // print the array
while(1){          // loops 'forever'
update(board); // use the rules of life to update the array values
print(board);  // print the new array       
cout << "---------------------------------------"<< endl;
Sleep(150000);  // pause so things don't happen too fast
}
return 0;

}

line 30. Change:
cout << b[j][i];
to
cout << (b[j][i] ? 'X' : ' ');
Thank you very much. That did the trick. If you have a second, might you be able to explain to me what you did there? What does the ? operator do?
The ?: operator works as follows:
Given x ? y : z it will test to see if 'x' is true or false; if it is true, then the result of the operator is 'y'. Otherwise, it is 'z'.

So in this case, it checks b[j][i] and determines if it is true or false. Since for integers 0 is false and nonzero is true, the operator returns 'X' if b[j][i] is 0 and ' ' otherwise.
Topic archived. No new replies allowed.