You are here:

C++/How to print results in a table

Advertisement


Question

                                  How to print results in a table
                  ********************************************************

     Hi Zlatko, the following program belong to the topic " An array inside a 2D array ". In this program the user
can enter five numbers once during the whole program being this a limitation but I think it could be fix it,
besides this the user can choose with what size of the matrix work to check which row's numbers are inside the
matrix chosen and which not. My problem here is I do not know how to print the results in a table. This is the
output I want:
   
                                Frecuency of the numbers of a row IN and/or OUT of a Matrix of size ( 5 x 5 )
      
=====================================================================
==============
                                                                                     Matrix's Numbers                             Total Matrix's
Numbers
          ROWS   (0)     (1)      (2)     (3)      (4)            IN                               OUT                              IN     OUT
      ------------------------------------------------------------------------------------
-------------------------------------------------------------
              0)      12      13      14      15      39             12  13  15                   14  39                            3         2
              1)        1      23      34      35      49               1  23  34  35             49                                  4         1
              2)        6      13      23      45      50             13  23  50                     6  45                            3         2
              3)        5      12      34      36      51             12  34  36  51               5                                  4         1
              4)        2      23      25      46      54             23  25  46  54               2                                  4         1
              5)      12      15      23      27      34
              6)        1      24      35      46      55
              7)        3      13      35      47      50
              8)        6        8      23      36      51
              9)        5      12      25      39      54

         To achieve this tabulation I think I should create:
 
               1)  A two dimensional array to store the entire data matrix
               2)  An array to store all the numbers within the matrix.
               3)  An array to store all the numbers out of it
               4)  An array to store the total of all the numbers within the matrix.
              5)  An array to store the total of all numbers outside the matrix.
  
               The problem is to determine how many arrays chose and what dimension should be used in each case.
In point 1) I am clear that it is two dimensional array but others do not know.
              I would like to use a function to print the results because I will use it several time in my main program.
I think this function should be like this:

           void printResults(int array[][columns], int numbersInMatrix[],
                                       int numbersOutMatrix[], int totalOfNumbersInOutMatrix[])
        {
             for(int i=0; i< rows; i++)
             {
                  cout<<setw(8)<<i<<")";
                  for(int j=0; j< columns; j++)
                  {
                       cout<<setw(7)<<array[i][j];
                  }
                  for(int j=0; j< columns; j++)
                  {
                       cout<<setw(6)<<numbersInMatrix[i]<<"   ";
                       cout<<setw(6)<<numbersOutMatrix[i]<<"   ";
                       cout<<setw(3)<<totalOfNumbersInOutMatrix[i];
                  }
                  cout<<endl;
             }
        }

              but like I said before, the argument of this function it could be wrong. Could you help to do this
because it is my first time to create a table this way. THANKS
             This is the code:

#include <iostream>
#include <iomanip>

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);

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);

      cout<<"
   Enter 5 numbers: ";

      for(int j=0; j<columns; j++)
      {
           cin>>array[0][j]; // just use 0 for the row
      }
      do
      {
          x=0;
          y=-1;

          cout<<"
   Enter the size of the matrix to analize: ";
          cin>>size;
          cout<<endl;
          cout<<setw(82)<<" Frecuency's numbers of a row IN and/or OUT "
                        "of a Matrix of size ( "<<size<<" x "<<columns<<" )
";
          secondPrintHeading();
          for(int i=0; i<rows; i++)
          {
              cout<<setw(8)<< i <<")";
              for(int j=0; j<columns; j++)
              {
                  cout<<setw(7)<<array[i][j];
              }
              cout<<endl;
          }
          cout<<endl;
          for(int i=rows-1-size; i>=0; i--)
          {
              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++;
                      cout<<setw(47)<<array[i][j]<<"  IN the matrix
";
                  }
                  else
                  {
                      notFoundCount++;
                      cout<<setw(47)<<array[i][j]<<"  NOT in matrix
";
                  }
              }
          cout<<endl;
          cout<<setw(40)<<foundCount<<"  numbers IN matrix"<<endl;
          cout<<setw(40)<<notFoundCount<<"  numbers NOT in matrix

