Im trying to store multiplication in a 3D array allocated through pointers (a practice problem in a book). I can get the 3D array but I cant store the multiplication in the pointer array (i guess you can say) in the for loop of the function "three dimensional array" any ideas why? When i comment out the whole for loop it works fine.
#include <iostream>
usingnamespace std;
/**
Function that makes a three dimensional array
with its argurment using a pointer and multiplies
numbers in a loop for its values and stores it
in the array of pointers
**/
void ***three_dimensional_array (int ***arg1)
{
for (int i = 0; i < 9; ++i)
{
arg1[i] = newint*[9];
}
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
for (int k = 0; k < 9; ++k)
{
arg1[i][j][k] = i * j * k; // stores multiplication in 3DI ARRAY
cout << arg1[i][j][k] << '\t';
}
}
}
delete[] arg1;
}
// call the function in main
int main()
{
int ***three_p_free_store = newint**[9];
three_dimensional_array(three_p_free_store);
}
Your array shall have i * j * k ( 9 * 9 * 9 ) elements.
You allocated only i * j ( 9 * 9 ) elements. The firs i ( 9 ) elements you allocated with the statement
int ***three_p_free_store = new int**[9];
and the next j ( 9 ) elements for each i you allocated with the statement
for (int i = 0; i < 9; ++i)
{
arg1[i] = new int*[9];
}
So now you need to allocate the next k ( 9 ) elements for each i * j.
What kind of book would that be? What i'd be hoping to see in a C++ book would be "here you have C-style arrays. They exist. Now let's move on to std::vector and std::array)". If you have 2 dimensional C style arrays in your code, something is fishy. If you get to 3, something is very, very wrong in your code.
Now let's move on to std::vector and std::array)".
Now it is very interesting to me. I would like to know did you use std::array with multidimensional arrrays?!:) Tell us how std::array is convient to use with multidimensional arrrays.:)
It is not a working with multidimensional array. It is a declaration from which it is not clear which subscriptor is the first, which is the second and so on. Show for example a construction with std::array that corresponds to alement record of a usual array as
sorry i couldnt reply sooner, the website was down. thanks for all your help. Vlad From Moscow, your idea worked and now the array is able to be used. Thank you.