#include <iostream>
using namespace std;
int main()
{
int i, j, k, m, dim1, dim2, dim3;cout << "Enter array dimensions: ";
cin >> dim1 >> dim2 >> dim3;
//int (*pa)[dim2][dim3] = new int[dim1][dim2][dim3]; // error C2057 [old code segment]
/*new rectified lines of code start*/
int ***pa = new int **[dim1];
// initialize array
for (i = 0; i < dim1; i++) {
pa[i]= new int *[dim2];
}
for (i = 0; i < dim1; i++) {
for (j = 0; j < dim2; j++) {
pa[i][j]= new int[dim3];
}
}
/*new rectified lines of code end*/
m = 0;
for (i = 0; i < dim1; i++)
for (j = 0; j < dim2; j++)
for (k = 0; k < dim3; k++)
pa[i][j][k] = m++;
cout << "Value of element (0,0,0): " << pa[0][0][0] << endl;
cout << "Value of element (1,2,1): " << pa[1][2][1] << endl; //[dim1,dim2,dim3 expected values- >= 1,2,1]
cout << "Value of last element: " << pa[dim1 - 1][dim2 - 1][dim3 - 1];
cout << "\n\n";
delete[] pa;
// Wait for the user to read the output on the console
system("PAUSE");return 0;
}
Enter your message below
Sign in or Join us (it's free).