You are here:

# C++/How to display the rows that do not match in a matrix

Question
QUESTION: HelloZlatko.
The objective of this program is to get the rows that are not the same within a data matrix. To achieve this it is necessary to compare all rows in a matrix selected by the user, this will get with the function:
ArrayCompare2 bool (int * first, int * second, int numElements)
{
for (int i = 0; i i++)
{
if (first [i] == second [i])
return false;
}
return true;
}
What I think is not working in this program is on the line 106 to the line 122 and of course this is because I do not know how to reproduce this problem. I not sure if I explain well. Thank you very much.

Lets have a look at this function.

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

It returns false as soon as it finds two corresponding elements which are equal. Just because two elements are equal it does not mean that all the elements are equal. Generally most comparison functions return true if two things are equal, and false if they are not. I think that is what you are trying to do. If two corresponding elements are NOT equal, then you know that the two arrays are not equal, and it is safe to return false right away. But, if two corresponding elements ARE equal, you cannot make a conclusion about the rest of the array.

First thing to do is fix the function to return false if the two arrays are unequal, and true otherwise.

If the objective is to find rows which are unique in the matrix, then this is how to do it, in psuedocode

for i = 0 to count
{
rowUnique = true; //Assume row i is unique until you find otherwise
// loop to check row i against all rows after it
for j = i + 1 to count
{
if (ArrayCompare(row[i], row[j]) == true)
{
rowUnique = false;
break; //You can break here, there is no point in looking further
}
}

// if no identical row was found, then row i is unique
if rowUnique
{
print row i is unique.
}
}

I think you can translate the logic into C.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Hello Zlatko.
This is what you told me to do:

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

const int rows = 15;
const int columns = 5;

using namespace std;

void displayRowNoMatched(int[], int );
bool ArrayCompare2( int*, int*, int );

int main()
{
cout<<endl;
int numberOfRows;
char choice;
//  bool reported[rows];
//  memset(reported, 0, sizeof(reported));
bool rowsMatched = true;

int array[rows][columns] = { {2, 15, 18, 20, 50},
{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},
{8, 18, 45, 47, 50},
{12, 17, 21, 23, 30},
{11, 36, 37, 41, 55},
{4, 12, 13, 15, 50},
{2, 15, 18, 23, 50},
{2, 14, 15, 20, 40},
{2, 15, 18, 23, 50}
};

cout<<endl;
cout<<setw(91)<<"Let's trying to find the rows that do not match in a matrix data \n";
cout<<setw(99)<<"-------------------------------------------------------------------------- \n\n\n\n";

do
{
// memset(reported, 0, sizeof(reported));
rowsMatched = true;

cout<<"    Enter the number of rows that you'd like to compare: ";
cin>> numberOfRows;
cout<<endl;
if( numberOfRows <= 1 )
{
cout<<endl<<endl<<endl<<endl<<endl;
cout<<setw(90)<<" I'm sorry you must select at least two rows "
"to be compared \n\n\n";
}
else if( numberOfRows > rows )
{
cout<<endl<<endl<<endl<<endl<<endl;
cout<<setw(78)<<" I'm sorry the maximum of rows to be compared is "
<< rows <<"\n\n\n";
}
else if( numberOfRows > 1 || numberOfRows < 16 )
{
cout<<endl<<endl<<endl;
cout<<setw(64)<<"Matrix Data \n";
cout<<setw(70)<<"---------------------\n\n\n";

for(int i=0; i< numberOfRows; i++)
{
cout<<setw(35)<< i <<")  |";
for(int j = 0; j < columns; j++)
{
cout<<setw(8)<< array[i][j];
}
cout<< endl;
}
for(int i=0; i< numberOfRows; i++)
{

for(int j = i+1; j < numberOfRows; ++j)
{
if(ArrayCompare2(array[i], array[j], columns) == false)
{
rowsMatched = false;
}
}
}
if( !rowsMatched )
{
cout<<endl<<endl<<endl<<endl;
cout<<setw(77)<<"Rows that do not match in the matrix data\n";
cout<<setw(85)<<"--------------------------------------------------- \n\n\n";

for(int i = 0; i < numberOfRows; ++i)
{
bool rowUnique = true;  //Assume row i is unique until you find otherwise

for(int j = i+1; j < numberOfRows; ++j)
{
if(ArrayCompare2(array[i], array[j], columns) == true)
{
rowUnique = false;
break;   // You can break here, there is no point in looking further
}
}
if( rowUnique )  // if no identical row was found, then row i is unique
{
// print row i is unique.
cout<<setw(35)<< i <<")  |";
displayRowNoMatched(array[i], columns);
cout<<endl;
}
}
}
else
{
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<setw(78)<<"There's not different rows "
"into the matrix data ("<< numberOfRows <<" x "<< columns <<")."<<endl<<endl<<endl;
}
}
cout<<"\n\n\n\n    Would you like to try again (y/n)? : ";
cin >> choice;
}
while (choice == 'y'|| choice == 'Y');

cout<<endl;

return 0;
}
{
cout<<setw(82)<<"  Rows        (0)     (1)     (2)     (3)     (4) \n";
cout<<setw(87)<<"--------------------------------------------------------- \n";

}
void displayRowNoMatched(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;
}

