Simple 3D Array C++

后端 未结 5 1590
深忆病人
深忆病人 2021-02-05 13:57

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 &l         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 14:14

    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 
    
    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;
    }
    

提交回复
热议问题