";
          }
          cout<<"
   Would you like to look up another "
                "size of the matrix? (y/n): ";
          cin >> choice;
      }  while (choice == 'y'|| choice == 'Y');
      cout<<endl;
      return 0;
}
void firstPrintHeading()
{
   
cout<<setw(82)<<"=====================================================

";
   cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)
";
   cout<<setw(81)<<"-----------------------------------------------------
";
}
/*
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)<<" ============================================"
                   "======================================

";
   cout<<setw(101)<<" Matrix's Numbers        Total Matrix's Numbers
";
   cout<<"      ROWS    (0)    (1)    (2)    (3)    (4)       "
         " IN            OUT              IN     OUT
";
   cout<<"   ------------------------------------------------------"
         "---------------------------------------------
";
}
/*
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;
}  

Answer
Hello Raul.

September 9
------------
I suppose I can answer some of your questions. It seems that analysis is done row by row. Once one row is analyzed, the results can be printed out, and analysis moves on to the next row. If that is the case, then I can answer your 5 questions about array sizes.

You asked about array dimensions and sizes.
1)  A two dimensional array to store the entire data matrix
Yes, you have that now.

2)  An array to store all the numbers within the matrix.
Yes, you need a one dimensional array. It seems that each row is analysed separately, and if you have 5 columns per row, then at most 5 numbers can be IN the matrix so the array should have space for 5, or whatever your column size is.

3)  An array to store all the numbers out of it
Yes, you need another one dimensional array for the numbers OUT of the matrix. Again, it seems that at most 5 numbers will be in this array.

4)  An array to store the total of all the numbers within the matrix.
I think you need just a single variable to store this because the analysis and output will be done row by row. As soon as one row is done, the variables can be reused for the next row.

5)  An array to store the total of all numbers outside the matrix.
Just like part 4, this can be a single integer.
 

September 8
-------------
Before I can help you more, I need to understand a few things. For the matrix

      (0)     (1)      (2)     (3)    (4)
------------------------------------------
0)      12      13      14      15      39
1)       1      23      34      35      49
2)       6      13      23      45      50
3)       5      12      34      36      51
4)       2      23      25      46      54
5)      12      15      23      27      34
6)       1      24      35      46      55
7)       3      13      35      47      50
8)       6       8      23      36      51
9)       5      12      25      39      54

When the user is asked to input a size to analyze
The user inputs 5. Does that mean that rows 0, 1, 2, 3 and 4 are analyzed ?

When row 0 is analyzed why are 12, 13 and 15 IN ? Is it because there is a 12 in row 5, a 13 in row 2, and a 15 in row 5 ?

For row 0, why is 14 OUT ? Is it because there is no 14 in the rest of the matrix, outside of row 0 ? Why is 39 OUT ? There is a 39 in row 9.

I cannot find a consistent pattern of what it means to be IN or OUT. I think if you help me understand what you are trying to get, I can show you the code for it.

Here is what I think you want.
Given an ending row E, go through each row R from 0 to E-1.
For each row R, print out which numbers in R are also in rows from 0 to E-1 but excluding row R.

So if E is 5, you want to analyze rows 0 through 4.

For row 0, all numbers in row 0, that are also in rows 1,2,3,4 are IN and all numbers not in rows 1,2,3,4 are OUT.

For row 1, all numbers in row 1, that are also in rows 0,2,3,4 are IN and the rest are OUT.

For row 2, all numbers in row 2, that are also in rows 0,1,3,4 are IN and the rest are OUT.

For row 3, all numbers in row 3, that are also in rows 0,1,2,4 are IN and the rest are OUT.

Finally, for row 4, all numbers in row 4, that are also in rows 0,1,2,3 are IN and the rest are OUT.

Is that what you want ?

Best regards
Zlatko

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.