Print 3D Array Horizontally

Hey guys. I have been sitting here trying to figure out how to print this 3D array out horizontally instead of vertically but I am stuck. I've used this website for many of my labs and most of the programming you guys do looks very advanced so don't mind my very beginner level of C++ programming. I'm just trying to use what I know.


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
 #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
    
    srand(time(NULL));
    int fun[3][3][3];
    
    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                int x = rand() % 10;
                fun[row][column][length] = x;
            }
        }
    }
    
    
    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                cout << fun[row][column][length] << " ";
            } cout << endl;
        } cout << endl;
    } cout << endl;
}


With this code, I have the three 3x3 arrays printing one on top of the other vertically. What I need is for them to be printed horizontally. If anyone has any suggestions that would be greatly appreciated!
youll have to print the first row of each array then end line, then the second row of each array then endline, then the third.

111 222 333 \n
111 222 333 \n
111 222 333 \n right?

its actually easier.

its a 2 deep loop. I think this is right. Fill it out and see if I got it right.
remember that integer division rounds down, so 0/3 is 0, 1/3 is 0, 2/3 is 0, 3/3 is 1, 4/3 is 1, 5/3 is 1, ... etc. And modulo repeats, 0%3 is 0, 1%3 is 1, giving 012 012 012

for(... rows of first matrix)
for(t = 0; z = 0; c = 0; c < matrixcols*3; c++)
{
t = c/3;
z = c%3;
if(t == 0)
//first matrix[z]

if(t == 1)
//2nd matrix[z]...

if(t == 2)
//3rd matrix
}



Last edited on
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
    
    srand(time(NULL));
    int fun[3][3][3];
    
    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                int x = rand() % 10;
                fun[row][column][length] = x;
            }
        }
    }
    
    

    for (int column=0; column<3; column++){                     // reverse order
       for ( int row=0; row<3; row++){                          //
            for (int length=0; length<3; length++){
                cout << fun[row][column][length] << " ";
            } cout << "     ";                                  // blanks between squares
       } cout << endl;
    } cout << endl;

}

8 7 4      4 0 7      0 1 1      
6 2 0      4 8 5      3 5 1      
4 0 0      7 5 1      6 0 3
Last edited on
correct just like that. But how would I go about doing so? BTW I realized I was very redundant and shortened the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main() {
    
    srand(time(NULL));
    int fun[3][3][3];
    
    for ( int row=0; row<3; row++){
        for (int column=0; column<3; column++){
            for (int length=0; length<3; length++){
                int x = rand() % 10;
                fun[row][column][length] = x;
                cout << fun[row][column][length] << " ";
            } cout << endl;
        } cout << endl;
    } cout << endl;
}
If I understand you correctly, the key is to switch the position of the outer two loops.

1
2
3
4
5
6
7
8
9
for (int j = 0; j < 3; ++j) {
    for (int i = 0; i < 3; ++i) {
        for (int k = 0; k < 3; ++k) {
            std::cout << fun[i][j][k] << " ";
        }
        std::cout << " ";
    }
    std::cout << "\n";
}


Edit:
Looks like I was too slow. Nothing new to see here.
Last edited on
BTW I realized I was very redundant and shortened the code.


NO - that won't be correct and they won't be the same, because fun[][][] will be generated in the order that they run across the lines.

Your original splitting of generation and output was correct. Reversing the outer two loops will then give the same matrices.

(You might want to comment out srand() temporarily to see this.)
Topic archived. No new replies allowed.