You are here:

C++/Creating a function

Advertisement


Question
Hello Zlatko, Thanks for your explanation in Creating a Function. I thought that understanding and create functions that display the rows that are repeated in an array would resolve this point
in my program but now I realize that it is not so, let me show you my program and the output that actually want to get, but first let me tell you that this program is to convert an array of data in their tens and then display the rows of the tens that are repeated:

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

const int rows= 14;
const int columns= 5;

using namespace std;

void printHeading();
void displayRowMatch(int [], int );
bool ArrayCompare2( int*, int*, int );
bool ArrayCompare3( int*, int*, int*, int );
bool ArrayCompare4(int* first, int* second, int* third, int* fourth, int numelements);

int main()
{
  cout<<endl;
  int numberOfRows;
  bool comparison = false;
  char choice;
  stringstream inMatrix;
  bool reported[rows];
  memset(reported, 0, sizeof(reported));
  int matrixOfTens[rows][columns];


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

    do
   {          
      memset(reported, 0, sizeof(reported));
      comparison = false;
         
      cout<<endl;
      cout<<setw(63)<<"    Equal Rows in a Matrix \n";
      cout<<setw(69)<<" ---------------------------------\n\n";
      cout<<"    Enter the number of rows that you'd like to compare: ";
      cin>> numberOfRows;
      cout<<endl;
      if( numberOfRows <= 1 )
      {
         cout<<setw(82)<<" I'm sorry you must select at least two rows "
         "to be compared \n\n";
      }
      else if( numberOfRows > rows )
      {
         cout<<setw(72)<<" I'm sorry the maximum of rows to be compared is "
         << rows << endl;
      }
      else
      {
      cout<<setw(55)<<" Turning a matrix of data of size"
         " ("<< numberOfRows <<" 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< numberOfRows; 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]<=9)
         {
         cout<<setw(7)<<"1";
         matrixOfTens[i][j]= 1;
         }
         else if(array[i][j]>=10 && array[i][j]<=19)
         {
         cout<<setw(7)<<"10";
         matrixOfTens[i][j]= 10;
         }
         else if(array[i][j]>=20 && array[i][j]<=29)
         {
         cout<<setw(7)<<"20";
         matrixOfTens[i][j]= 20;
         }
         else if(array[i][j]>=30 && array[i][j]<=39)
         {
         cout<<setw(7)<<"30";
         matrixOfTens[i][j]= 30;
         }
         else if(array[i][j]>=40 && array[i][j]<=49)
         {
         cout<<setw(7)<<"40";
         matrixOfTens[i][j]= 40;
         }
         else if(array[i][j]>=50 && array[i][j]<=56)
         {
         cout<<setw(7)<<"50";
         matrixOfTens[i][j]= 50;          
         }
         }
         cout<<endl;
  }
      
      cout<<endl<<endl;
      cout<<setw(70)<<"Equal rows in the Matrix of Tens "
         "of size ("<< numberOfRows <<" 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< numberOfRows; i++)
      {
         if (! reported[i])
         {
         for(int j=i+1; j< numberOfRows; j++)
         {
         
         // ZM if there are any matches, the
         // comparison flag is set to prevent the "no match" message.
         
         if(ArrayCompare2(matrixOfTens[i], matrixOfTens[j], columns))
         {
         comparison = true; // ZM if **any** matches,
         // dont print the no match message below.
         while(! reported[i])
         {
         if (! reported[i])
         {
         if(i >= 0 || i <= 49)
         cout<<setw(29)<<i<< ',' <<"  "<<j<<")  |";
         
         displayRowMatch(matrixOfTens[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 ("<< numberOfRows <<" x "<< columns <<")"<<endl;
      }
      }
      cout<<"\n    Would you like to trie again (y/n)? : ";
      cin >> choice;
 }    while (choice == 'y'|| choice == 'Y');

   cout<<endl;
   system ("PAUSE");
   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 ArrayCompare2(int* first, int* second, int numelements)
{
  for(int i=0; i<numelements; i++)
  {
      if(first[i] != second[i])
         return false;
  }
  return true;
}

bool ArrayCompare3(int* first, int* second, int* third, int numelements)
{
  return ArrayCompare2(first, second, numelements) && ArrayCompare2(first, third, numelements);
}

bool ArrayCompare4(int* first, int* second, int* third, int* fourth, int numelements)
{
  for(int i=0; i<numelements; i++)
  {
      if(first[i] != second[i] || first[i] != third[i] || first[i] != fourth[i])
         return false;
  }
  return true;
}

    This is the output I want to get from the array data if I select 13 rows to compare:

    Rows
    Matched          (1)    (2)    (3)    (4)    (5)
--------------------------------------------------------
       6,  7)    |     1      1     10     10     20
0, 2, 11, 12)    |     1     10     10     20     50
         
  Let's suppose that you run the program and select 4 rows to compare then you will get the
following output:

    Rows
    Matched          (1)    (2)    (3)    (4)    (5)
--------------------------------------------------------
       0,  2)    |     1     10     10     20     50

  But now if you select 13 rows to compare then you will get the following output:

    Rows
    Matched          (1)    (2)    (3)    (4)    (5)
--------------------------------------------------------
       0,  2)    |     1     10     10     20     50
       6,  7)    |     1      1     10     10     20
      11, 12)    |     1     10     10     20     50

 If you see in this output the function ArrayCompare2() does its work but is the moment to the
