Start New Discussion within our Software Development Community

I have a practise problem to write a function that takes 3 dim and dynamicly allocates 3d array with those values and then (PART THAT I DONT GET) : fills the 3-dimensional array with multiplication tables. What am I even supossed to do ?? Here's my code :

int main()
{
  int dimX = 0;
  int dimY = 0;
  int dimZ = 0;

      cout << "Enter a lenght : ";
      cin >> dimX;
      cout << "Enter widht : ";
      cin >> dimY;
      cout << "Enter a height : ";
      cin >> dimZ;

      John_is_3D(dimX,dimY,dimZ);
  }


  void John_is_3D(int lenght,int width,int height) {

        int val=0;
        int *** John;
        John = new int**[lenght];

        for(int x = 0;x < lenght;x++){

                John[x] = new int*[width];

            for(int y = 0;y < width;y++){

                   John[x][y] = new int[height];

               for(int z = 0;z < height;z++){

                       John[x][y][z] = 
               }
            }


        }

              for(int x = 0;x < lenght;x++){

                for(int y = 0;y < width;y++){

                  for(int z = 0;z < height;z++){

                     cout<< John[x][y][z] << "   ";
              }

            }
                 cout << endl<<endl;
        }

  }

You've sort of started along the right idea. Use 3 nested loops and add the multiplication product to the array in the innermost loop:

for(int x = 0;x < lenght;x++)
    for(int y = 0;y < width;y++)
        for(int z = 0;z < height;z++)
            John[x][y][z] = x*y*z;
This question has already been answered. Start a new discussion instead.