#Output problem idk why its giving me weird output
10 messages · Page 1 of 1 (latest)
`#include<iostream>
#include<string>
int main(){
std::string z[3][2][2]={
{{"a","b"},{"c","d"}},
{{"e","f"},{"g","h"}},
{{"i","h"},{"j","k"}},
};
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
std::cout << z[3][2][2] <<" ";
}
}
}
std::cout << std::endl;
return 0;
}
`
Since the dimensions of the array are [3][2][2], the largest index possible is [2][1][1] (since arrays are 0-indexed).
z[3][2][2] is outside the bounds of the array and is undefined behavior
tell me how to Fix
Well, what are you trying to do?
print those Multidimensional Arrays with loops
Look at the indices you're using to index the array
If you want to iterate over the array, would you use a constant?
didn't understand
Look in the innermost loop
std::cout << z[3][2][2] <<" ";
The array is being indexed by constant values (3, 2 and 2)
This means every time the loop is iterated, the array is indexed by the same values
You've defined the variables i, j and k