Solved

Spacing out a 2 dimensional array's output

Posted on 2004-09-30
3
258 Views
Last Modified: 2010-05-19
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
Comment
Question by:frogpad
3 Comments
 
LVL 11

Assisted Solution

by:avizit
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

by:
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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does std::atomic give me? 7 114
is twain_32.dll cmpatible with windows 10 ? 10 127
I could not build boost code, 10 79
show out valin of json for debugging in visaul c++ 1 16
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now