Solved

Spacing out a 2 dimensional array's output

Posted on 2004-09-30
3
261 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error C2678: binary '!=': no operator found... 4 65
Issues with C++ Class 19 108
How to Correctly derive class from CWinThread in MFC 4 89
Need some help with design 17 21
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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