You are here:

C++/How to print results in a table

Advertisement


Question
Hello Zlatko, I checked when the user enters a size of 1 or 2 and it is working
the way I want. The output I mailed you is different from the output you get if
you run the program may be is that what make you think there is something
wrong if not, I do not know what you try to tell me, once again thanks

Answer
Ok, if you say your program is running correctly, I believe you. However I probably broke it because I changed your analysis loop to go forwards, not backwards. I'm sure you can fix it if it is broken. To print out in a table, I used stringstream objects to hold parts of the output and then assembled the parts at the end of the analysis loop. You have to print the matrix row, and the analysis of the row, before moving on to the next row. That is the simplest way to create the table.

If you want to just test the formatting, and get tired of inputting 5 numbers and the size, just uncomment the #define TESTING line.

Good luck and best regards
Zlatko

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

const int maxRows = 20; // This is the maximum number of rows
int rows=10; // this is the number of rows present
const int columns=5;
bool foundTheNumber = false;

using namespace std;

void firstPrintHeading();
void shiftDownOneRow(int theArray[][columns]);
void secondPrintHeading();
bool isInArray(int number, int theArray[][columns],
              int startingRow, int endingRow);


//
//
// uncomment this for testing your formatting
//#define TESTING
//
//
//

int main()
{
   cout<<endl;
   int foundCount=0;
   int notFoundCount=0;
   char choice;
   int size;
   int x=0;
   int y=-1;
   int array[maxRows][columns]={ // change to maxRows. The first 5 rows are initialized
       {6,13,23,45,50},
       {5,12,34,36,51},
       {2,23,25,46,54},
       {2,7,35,39,48},
       {3,5,26,39,49},
       {12,15,23,27,34},
       {1,24,35,46,55},
       {3,13,35,47,50},
       {6,8,23,36,51},
       {5,12,25,39,54}};

       firstPrintHeading();

       for(int i=0; i<rows; i++)
       {
           cout<<setw(33)<< i <<")";
           for(int j=0; j<columns; j++)
           {
               cout<<setw(8)<< array[i][j];
           }
           cout<<endl;
       }
       //shift down the rows of the array
       shiftDownOneRow(array);

#ifndef TESTING
       cout<<"Enter 5 numbers: ";

       for(int j=0; j<columns; j++)
       {
           cin>>array[0][j]; // just use 0 for the row
       }
#endif
       do
       {
           x=0;
           y=-1;
#ifndef TESTING
           cout<<"Enter the size of the matrix to analize: ";
           cin>>size;
#else
           size = 5;
#endif
           cout<<endl;
           cout<<setw(82)<<" Frecuency's numbers of a row IN and/or OUT of a Matrix of size ( "<<size<<" x "<<columns<<" )\n";
           secondPrintHeading();


           /* this is not the place to print out the row, it must be formatted in the analysis loop */
/*           for(int i=0; i<rows; i++)
           {
               matrixRow<<setw(8)<< i <<")";
               for(int j=0; j<columns; j++)
               {
                   matrixRow<<setw(7)<<array[i][j];
               }
               cout<<endl;
           }
           cout<<endl;
*/



           // really I think you dont need to go backwards here.
           // you decide if you want to start analysis at row 0 or row 1
           // if starting at row 1, then you need to print row 0 before the analysis starts
           //for(int i=rows-1-size; i>=0; i--)
           for(int i = 0; i < size; ++i)
           {
               stringstream inMatrix;
               stringstream outMatrix;
               stringstream outputRow;

               //
               // format the row
               //
               outputRow<<setw(8)<< i <<")";
               for(int j=0; j<columns; j++)
               {
                   outputRow<<setw(7)<<array[i][j];
               }

               //
               // analyze the row
               //
               foundCount=0;
               notFoundCount=0;
               x++;// what name do think is meaningful for this variables
               y++;
               for(int j=0; j<columns; j++)
               {
                   // Remember, rows is the count of the number of rows
                   // the last valid row index is rows-1, because indexes start at 0
                   if (isInArray(array[i][j], array, rows-size-y, rows-x))
                   {
                       foundCount++;
                       inMatrix <<setw(3)<<array[i][j];
                   }
                   else
                   {
                       notFoundCount++;
                       outMatrix <<setw(3)<<array[i][j];
                   }
               }
               outputRow << "      " << inMatrix.str();
               // The first "|" will be at column 70
               outputRow << setw(70-outputRow.str().length()) << "| " << outMatrix.str();
               // The second "|" will be at column 85
               outputRow << setw(85-outputRow.str().length()) << "| " << setw(3) << foundCount;
               outputRow << " | " << setw(3) << notFoundCount;
               cout << outputRow.str() << endl;
           }

           //
           // print out the rest of the rows
           //
           for(int i=size; i<rows; i++)
           {
               cout<<setw(8)<< i <<")";
               for(int j=0; j<columns; j++)
               {
                   cout<<setw(7)<<array[i][j];
               }
               cout<<endl;
           }
           cout<<endl;

#ifndef TESTING
           cout<<"Would you like to look up another "
               "size of the matrix? (y/n): ";
           cin >> choice;
#else
           choice = 'n';
#endif

       }  while (choice == 'y'|| choice == 'Y');
       cout<<endl;
       return 0;
}
void firstPrintHeading()
{

   cout<<setw(82)<<"=====================================================\n";
   cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
   cout<<setw(81)<<"-----------------------------------------------------\n";
}

/*
start at second last row, and shift it to the last
then keep going backwards until the first row is shifted to the
second row
*/
void shiftDownOneRow(int theArray[][columns])
{
   for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow)
   {
       for(int col = 0; col < columns; ++col)
       {
           theArray[shiftRow + 1][col] = theArray[shiftRow][col];
       }
   }
   ++rows;
}
void secondPrintHeading()
{
   cout<<setw(97)<<" ==================================================================================\n";
   cout<<setw(101)<<" Matrix's Numbers        Total Matrix's Numbers\n";
   cout<<"      ROWS    (0)    (1)    (2)    (3)    (4)        IN            OUT              IN     OUT\n";
   cout<<"   ---------------------------------------------------------------------------------------------------\n";
}
/*
Search an array from the startingRow to the endingRow for a number. Return true
if the number is found, and false otherwise
*/
bool isInArray(int number, int theArray[][columns],
              int startingRow, int endingRow)
{
   for(int row = startingRow; row <= endingRow; ++row)
   {
       for(int col = 0; col < columns; ++col)
       {
           if (theArray[row][col] == number) return true;
       }
   }
   return false;
}

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.