Solved

Spacing out a 2 dimensional array's output

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

707 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

14 Experts available now in Live!

Get 1:1 Help Now