You are here:

C++/Problem getting the desire output

Advertisement


Question

Hi Zlatko
      Thanks for your last help. Now my problem is how to get the desire output in a table of numbers. This is the
program I have created:

#include <iostream>
#include <iomanip>

int rows=5;
int columns=5;

using namespace std;

int main()
{
   cout<<endl;
   int array[5][5]={{2,13,23,45,50},
                           {5,12,34,36,51},
                           {2,23,25,46,54},
                           {2,7,35,39,48},
                           {3,5,26,39,49}};

   cout<<setw(72)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
   cout<<setw(75)<<"--------------------------------------------------\n";

   for(int i=0; i< rows; i++)
   {
       cout<<setw(30)<< i <<")";
     for(int j=0; j< columns; j++)
       {
           cout<< setw(8)<< array[i][j];
       }
       cout<<endl;
   }
   cout<<endl<<endl;

      cout<<setw(69)<<" DISTRIBUTION'S NUMBERS IN THE RANGE  BETWEEN\n";
  cout<<setw(73)<<"=============================================== \n\n";
  cout<<setw(54)<<" (1 - 19)             (20 - 39) ";
  cout<<"                (40 - 56)\n";
  cout<<setw(60)<<"------------------------------------------------";
  cout<<"----------------------\n";
   for(int i = 0; i < rows; i++)
    {
        cout<<setw(15)<< i <<")";
        cout<<" ";
        for(int j = 0; j < columns; j++)
           if(array[i][j]>=1 && array[i][j]<=19)
             {
              cout<<setw(4)<< array[i][j];
             }
          cout<<setw(20)<<" ";
            for(int j = 0; j < columns;  j++)
        if(array[i][j]>=20 && array[i][j]<=39)
         {
            cout<<setw(4)<< array[i][j];
         }
          cout<<setw(23)<<" ";
           for(int j = 0; j < columns; j++)
        if(array[i][j]>=40 && array[i][j]<=56)
         {
            cout<<setw(4)<< array[i][j];
         }
         cout<<endl;
    }
    cout<<endl;
   return 0;
}
                      This is the output of this program:
   
                                           ROWS     (0)     (1)     (2)     (3)     (4)
                                          --------------------------------------------------
                                                  0)       2      13      23      45      50
                                                  1)       5      12      34      36      51
                                                  2)       2      23      25      46      54
                                                  3)       2       7      35      39      48
                                                  4)       3       5      26      39      49


                          DISTRIBUTION'S NUMBERS IN THE RANGE BETWEEN
                      ===============================================

                                      (1 - 19)             (20 - 39)                 (40 - 56)
                              ----------------------------------------------------------------------
                                 0)    2  13                      23                         45  50
                                 1)    5  12                      34  36                         51
                                 2)    2                      23  25                         46  54
                                 3)    2   7                      35  39                         48
                                 4)    3   5                      26  39                         49
      
          What I really  want is that all the columns start at the same point like the column (1 - 19)
does it, for example this is my desire output:

                          DISTRIBUTION'S NUMBERS IN THE RANGE BETWEEN
                      ===============================================

                                      (1 - 19)                (20 - 39)                    (40 - 56)
                              ----------------------------------------------------------------------
                                 0)    2  13                   23                             45  50
                                 1)    5  12                   34  36                       51
                                 2)    2                         23  25                       46  54
                                 3)    2   7                    35  39                       48
                                 4)    3   5                    26  39                       49

        Thanks again.  

Answer
Hello Raul.

You need to print the correct number of spaces based on how long the text is in a particular cell. Use a string stream to format a particular cell and then print out the string stream string all at once. Use the length of the string stream string to calculate how many spaces to print out before the next cell. The code below shows how to do it. Beware that the code below will not handle the case when there are too many numbers to fit into a cell. You will have to decide what to do in that case.

#include <iostream>
#include <sstream>
#include <iomanip>

int rows=5;
int columns=5;

using namespace std;

int main()
{
   cout<<endl;
   int array[5][5]={{2,13,23,45,50},
   {5,12,34,36,51},
   {2,23,25,46,54},
   {2,7,35,39,48},
   {3,5,26,39,49}};

   cout<<setw(72)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
   cout<<setw(75)<<"--------------------------------------------------\n";

   for(int i=0; i< rows; i++)
   {
       cout<<setw(30)<< i <<")";
       for(int j=0; j< columns; j++)
       {
           cout<< setw(8)<< array[i][j];
       }
       cout<<endl;
   }
   cout<<endl<<endl;

   cout<<setw(69)<<" DISTRIBUTION'S NUMBERS IN THE RANGE  BETWEEN\n";
   cout<<setw(73)<<"=============================================== \n\n";
   cout<<setw(54)<<" (1 - 19)             (20 - 39) ";
   cout<<"                (40 - 56)\n";
   cout<<setw(60)<<"------------------------------------------------";
   cout<<"----------------------\n";
   for(int i = 0; i < rows; i++)
   {
       cout<<setw(19)<< i <<")";
       cout<<" ";

       stringstream ss;
       for(int j = 0; j < columns; j++)
       {
           if(array[i][j]>=1 && array[i][j]<=19)
           {
               ss<<setw(4)<< array[i][j];
           }
       }
       cout << ss.str();
       cout<<setw(22 - ss.str().length())<<" "; // print spaces based on remaining length
       ss.str(""); // clear the string stream contents.

       for(int j = 0; j < columns;  j++)
       {
           if(array[i][j]>=20 && array[i][j]<=39)
           {
               ss<<setw(4)<< array[i][j];
           }
       }
       cout << ss.str();
       cout<<setw(26 - ss.str().length())<<" ";
       ss.str("");

       for(int j = 0; j < columns; j++)
       {
           if(array[i][j]>=40 && array[i][j]<=56)
           {
               ss<<setw(4)<< array[i][j];
           }
       }
       cout << ss.str();
       cout<<endl;
   }
   cout<<endl;
   return 0;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.