Arrays rows/columns

I need the problem to show 3 rows and 5 columns. The output should be like this:

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3

My output is just a list. How do I modify it so it can be like the example above?

Here's the actual question:

Declare a two-dimensional array of integers named 'a', with 3 rows and 5 columns. Set the first row to all 1s, set the second row to all 2s, and the third row to all 3s. Lastly, display the two-dimensional array (in typical matrix fashion, rows/columns).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;
int main ()

{
   int a[3][5] = { {1,1,1,1,1}, {2,2,2,2,2}, {3,3,3,3,3} };
   for ( int i = 0; i < 3; i++ )
      for ( int j = 0; j < 5; j++ )

      {
         cout << a[i][j]<< endl;
      }
   return 0;
}
What do you do on line 12?
Display the array but it displays like this:

1
1
1
1
1
2
2
2
2
2
3
3
3
3
3

I need it like this:

1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Line 12 does not display an array. It does two things:
1. Display one element from an array: << a[i][j]
2. Display a newline character: << endl

If you don't want the newline, then don't write it to stream.
If I erase endl, it will display it as 111112222233333 which isn't what I wanted. I'm trying to make it look like
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
To give you an idea:
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
/*
Declare a two-dimensional array of integers named 'a', 
with 3 rows and 5 columns. Set the first row to all 1s, 
set the second row to all 2s, and the third row to all 3s. 
Lastly, display the two-dimensional array (in typical 
matrix fashion, rows/columns).
*/

#include <iostream>


int main()
{
    int a[3][5] {{1, 1, 1, 1, 1}, { 2, 2, 2, 2, 2 }, 
    { 3, 3, 3, 3, 3 } };

    for (int cols = 0; cols < 5; cols++)
    {
	   for (int rows = 0; rows < 3; rows++)
	   {
		  std::cout << " " << a[rows][cols];
	   }

	   std::cout << std::endl;;
    }

    return 0;
}
Sidenote:
I need the problem to show 3 rows and 5 columns. The output should be like this:
1 2 3
1 2 3

You do correctly create int a[3][5] for "3 rows and 5 columns".

However, a row is horizontal and a column is vertical. Therefore, the expected output of a is:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

If you really want to print in the order that you have shown in your example, then you want to display column N of the table as row N of the output.

If I erase endl, it will display it as 1111

Yes. When you write only the values and nothing else, only the values do show. When you write newline after each value, each value is on different line.

chicofeo's code writes a whitespace before every value and a newline only after the inner loop.
Topic archived. No new replies allowed.