You are here:

C++/Creating a funtion

Question
Hello Zlatko, one year ago you created this function:

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;
}
As you can see, what this function does is to compare two rows of numbers into an array to check if they match, This function is fantastic, but with the limitation that only compare two rows, but what about if there are three or more rows equal into the array, then this function does not work. I created the following function based in your function:

bool MyCompare(int* first, int* second, int* thirt, int numelements)
{
bool rowMatch = true;

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

return  false;
}
}
}
}
return true;
}
but I can not run my program using this function:
Could you check this function to see if it is right?
It is possible to create a function that return all the equal rows into an array no matter what is the size of this array? Thanks.

Hello Raul.

I cannot take the credit for creating that MyCompare function. I helped you to debug it, but clearly I did not do a very good job. As I understand it, the original MyCompare was to compare 2 integer arrays and return their equality. For that you don't need three nested loops. In fact, in the original, the loop with the k counter does nothing useful, and rowMatch is never used either. So lets start with fixing the original MyCompare, and lets give it to a smarter name.

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

To compare if two arrays are equal, all you really need to do is go though the 2 arrays and compare their corresponding elements. As soon as a mismatch is found, the function can return false. If no mismatch is found then the arrays must be equal.

To add a third array, you can change the ArrayCompare function to accept a third array or you can use the ArrayCompare function as a building block to another comparison function.

Method 1
------------------------------
With the first idea, you can add a third array like this:

bool ArrayCompare3(int* first, int* second, int* third, int numelements)
{
for(int i=0; i<numelements; i++)
{
if(???)
return false;
}
return true;
}

The question is what replaces the question marks ???

It is easy to check that the corresponding elements of all three arrays are equal
equal means (first[i] == second[i] && first[i] == third[i])
Notice that we don't need to check second against third because if first matches second, and first matches third, then we know that second matches third too.

But, we want to check if they are not all equal, so the expression can be rewritten as
notEqual means !(first[i] == second[i] && first[i] == third[i])
Now, using DeMorgan's rule, we can rewrite that as
notEqual means (first[i] != second[i] || first[i] != third[i])
so you see that part of the problem was that you were using && when you should have been using ||.

The final version of the function becomes:

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

Method 2
------------------------------
With the second method, you use ArrayCompare as a building block for ArrayCompare3

bool ArrayCompare(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 ArrayCompare(first, second, numelements) && ArrayCompare(first, third, numelements)
}

In this method, if the comparison of first and second returns true, the program goes on to compare first and third. If the comparison of first and second returns false, the program does not check first against third and the ArrayCompare3 function returns false right away.

Well, I hope that helps you out a bit.

Best regards
Zlatko
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Zlatko is the best

C++

Volunteer

Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.