Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Spacing out a 2 dimensional array's output

Posted on 2004-09-30
3
Medium Priority
?
264 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

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

636 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