You are here:

C++/How to compare two 2D array and display the rows that do not match

Advertisement


Question
Hello Zlatko,
This program i am trying to do is to compare two 2D array and display the rows that do not match, where the user has the possibility to choose the size of the matrix of the first array  and compare it with regard to the second array.
The only change i made with regard to what I had taught previously was on the line 121, where i placed the name of the arrays that i am comparing ( firstArray and secondArray ). I believe that the problem lies in the  function ( compareTwo2DArrays )  which is designed to compare arrays within a single 2D array and my objective is to compare two arrays of 2D . Could you help me. Thanks.


#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>

const int rows = 8;
const int rowsNumber = 13;
const int columns = 5;

using namespace std;

void printHeading();
void displayRowUnMatched(int [], int);
bool compareTwo2DArrays(int*, int*, int);

int main()
{
   cout<<endl;
   int numberOfRows;
   char choice;
   int firstArray[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}
   };
   int secondArray[rowsNumber][columns] =  { {2, 15, 18, 20, 50},
       {7, 11, 14, 15, 34},
       {4, 5, 8, 10, 40},
       {7, 12, 13, 27, 51},
       {1, 31, 33, 34, 50},
       {8, 18, 45, 47, 50},
       {12, 15, 17, 23,53},
       {1, 10, 12, 32, 36},
       {3, 4, 15, 17, 20},
       {23, 25, 34, 46, 49},
       {17, 34, 36, 38, 51},
       {1, 2, 13, 19, 27},
       {12, 17, 21, 23, 30}
   };

   do
   {
       cout<<"    Enter the number of rows that you'd like to compare: ";
       cin>> numberOfRows;
       cout<<endl;
       if( numberOfRows <= 1 )
       {
         cout<<endl<<endl<<endl;
         cout<<setw(90)<<" I'm sorry you must select at least two rows "
         "to be compared




";
       }
       else if( numberOfRows > rows )
       {
         cout<<endl<<endl<<endl;
         cout<<setw(78)<<" I'm sorry the maximum of rows to be compared is "
         << rows <<"




";
       }
       else
       {
         cout<<endl<<endl<<endl;
         cout<<setw(64)<<"First Array
";
         cout<<setw(71)<<"---------------------


";
         printHeading();

         for(int i = 0; i < numberOfRows; ++i)
         {
         cout<<setw(37)<< i <<")  |";
         for(int j = 0; j < columns; ++j)
         {
         cout<<setw(7)<< firstArray[i][j];
         }
         cout<< endl;
         }
         cout<<endl<<endl<<endl;
         cout<<setw(64)<<"Second Array
";
         cout<<setw(71)<<"----------------------


";
         printHeading();

         for(int i = 0; i < rowsNumber; ++i)
         {
         cout<<setw(37)<< i <<")  |";
         for(int j = 0; j < columns; ++j)
         {
         cout<<setw(7)<< secondArray[i][j];
         }
         cout<<endl;
         }
         cout<<endl<<endl<<endl;
         bool uniqueHeadingPrinted = false;

         for(int i = 0; i < rowsNumber; ++i)
         {
         bool rowUnique = true;  //Assume row i is unique until you find otherwise
         for(int j = 0; j < numberOfRows; ++j)
         {
         if( i != j && compareTwo2DArrays(secondArray[i], firstArray[j], columns) == true )
         {
         rowUnique = false;
         break;
         }
         }
         if( rowUnique )
         {
         if ( ! uniqueHeadingPrinted )
         {
         cout<<setw(87)<<"Rows that do not match between the first and second array
";
         cout<<setw(94)<<"------------------------------------------------------------------


";
         printHeading();
         uniqueHeadingPrinted = true;
         }
         cout <<setw(37)<< i <<")  |";
         displayRowUnMatched(secondArray[i], columns);
         cout<< endl;
         }
         }
         cout<<endl<<endl<<endl<<endl;
       }
       cout<<"    Would you like to try again (y/n)? : ";
       cin >> choice;
   }
   while (choice == 'y'|| choice == 'Y');

   return 0;
}

void printHeading()
{
   cout<<setw(79)<<"  Rows        (0)    (1)    (2)    (3)    (4)
";
   cout<<setw(84)<<"-----------------------------------------------------
";
}

void displayRowUnMatched(int array[], int index)
{
   for( int i=0; i < index; i++)
   {
       cout << setw(7) << array[i];
   }
}

bool compareTwo2DArrays(int first[], int second[], int numelements)
{
   for(int row = 0; row < numelements; ++row)
   {
       if(first[row] != second[row])
         return false;
   }
   return true;
}

Answer
Hi Raul

Before, you were comparing a 2D array against itself. For that reason you wanted to avoid comparing row[i] with row[j] when i == j and for that reason you have the check of i!=j before doing the comparison.

Now you are comparing two different 2D arrays, so the restriction that i must not equal j no longer holds. Remove that from the line

if( i != j && compareTwo2DArrays(secondArray[i], firstArray[j], columns) == true )

and you should get better results.

I'm not on my regular computer, so I cannot try it out. Let me know if you still have trouble with it.

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.