## Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

• Help others & share knowledge
• Earn cash & points
Solved

# Spacing out a 2 dimensional array's output

Posted on 2004-09-30
260 Views
Hi, thanks in advance for your help. I rememer doing this with a different programming language, but forgot how. Can someone tell me how I can make the output of the following array so all the rows and columns line up like a matrix (or a box if you prefer)? Thanks again.

int main()

{
int arr[3][5]=
{
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};

for (int i=0;i<3;i++)
{
cout << "[";
for (int j=0;j<5;j++)
cout << arr[i][j]<< " ";
cout << "]" << endl;
}

getch();
}
0

LVL 11

Assisted Solution

avizit earned 40 total points
ID: 12196938
try changing

cout << arr[i][j]<< " ";

to

cout << arr[i][j]<< "\t";

i.e replace the space after the element by a 'tab' , is this the result what you wanted ?
0

LVL 55

Accepted Solution

Jaime Olivares earned 60 total points
ID: 12196954
You can use setw() formatting function:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
int arr[3][5]=
{
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};

for (int i=0;i<3;i++)
{
cout << "[";
for (int j=0;j<5;j++)
cout << setw(4) << arr[i][j]<< " ";
cout << "]" << endl;
}

getch();
}
0

Author Comment

ID: 12196991
Thanks, I tried both solutions and they both are good solutions. Take care.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.