### I could not attend class and i'm lost

This is translated from Icelandic but I hope y'all understand it.

Make a dimensional array with 9 seats (3*3) and use a loop to put in the letters X and O so it looks like this
XOX
OXO
XOX
Then use a loop to print it on the screen.

Can someone show me how this is done?

Take a look at this tutorial that should help explain a multi dimensional array that you will need. http://www.cplusplus.com/doc/oldtutorial/arrays/

Other than that you will just need loops and if/else statements to create the logic to fill the array.
i would probably make 2 loops to go through the array (x and y)
then take the sum of both the loop counters and mod it by 2 (to check if its even or odd)
if its equal to 0 (its even) then set the value to X else (its odd) set the value to O

 ``12345678910111213141516171819202122`` ``````int main(int argc, char** argv) { //decalre array char array[3][3]; //initialize array for(int y = 0; y < 3;y++) for(int x = 0; x < 3; x++) if((x+y)%2 == 0) array[y][x] = 'X'; else array[y][x] = 'O'; //prints array for(int y = 0; y < 3; y++) { for(int x = 0; x < 3; x++) printf("%c ",array[y][x]); printf("\n"); } return 0; }``````

if you want you can also easily calculate the number of rows and columns in the array and then use that in the loops to make it possible to scale the array to any size you desire.

 ``12345678910111213141516171819202122232425`` ``````//decalre array char array[5][10]; //calculate rows & columns int rows = sizeof(array) / sizeof(array[0]); int columns = sizeof(array[0]) / sizeof(char); //initialize array for(int y = 0; y < rows;y++) for(int x = 0; x < columns; x++) if((x+y)%2 == 0) array[y][x] = 'X'; else array[y][x] = 'O'; //prints array for(int y = 0; y < rows; y++) { for(int x = 0; x < columns; x++) printf("%c ",array[y][x]); printf("\n"); } printf("rows: %d \n",rows); printf("columns: %d \n",columns);``````
Last edited on
Thank you stav but I get an error. The error:'pintf' was not declared in this scope.
Sorry if there is an obvious fix, I'm new.
Last edited on
Compiles fine for my compiler GNU GCC, Im gonna assume you didn't

 `` `` ``#include // for use of printf(); function ``

 ``12345678910111213141516171819202122232425262728293031323334`` `````` #include #include int main() { // initialize array char array[5][10]; //calculate rows & columns int rows = sizeof(array) / sizeof(array[0]); int columns = sizeof(array[0]) / sizeof(char); //initialize array for(int y = 0; y < rows;y++) for(int x = 0; x < columns; x++) if((x+y)%2 == 0) array[y][x] = 'X'; else array[y][x] = 'O'; //prints array for(int y = 0; y < rows; y++) { for(int x = 0; x < columns; x++) printf("%c ",array[y][x]); printf("\n"); } printf("rows: %d \n",rows); printf("columns: %d \n",columns); return 0; }``````

If that didn't work try repasting the code I gave you! :)

Also he's using C++ 98/03 so take the stuff from the parameters of main() away if you're using C++11 (If you're new to C++ I recommend using C++11 (and 14) instead :) should be no reason to use 98 anymore especially not come C++20!
Last edited on
Topic archived. No new replies allowed.