Here is the input file www.dropbox.com/s/qsb3g5h19fpzhyk/input.txt?dl=0 Here is
ID: 3564783 • Letter: H
Question
Here is the input file
www.dropbox.com/s/qsb3g5h19fpzhyk/input.txt?dl=0
Here is asst4.cpp that has everything done, you just have to basically fill in the blanks
www.dropbox.com/s/do0nz2z6wqn8qgd/asst4.cpp?dl=0
In this assignment, you will create a dynamic 3 dimensional array using a pointer. Pointer
arithmetic will be done to access a specific element of the 3 dimensional array. In your file, the
following will be done:
1. A pointer will be declared and it will hold the base address of the 3 dimensional array
2. Dynamic memory will be allocated to set up the array, there will be a few array of
pointers in which at the end, the last array created will hold integers. An array with
dimensions 3x3x3 will be created.
3. The file called input.txt will be in the Assignment 4 section of the forum which will
contain 27 distinct integers in which the values will be read using filestreams and will
populate the array.
4. Pointer arithmetic will be done with the pointer that points to the base address of the
array and then the value that it's pointing to will be printed to the screen.
5. The address of the pointer will be reset to the base address of the array and then the
value of the array will be printed at the appropriate index of the array which should
match from the value printed in part 4.
6. Then the array must be deallocated properly.
So you'll have a pointer:
int *** array3D;
And by using the new keyword appropriately, you will create the 3D array. After the array is
allocated and is populated with contents of the input file, you will perform pointer arithmetic.
For example:
array3D = array3D + 1;
will move the address of what array3D is pointing to. So we can access different elements of
the array without array3D[][][] with the respective indices notation. Don't use:
array3D++;
I always kept getting segmentation faults when I tried that! After the array is allocated
properly, ***array3D should be the same value as array3D[0][0][0]. Let's say you wanted the
contents of array3D[1][1][1], would would you have to do to array3D pointer in order for
***array3D to point to the same memory location?
You will perform pointer arithmetic to output these values: array3D[2][1][0], array3D[2][2][1],
array3D[2][1][2], array3D[2][2][2], and array[0][1][2]. The output is on the following page../a.out
Reference to array3D[2][1][0]
24
24
Reference to array3D[2][2][1]
7
7
Reference to array3D[2][1][2]
9
9
Reference to array3D[2][2][2]
35
35
Reference to array3D[0][1][2]
36
36
So ***array3D and array3D[][][] with the appropriate indices will be printed and
they both need to be the same value.
Explanation / Answer
//Sample output
utkarsh@utkarsh-ThinkPad-E520:~/chegg$ g++ dynamic3DArray.cpp -o dynamic3DArray
utkarsh@utkarsh-ThinkPad-E520:~/chegg$ ./dynamic3DArray
Printing out the contents of the Array using indices :
34 37 28
16 44 36
43 50 22
13 41 10
14 27 23
12 19 18
30 33 31
24 3 9
20 7 35
Printing out the contents of the Array using pointers :
34 37 28
16 44 36
43 50 22
13 41 10
14 27 23
12 19 18
30 33 31
24 3 9
20 7 35
//Code
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream fin("input.txt");
int i,j,k;
int*** array3D;
array3D = new int**[3];//array3D to hold address of 3 2D arrays
for(i=0;i<3;i++){
*(array3D+i) = new int*[3];//Each 2D array to hold 3 1D Arrays
for(j=0;j<3;j++){
*(*(array3D+i) + j) = new int[3];//Finally each 1D array to hold 3 integers
for(k=0;k<3;k++){//Populate the Array
fin>>(*(*(*(array3D+i) + j) + k));
}
}
}
cout<<"Printing out the contents of the Array using indices : "<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
cout<<array3D[i][j][k]<<" ";
}
cout<<endl;
}
cout<<endl;
}
cout<<"Printing out the contents of the Array using pointers : "<<endl;
//Print and deallocate the array
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
cout<<(*(*(*(array3D+i) + j) + k))<<" ";
}
cout<<endl;
delete *(*(array3D+i) + j);
}
delete *(array3D+i);
cout<<endl;
}
delete array3D;
}