7 Replies - 349 Views - Last Post: 20 February 2013 - 09:52 PM Rate Topic: ***** 1 Votes

#1 AwesomeProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 13

Multidimensional Arrays

Posted 20 February 2013 - 12:24 PM

Hello, I have recently been trying to make a new program making multidimensional arrays! Here is my code:
 #include <iostream>

using namespace std;

int main()
{

    const int MAX_ROWS = 3;
    const int MAX_COLUMNS = 3;
    
    char table [MAX_ROWS][MAX_COLUMNS] = {
        {  {'O', 'X', 'O'},
            {'O', 'X', 'X'},
            {'X', 'O', 'X'} }
    };
    
    for (int i = 0;i<MAX_ROWS;i++){
        for (int j = 0; j<MAX_COLUMNS;j++) {
            cout << table[i][j];
        }
        cout << endl;
    }
    
    return 0;
}



I get the error: Excess elements in scalar initializer on line 12 on the X.
Could anybody help?

Is This A Good Question/Topic? 0
  • +

Replies To: Multidimensional Arrays

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,771
  • Joined: 25-December 09

Re: Multidimensional Arrays

Posted 20 February 2013 - 12:30 PM

It looks like maybe you have too many braces in your initializer.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 730
  • Joined: 27-June 09

Re: Multidimensional Arrays

Posted 20 February 2013 - 12:30 PM

You have an extra set of brackets.

    char table [MAX_ROWS][MAX_COLUMNS] = {
        {  {'O', 'X', 'O'},
            {'O', 'X', 'X'},
            {'X', 'O', 'X'} }
    };
    //should be 
    char table [MAX_ROWS][MAX_COLUMNS] = {
        {'O', 'X', 'O'},
            {'O', 'X', 'X'},
            {'X', 'O', 'X'}
    };

Was This Post Helpful? 0
  • +
  • -

#4 AwesomeProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 13

Re: Multidimensional Arrays

Posted 20 February 2013 - 12:41 PM

Ok thanks that solve that problem, however, this code:

#include <iostream>

using namespace std;

int main()
{

    const int MAX_ROWS = 3;
    const int MAX_COLUMNS = 3;
    
    char table [MAX_ROWS][MAX_COLUMNS] = {
        {  'O', 'X', 'O',
            'O', 'X', 'X',
            'X', 'O', 'X' }
    };
    
    for (int i = 0;i<MAX_ROWS;i++){
        for (int j = 0; j<MAX_COLUMNS;j++) {
            cout << table[i][j];
        }
        cout << endl;
    }
    
    return 0;
}


gets an error message: Excess elements in array initializer on line 13 on the O. Could anybody help again?

Oops sorry i should of deleted that 4th post.

Thanks for quick reply! :bigsmile:

Sorry for post 4! I didn't read the posts fully :unsure: Thanks!
:turned:
Was This Post Helpful? 0
  • +
  • -

#5 ipoood  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-March 12

Re: Multidimensional Arrays

Posted 20 February 2013 - 04:46 PM

You don't have enough brackets now.

char table [MAX_ROWS][MAX_COLUMNS] = {  

         {  'O', 'X', 'O',  

             'O', 'X', 'X',  

             'X', 'O', 'X' }  

     }; 



should be like this:

char table[MAX_ROWS][MAX_COLUMNS] = {
{'O', 'X', 'O'},

{'O', 'X', 'X'},  

{'X', 'O', 'X'}

} 



Each row needs to have its own set of brackets with a comma in between each one.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Multidimensional Arrays

Posted 20 February 2013 - 08:11 PM

Quote

Each row needs to have its own set of brackets with a comma in between each one.

Not every row needs it's own set of brackets, it is sufficient however. Since a 2D array is continuously addressed in memory, you can omit the row brackets.

char table[MAX_ROWS][MAX_COLUMNS] = {
'O', 'X', 'O',

'O', 'X', 'X',  

'X', 'O', 'X'
} 


Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,771
  • Joined: 25-December 09

Re: Multidimensional Arrays

Posted 20 February 2013 - 09:22 PM

Actually if I try to leave out the row braces I get these warnings:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|
main.cpp|13|warning: missing braces around initializer for ‘char [3]’ [-Wmissing-braces]|


Jim
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Multidimensional Arrays

Posted 20 February 2013 - 09:52 PM

Quote

Actually if I try to leave out the row braces I get these warnings:

I do if I compile with -Wall. I also get this warning with statements like
int arr[3][3] = {0};



Statements like this do not go against the standard which states that the left out aggregate shall be initialized implicitly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1