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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.

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
//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 <stdio.h> // for use of printf(); function  


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
 
#include <iostream> 
#include <stdio.h>

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.