But the objective of this program is to get the rows that do not match within a data matrix.
The following should be the correct output:

Let's trying to find the rows that do not match in a matrix data
--------------------------------------------------------------------------

Enter the number of rows that you'd like to compare: 2

Matrix Data
---------------------

Rows        (0)     (1)     (2)     (3)     (4)
---------------------------------------------------------
0)  |       2      15      18      20      50
1)  |       2      15      18      20      50

There's not different rows into the matrix data (2 x 5).

Would you like to try again (y/n)? : y
Enter the number of rows that you'd like to compare: 4

Matrix Data
---------------------

Rows        (0)     (1)     (2)     (3)     (4)
---------------------------------------------------------
0)  |       2      15      18      20      50
1)  |       2      15      18      20      50
2)  |       7      11      14      15      34
3)  |       7      12      13      27      51

Rows that do not match in the matrix data
---------------------------------------------------

Rows        (0)     (1)     (2)     (3)     (4)
---------------------------------------------------------
2)  |       7      11      14      15      34
3)  |       7      12      13      27      51

Thank you very much for what you explained me earlier, it was very useful.

Hi Raul.

The major error I made was to have j go from i+1. It needs to go from 0. We want to compare the current row (i) against previous rows, as well as next rows, but we don't want to compare when both rows are the same row (when i == j). I always take a risk when I don't check my work on the computer.

Running the comparison loop once is enough. You don't need to run it once just to set the rowsMatched flag, and then run it again to find which ones are unique.

I think the code below works.

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

const int rows = 15;
const int columns = 5;

using namespace std;

void displayRowNoMatched(int[], int );
bool ArrayCompare2( int*, int*, int );

