need help giving 2D array an initial value

the tutorial shows that something like this
int billy [] = {16, 2, 77, 40, 12071};
is possible.

Is it possible to give a 2D array an initial value?
Yes, here is an example.

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

int main()
{
 //const int WIDTH = 5
 //const int HEIGHT = 10
 int Table[3][4] = {{8, 2, 6, 5},
                    {6, 3, 1, 0},
                    {8, 7, 9, 6}};

for (int i = 0; i < 3; i++)
{
  for (int j = 0;j < 4; j++)
  {
    cout << Table[i][j] << ' ';
  }
  cout << endl;
}

return 0;
}
Last edited on
ignore those comments, those are leftovers
1
2
3
4
5
6
7
8
9
10
11
int main()
{
int gr[9][9]=  {{6,0,3,0,2,0,0,9,0},
		{0,0,0 0,5,0,0,8,0},
		{0,2,0,4,0,7,0,0,1},
	        {0,0,6,0,1,4,3,0,0},
	        {0,0,0,0,8,0,0,5,6},
	        {0,4,0,6,0,3,2,0,0},
	        {8,0,0,2,0,0,0,0,7},
	        {0,1,0,0,7,5,8,0,0},
	        {0,3,0,0,0,6,1,0,5}};


I'm trying to give this 2D array initial values, but I keep on getting errors.
You are missing a comma on the second row
why do you initialize array like this?
it's far more simple to make some function which will initialize it the same way so you can reinitialize more times.
damn it. thanks Ron. Didn't notice that missing comma. Also codekiddy, i did that later before this thread got that reply days later.
Topic archived. No new replies allowed.