You are here:

C++/Working with functions

Advertisement


Question

    Hello Zlatko. I tried in the following program get the equal rows from a matrix of tens but without any success. The function
Mycompare() when is call read the matrix of data, not the matrix of tens and therefore I get an output that I don't want. I do not have
idea as to do that the function MyCompare read the matrix of tens. Can you help me on this program. Thanks.

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

const int rows= 11;
const int columns= 5;

using namespace std;

void printHeading();
void displayRowMatch(int [], int );
bool MyCompare(int*, int* , int );

int main()
{
   cout<<endl;
   int sizeMatrix;
   bool comparison = false;
   bool foundTheRows = false;
   char choice;
   stringstream inMatrix;
   int sumRow[rows];
   memset(sumRow, 0, sizeof(sumRow));
   bool reported[rows];
   memset(reported, 0, sizeof(reported));
   int foundRows[rows];
   memset(foundRows, 0, sizeof(foundRows));

   int array[rows][columns] ={
     {2, 15, 18, 20, 50},
     {2, 15, 18, 20, 50},
     {7, 11, 14, 15, 34},
     {1, 31, 33, 34, 50},
     {8, 18, 45, 47, 50},
    {1, 10, 12, 32, 36},
    {3, 4, 15, 27, 50},
    {1, 2, 13, 19, 27},
    {12, 17, 21, 23, 30},
    {11, 36, 37, 41, 55},
    {4, 12, 13, 21, 50 }};

   do
  {
       memset(reported, 0, sizeof(reported));
       memset(foundRows, 0, sizeof(foundRows));
       comparison = false;
       foundTheRows = false;

       cout<<"    Enter the size of a matrix: ";
       cin>>sizeMatrix;
       cout<<endl;
      cout<<setw(55)<<" Turning a matrix of data of size"
                     " ("<<sizeMatrix<<" x "<<columns<<") into a matrix of tens\n";
      cout<<setw(91)<<"-------------------------------------------------"
                       "-----------------------\n\n";
       cout<<setw(85)<<"Matrix of Data                                      "
                       "Matrix of Tens\n";
       cout<<setw(95)<<"-------------------------------                      "
                       "-------------------------------\n\n";
       printHeading();

      for(int i=0; i< sizeMatrix; i++)
      {
          cout<<setw(6)<<i<<")   |";
          for(int j=0; j< columns; j++)
          {
              cout<<setw(7)<<array[i][j];
           }
          cout << inMatrix.str();
           cout<<setw(7 - inMatrix.str().length())<<"|| ";  
           inMatrix.str("");
           cout<<setw(4)<<i<<")   |";

           for(int j=0; j<columns; j++)
           {
               if(array[i][j]>=1 && array[i][j]<=10)
               {
                   cout<<setw(7)<<"1";
               }
               else if(array[i][j]>=10 && array[i][j]<=19)
               {
                   cout<<setw(7)<<"10";
               }
               else if(array[i][j]>=20 && array[i][j]<=29)
               {
                   cout<<setw(7)<<"20";
               }
               else if(array[i][j]>=30 && array[i][j]<=39)
               {
                   cout<<setw(7)<<"30";
               }
               else if(array[i][j]>=40 && array[i][j]<=49)
               {
                   cout<<setw(7)<<"40";
               }
               else if(array[i][j]>=50 && array[i][j]<=56)
               {
                   cout<<setw(7)<<"50";
               }
           }
           cout<<endl;
      }
      cout<<endl<<endl;
       cout<<setw(75)<<"Repetition of rows in the Matrix of Tens "
                       "of size ("<<sizeMatrix<<" x "<<columns<<")"<<endl;
     cout<<setw(90)<<"-------------------------------------------------"
                       "---------------------\n\n";
       cout<<setw(32)<<"Rows\n";
       cout<<setw(80)<<" Matched         (0)     (1)     (2)     (3)     (4) \n";
       cout<<setw(86)<<"-----------------------------------------"
                       "-----------------------\n";

       for(int i=0; i< sizeMatrix; i++)
       {
           if (! reported[i])
           {
               for(int j=i+1; j< sizeMatrix; j++)
               {

             // ZM if there are any matches, the
             // comparison flag is set to prevent the "no match" message.

                   if(MyCompare(array[i], array[j], columns))
                   {
                       comparison = true; // ZM if **any** matches,
                                          // dont print the no match message below.
                       while(! reported[i])
                       {
                           if (! reported[i])
                           {
                               cout<<setw(29)<<i<< ',' <<" "<<j<<")   |";

                               displayRowMatch(array[i], columns);

                               reported[i] = true;
                               reported[j] = true;
                           }
                       }
                   }
               }
               if (reported[i]) cout << endl;
           }
       }
       cout<<endl;
       if(! comparison )
       {
           cout<<setw(69)<<"There's not equal rows "
                          "in the matrix ("<<sizeMatrix<<" x "<<columns<<")"<<endl;
       }
       cout<<"\n    Would you like to analyze another "
             "size of other matrix (y/n)? : ";
       cin >> choice;
  }    while (choice == 'y'|| choice == 'Y');

    cout<<endl;
    return 0;
}
void printHeading()
{
   cout<<setw(99)<<" Rows       (0)    (1)    (2)    (3)    (4)         "
                   "Rows       (0)    (1)    (2)    (3)    (4)\n";
   cout<<setw(97)<<"------------------------------------------------      "
                   "------------------------------------------------\n";
}
void displayRowMatch(int array[], int index)
{
   for( int i=0; i < index; i++)
   {
     cout << setw(8) << array[i];
   }
}
bool MyCompare(int* first, int* second, int numelements)
{
   bool rowMatch = true;

   for(int i=0; i<numelements; i++)
   {
     for(int j=i+1; j<numelements; j++)
      {
        for(int k=0; k<columns && rowMatch; k++)
         {
            if(first[i]!=second[i])

            return  false;
         }
      }
   }
   return true;
}  

Answer
Hi Raul.
The matrix of 10s doesn't really exist. The program just prints out the 10s data based on the original matrix. You will need to save the 10s into another array and then pass that array to MyCompare. Right now the program is passing the original matrix into MyCompare, so of course that is the one being examined.

Create a new two dimensional array like this:
int matrixOfTens[rows][columns];
and fill it as you are doing the printing. Then pass it to MyCompare.

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.