### 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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````#include #include #include #include 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.