?
Solved

Spacing out a 2 dimensional array's output

Posted on 2004-09-30
3
Medium Priority
?
263 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
[X]
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
  • Learn & ask questions
3 Comments
 
LVL 11

Assisted Solution

by:avizit
avizit earned 160 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 240 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

771 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