Printing Elements of 3D Array of pointers
Feb 8, 2016 at 1:11am UTC
Here is my full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
int ***my3DArray;
int length, width, height;
bool doAgain = true ;
string answer("" );
string yes("y" );
do {
cout << "Enter length: " ;
cin >> length;
cout << "\nEnter width: " ;
cin >> width;
cout << "\nEnter height: " ;
cin >> height;
srand((unsigned )time(NULL));
my3DArray = new int **[length];
for (int i= 0; i < length; ++i){
my3DArray[i] = new int *[width];
for (int j = 0; j< width; ++j){
my3DArray[i][j] = new int [height];
for (int k = 0; k < height; ++k){
my3DArray[i][j][k] = rand()%100;
cout << my3DArray[i][j][k] << " " ;
do {
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: " ;
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)" ;
cin >> answer;
if (answer.compare(yes) == 0)
doAgain = true ;
else doAgain = false ;
}
while (doAgain == true );
}
cout << endl;
}
cout << endl;
}
for (int i = 0; i < length; i++){
for (int j = 0; j < width; j++){
delete []my3DArray[i][j];
}
delete []my3DArray[i];
}
delete []my3DArray;
my3DArray = NULL;
cout << "Again? (y, n)" ;
cin >> answer;
if (answer.compare(yes) == 0)
doAgain = true ;
else doAgain = false ;
}
while (doAgain == true );
}
This prints a 3D array of integers, I need to write a function called tick that returns the element of user specified coordinates i j k. That part of the code is this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
do {
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: " ;
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)" ;
cin >> answer;
if (answer.compare(yes) == 0)
doAgain = true ;
else doAgain = false ;
}
while (doAgain == true );
when I have this in my code, it doesnt print out the 3D array and I get a segmentation fault when it's supposed to print the element. Any Ideas? Thanks in advance
Topic archived. No new replies allowed.