function ArrayCompare4() does its work too, because there are 4 rows equal and this is my
problem, how the functions ArrayCompare3() and ArrayCompare4() do their work inside the main()

I created another program using the function ArrayCompare4() but at the end it will do the same that this program:

  for(int i=0; i< numberOfRows; i++)
      {
         if (! reported[i])
         {
         for(int j=i+1; j< numberOfRows; j++)
         {
         for(int k=j+1; k< numberOfRows; k++)
         {
         for(int l=k+1; l< numberOfRows; l++)
         {
         // ZM if there are any matches, the
         // comparison flag is set to prevent the "no match" message.
         
         if(ArrayCompare4(matrixOfTens[i], matrixOfTens[j], matrixOfTens[k],
         matrixOfTens[l], columns))
         {
         comparison = true; // ZM if **any** matches,
         // dont print the no match message below.
         while(! reported[i])
         {
         if (! reported[i])
         {
         if(i >= 0 || i <= 49)
         cout<<setw(20)<<i<< ',' <<"  "<<j<< ',' <<"  "
         <<k<< ',' <<" "<<l<<")  |";
         
         displayRowMatch(matrixOfTens[i], columns);

         reported[i] = true;
         reported[j] = true;
         reported[k] = true;
         reported[l] = true;          
         }
         }
         }
         }
         }
         }
         if (reported[i]) cout << endl;
         }
      }

THANKS.

Answer
Hello Raul.

You don't need the ArrayCompare3 and ArrayCompare4 functions to get the output you want. If you find yourself making new functions for each case, you need to step back and re-examine the problem for a better solution. Certainly you cannot have a general solution by making a new function for every different quantity of rows which you want to check.

The problem with the code is these line right here:
cout<<setw(29)<<i<< ',' <<"  "<<j<<")  |";
displayRowMatch(matrixOfTens[i], columns);

It prints out 2 row values, and the closing bracket and then the matching row. How could it possibly print out more row values? The key is that the program needs to wait until all the matrixOfTens[j] rows are examined before it starts the displayRowMatch. The displayRowMatch must wait until for(int j=i+1; j< numberOfRows; j++) is done.

Inside the j loop, if a row matching i is found, then i is reported first, if it has not been reported before, and then j is reported. You could use cout to report the row numbers, but I found that sending the row numbers to a stringstream has an advantage. Using a stringstream lets you calculate the length of the output so the vertical bars '|' can be aligned nicely.

Try this code to replace your i loop and everything inside. Ask me if you don't understand the reason for something. Have fun!

for(int i=0; i< numberOfRows; i++)
{
   stringstream rowNumbers;
   if (! reported[i])
   {
       for(int j=i+1; j< numberOfRows; j++)
       {
         if(ArrayCompare2(matrixOfTens[i], matrixOfTens[j], columns))
         {
         comparison = true;
         if (! reported[i])
         {
         rowNumbers << setw(3) << i;
         reported[i] = true;
         }
         rowNumbers << setw(3) << j;
         reported[j] = true;
         }
       }
       if (reported[i])
       {
         //This setw lines up the vertical bars
         cout << setw(33 - rowNumbers.str().length()) << " " << rowNumbers.str();
         cout << ")  |";
         displayRowMatch(matrixOfTens[i], columns);
         cout << endl;
       }
   }
}

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.

©2016 About.com. All rights reserved.