Help with 2D array

Aug 1, 2015 at 1:09pm
I'm supposed to create a 8x8 board using 2D array. I have no clues on how to create but based on searching from the forum here, I managed to create one but I have no idea if I did it correctly. Can someone please help me check if I'm right.

1
2
3
4
5
6
7
8
9
10
11
  #include<iostream>
using namespace std;

void InitialiseBoard(char board[8][8]);

int main()
{
	char board[8][8];

return 0;
}


If this is correct, do I need to do a for loop to generate the board after this?
Aug 1, 2015 at 1:12pm
Hi,
look in the tutorial section at the top left of this page :+)
Aug 1, 2015 at 9:15pm
closed account (E0p9LyTq)
You might want to study up a bit on arrays.

http://www.cplusplus.com/doc/tutorial/arrays/
Aug 1, 2015 at 10:19pm
He's asking if he has done it right so far.

Answer: Yes. Good job.

Make sure to give your function a body and have fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void InitialiseBoard(char board[8][8])
{
  for (int row = 0; row < 8; row++)
  for (int col = 0; col < 8; col++)
    board[row][col] = ' ';
}

int main()
{
  char board[8][8];
  InitialiseBoard(board);


  return 0;
}

Other functions that operate on your array will work similarly.

Printing your gameboard will require some careful thinking. I recommend you get out a piece of graph paper and draw what you want it to look like, so you know what things go where, and then work on printing it, row by row.

Hope this helps.
Last edited on Aug 1, 2015 at 10:20pm
Aug 2, 2015 at 8:40am
Thanks for the help. How do I get started on printing the board? I have to insert 10 of each objects(apples,bananas etc.) across the board randomly but Im not sure how to get started.

This are my codes so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void InitialiseBoard(char board[10][10])
{
	for (int row = 0; row < 10; row++)
		for (int col = 0; col < 10; col++)
			board[row][col] = ' ';
}

int main()
{
	char board[10][10];
	InitialiseBoard(board);

	int apple = 10;
	int banana = 10;
	int milk = 10;
	int water = 10;

	return 0;
}
Aug 2, 2015 at 1:11pm
It is unclear exactly what the problem is?
I take it to say that there are ten lots of four items,
apple, banana, milk and water, I have labelled them A, B, M, and W.
And you want them randomly placed in a 10x10 array.
So you first select a random item,
rndItem = (rand()%4);
where A=0, B=1, M=2 and W=3
If there are any left of the chosen item you cross it off the list
apple = apple - 1 // remove an apple
You then randomly select an entry in the 10x10 array for an empty cell.

rndRow = (rand()%10);
rndCol = (rand()%10);
Is board[rndRow][rndCol] != ' '

And you fill in the found empty cell with a symbol for the item as I have done below.
You keep doing this until there are no more items left.


. . W W A . W . B .
. . B M A W . A M B
. W . . . . B B W .
. . . A . W A B . .
. . W B . . . . W M
W . M . . . . . .
B A A . . M . . W .
M . . . . B . . . B
. W . . A . M A W .
. . . . . . . A . .
Last edited on Aug 2, 2015 at 1:13pm
Topic archived. No new replies allowed.