arrays- not sure what to input in for loop, below is the question, how would i go about it

int arr [3] [3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};



Print the elements of arr in three ways:

1 2 3 4 5 6 7 8 9

1 4 7 2 5 8 3 6 9

9 8 7 6 5 4 3 2 1



Note: this does not involve three separate 2D arrays; there should be only one 2D array.

This sounds like a homework question, but I'll bite.

Imagine that the array looks like this:

1 2 3
4 5 6
7 8 9

The three ways they want you to display the contents are going from left to right and top to bottom, top to bottom and left to right, and right to left and bottom to top. These can easily be achieved using two nested for loops. Try doing it yourself and post what you get here.

Also, please use code tags ([code ][/code])
Last edited on

int main()
{
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
{


return 0;
}

Not sure what to do next and that code tag thing i dont understand how im suppose to do that, i think i did it by clicking the first option were it says Format but idk making sure if thats what you mean?
You need to output something with a printf or cout statement in the loop body.
Think about the array like this. The element array[p][q] is where lines p and q intersect. How can you move p and q iteratively, to print what you want?

   q
   |
p- 1 2 3
   4 5 6
   7 8 9
_____________________________
p = 0, q = 0, array[p][q] = 1


     q
     |
   1 2 3
   4 5 6
p- 7 8 9
_____________________________
p = 2, q = 1, array[p][q] = 8
Last edited on
how would i print a number in reverse order?
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
#include <iostream>
using namespace std;
        
int main()
{
        int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}};
        for(int i=0; i<3; i++)
           for(int j=0; j<3; j++)
           {
               cout<<arr[i][j]<<" ";
           }
               cout<<endl;
        for(int i=0; i<3; i++)
           for(int j=0; j<3; j++)
           {
               cout<<arr[j][i]<<" ";
           }
               cout<<endl;
        for(int i=0; i<3; i++)
         for(int j=0; j<3; j++)   
           {
                                    
           }
               cout<<endl;
return 0;   
}
        

I was able to get the first prints but the last one where 1 2 3 4 5 6 7 8 9, is reversed to 9 8 7 6 5 4 3 2 1, i wasnt able to do it.
haha nvm i got it, thanks
Topic archived. No new replies allowed.