printing 2D arrays

i've given an assignment that i should genereat a 2*3 array with a given values and i want to print out this array in a tabular structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <iostream>
#include <iomanip>
using namespace std;

void main()
{
	float AB[2][3] = {5.0 , 4.5 , 3.0 , -16.0 , -5.9 , 0.0};

	for (int row=0; row<2; row++)
		for (int col=0; col<3;col++)
			cout << AB[row][col] << endl;

	system ("pause");
}
Hi Shebbo Muhammed,
you could use setw(int) as in:
std::cout << std::setw(5);

before printing a value, in your case before:
cout << AB[row][col] << endl;

you could write like this:
1
2
3
4
5
6
7
8
9
for (int row=0; row<2; row++)
    {
		for (int col=0; col<3;col++)
        {
            cout << setw(5);
            cout << AB[row][col] << " ";
        }
        cout << endl;
    }
Last edited on
Topic archived. No new replies allowed.