initz dat 2d array

Basically I am having a problem initializing a 2d array.

1
2
3
4
int compare [][] = { { 4, 9, 2},
		     {3, 5, 7},
                     {8, 1, 6} }; 
  


However, I get the error array may not have an element of this type. What am I missing here?
When initializing an array, you need to set the number of indexes. I'm sure this code will work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int compare[3][3]= {{4,9,2},
                        {3,5,7},
                        {8,1,6}};
    //output array values
    for (int y=0; y<3; y++)
    {
        for (int x=0; x<3; x++)
            cout<<compare[y][x]<<" ";
        cout<<endl;
    }
    return 0;
}

4 9 2
3 5 7
8 1 6

Or, you could initialize your array to all 0's, and then populate it. It really depends on what you are trying to do. Still, this should get you moving in the right direction once you understand how initialization works.
int compare[3][3]={};//initialize with zeroes

I'm unsure how familiar you may be with vectors, but the primary difference between vectors and arrays is that array sizes need to be known at the time of declaration, in contrast to vectors, where the size can be unknown.
Last edited on
Yea, I know what vectors are. It just I have coded in well at least 1/2 year so I'm just re-familiarizing myself with small basic stuff. But thanks your answer was on the spot.
Sure thing. I'm still learning about vectors myself, although I understand their basic usage.
Topic archived. No new replies allowed.