14

I am a novice in C++ and I am trying to create a simple static 3 Dimensional Array and then print it out in console.

Here is my current code:

#include <iostream>
using namespace std;

int main()
{
  const int MAX_ROW = 2;
  const int MAX_COL = 2;
  const int MAX_HEIGHT = 2;

  int MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] = { {1,1},
                           {2,10},
                           {3,15},
                           {4,20},
                           {5,25},
                           {6,30},
                           {7,35},
                           {8,40} };

  for(int Row = 0; Row < MAX_ROW; ++Row)
  {
   for(int Col =0; Col < MAX_COL; ++Col)
   {
    for(int Height = 0; Height < MAX_HEIGHT; ++Height)
     {
      cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] << endl;
     }
    }
   }



  return 0;
}

When I compile the compiler notifies me stating "error: too many initializers for ‘int [2][2][2]"

Other questions have used pointers which I am not familiar with.

Thank you in advance!

Edit: The syntax is wrong so I have corrected it with the correct corresponding code as answered below. Now in the output of the program each array space is 32767. A full integer space instead of the assigned values. Can anybody address this in their answer? I have not changed any code except my initialisation of the array.

4
  • You are allocating 3X3 array with row length of 2. But you are initializing with only two dimensions with 8 elements in each dimension. Add a 3rd dimension and either use less elements per dimension or increase the size of each dimension Commented Oct 10, 2013 at 7:46
  • 4
    A big +1 for posting a concise, complete example program - this is so rare! Commented Oct 10, 2013 at 7:47
  • Nice, well-posed question. But, the normal way to do this in C++ is to allocate a contiguous memory block and overload operator () (unsigned,unsigned,unsigned) for element access (const and non-const versions). Commented Oct 10, 2013 at 8:05
  • Don't forget about use delete after your work, look at this Commented Dec 25, 2016 at 17:51

5 Answers 5

9

Your syntax is wrong.

int a[2][2][3] = {     // Initialize entire variable
  {                    //   1 of 2 (leftmost array)
    { 1, 2, 3 },       //     1 of 2 (inner array)
    { 4, 5, 6 },       //     2 of 2 (inner array)
  },

  {                    // 2 of 2 (leftmost array)
    { 7, 8, 9 },       //     1 of 2 (inner array)
    { 10, 11, 12 },    //     2 of 2 (inner array)
  },
}

What you've shown would be an int [8][2].

Sign up to request clarification or add additional context in comments.

1 Comment

This isn't static now is it? Because there isn't a defined max amount of each array using a const int.
6

Change the code as per following. You can see there are 2 groups containing two tuples each having two elements in it.

 int MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] = { 
                                               { {1,1},{2,10} }, 
                                               { {4,20},{5,25} } 
                                             };

Have a look in following example to make it more clear

  int arr[2][3][4] = { 
                       { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} },
                       { {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4} } 
                     };

As you can see, there are two groups, each containing three groups of 4 numbers.

4 Comments

Can you explain as to which integers are the 2nd and 3rd dimension?
Ok I understand now. But now when I run the program the output is given as a full int showing that each space in the array is equal to 32767! I compile using from the command line using gcc with the command gcc -l stdc++ Arrays-v3.cpp
What do you mean by space equal to 32767.?
Instead of printing to the command line the assigned values in the program it prints 32767 as every value in the array. I am not sure why.
0

Your declare a 2x2x2 array, but defining it as a 2x8 array.

Also, when you print the content of your array, you use MAX_* as indexes instead of your loop variables.

#include <iostream>

int main()
{
    const int MAX_ROW = 2;
    const int MAX_COL = 2;
    const int MAX_HEIGHT = 2;

    int MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] = {
        {
            {1,1}, {1,-1}
        },
        {
            {2,10}, {2,-10}
        }
    };

    for(int Row = 0; Row < MAX_ROW; ++Row)
        for(int Col =0; Col < MAX_COL; ++Col)
            for(int Height = 0; Height < MAX_HEIGHT; ++Height)
                std::cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] << std::endl;


  return 0;
}

Comments

0

you array MyArray[MAX_ROW][MAX_COL][MAX_HEIGHT] only can hold 2*2*2=8 elements, but

{ {1,1},
                           {2,10},
                           {3,15},
                           {4,20},
                           {5,25},
                           {6,30},
                           {7,35},
                           {8,40} };

has 16 elements. so there are too many initializers

Comments

0

Apart from wrong array initialization as others have pointed out you also have an error in printing. You always print the same element that doesn't even exist (which is undefined behaviour).

cout << "Integer["<< Row << "][" << Col << "][" << Height << "] = " << MyArray[Row][Col][Height] << endl;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.