int main()
{
cout<<endl;
int numberOfRows;
char choice;
//  bool reported[rows];
//  memset(reported, 0, sizeof(reported));
bool rowsMatched = true;

int array[rows][columns] = { {2, 15, 18, 20, 50},
{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},
{8, 18, 45, 47, 50},
{12, 17, 21, 23, 30},
{11, 36, 37, 41, 55},
{4, 12, 13, 15, 50},
{2, 15, 18, 23, 50},
{2, 14, 15, 20, 40},
{2, 15, 18, 23, 50}
};

cout<<endl;
cout<<setw(91)<<"Let's trying to find the rows that do not match in a matrix data \n";
cout<<setw(99)<<"-------------------------------------------------------------------------- \n\n\n\n";

do
{
// memset(reported, 0, sizeof(reported));
rowsMatched = true;

cout<<"    Enter the number of rows that you'd like to compare: ";
cin>> numberOfRows;
cout<<endl;
if( numberOfRows <= 1 )
{
cout<<endl<<endl<<endl<<endl<<endl;
cout<<setw(90)<<" I'm sorry you must select at least two rows "
"to be compared \n\n\n";
}
else if( numberOfRows > rows )
{
cout<<endl<<endl<<endl<<endl<<endl;
cout<<setw(78)<<" I'm sorry the maximum of rows to be compared is "
<< rows <<"\n\n\n";
}
else if( numberOfRows > 1 || numberOfRows < 16 )
{
cout<<endl<<endl<<endl;
cout<<setw(64)<<"Matrix Data \n";
cout<<setw(70)<<"---------------------\n\n\n";

for(int i=0; i< numberOfRows; i++)
{
cout<<setw(35)<< i <<")  |";
for(int j = 0; j < columns; j++)
{
cout<<setw(8)<< array[i][j];
}
cout<< endl;
}

bool uniqueHeadingPrinted = false;
for(int i=0; i< numberOfRows; i++)
{
bool rowUnique = true;  //Assume row i is unique until you find otherwise

for(int j = 0; j < numberOfRows; ++j)
{
if(i != j && ArrayCompare2(array[i], array[j], columns) == true)
{
rowUnique = false;
break;
}
}
if (rowUnique)
{
if ( ! uniqueHeadingPrinted)
{
cout<<endl<<endl<<endl<<endl;
cout<<setw(77)<<"Rows that do not match in the matrix data\n";
cout<<setw(85)<<"--------------------------------------------------- \n\n\n";
}
cout<<setw(35)<< i <<")  |";
displayRowNoMatched(array[i], columns);
cout<<endl;
}
}
if ( ! uniqueHeadingPrinted)
{
cout<<endl<<endl<<endl<<endl<<endl<<endl;
cout<<setw(78)<<"There's not different rows "
"into the matrix data ("<< numberOfRows <<" x "<< columns <<")."<<endl<<endl<<endl;
}

//if( !rowsMatched )
//{
//    cout<<endl<<endl<<endl<<endl;
//    cout<<setw(77)<<"Rows that do not match in the matrix data\n";
//    cout<<setw(85)<<"--------------------------------------------------- \n\n\n";
//
//    for(int i = 0; i < numberOfRows; ++i)
//    {
//        bool rowUnique = true;  //Assume row i is unique until you find otherwise
//
//        for(int j = i+1; j < numberOfRows; ++j)
//        {
//          if(ArrayCompare2(array[i], array[j], columns) == true)
//          {
//          rowUnique = false;
//          break;   // You can break here, there is no point in looking further
//          }
//        }
//        if( rowUnique )  // if no identical row was found, then row i is unique
//        {
//          // print row i is unique.
//          cout<<setw(35)<< i <<")  |";
//          displayRowNoMatched(array[i], columns);
//          cout<<endl;
//        }
//    }
//}
//else
//{
//    cout<<endl<<endl<<endl<<endl<<endl<<endl;
//    cout<<setw(78)<<"There's not different rows "
//        "into the matrix data ("<< numberOfRows <<" x "<< columns <<")."<<endl<<endl<<endl;
//}
}
cout<<"\n\n\n\n    Would you like to try again (y/n)? : ";
cin >> choice;
}
while (choice == 'y'|| choice == 'Y');

cout<<endl;

return 0;
}
{
cout<<setw(82)<<"  Rows        (0)     (1)     (2)     (3)     (4) \n";
cout<<setw(87)<<"--------------------------------------------------------- \n";

}
void displayRowNoMatched(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;
}

Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Zlatko is the best expert. Thanks Zlatko for everything.

• Ask a Question

C++

Volunteer

#### Zlatko

##### Expertise

No longer taking questions.

##### Experience

No longer taking questions.

Education/Credentials
No longer taking questions.