Hi there I was wondering if I could get help with some C Her
Hi there! I was wondering if I could get help with some C++. Here is the question:
Again, consider the testPIN function used in Program 7-21. For convenience, we have reproduced the code for you below.
Rewrite the function body of this function so that instead of using iterating through the elements of the parallel arrays , it returns true or false by calling countMatches the function defined in the previous exercise (10988), and checking its return value.
Here is the code for the question:
bool testPIN(int custPIN[], int databasePIN[], int size)
{
for (int index = 0; index < size; index++)
{
if (custPIN[index] != databasePIN[index])
return false;
}
return true;
}
Here is the function countMatches:
int countMatches(int custPIN[], int databasePIN[], int size)
{
int matches = 0;
for (int index = 0; index < size; index++) {
if (custPIN[index] == databasePIN[index])
matches++;
}
return matches;
}
What I dont understand is, how do they need me to check the value for matches without iterating through each value of testPIN?
Solution
The testPIN() function can be modified as below.
bool testPIN(int custPIN[], int databasePIN[], int size)
{
int matches = countMatches(custPIN, databasePIN, size);
if (matches == size)
return true;
else
return false;
}
As per the problem, call countMatches() function from testPIN() function.
Withing countMatches(), the testing happens by comparing every custPIN with every databasePIN.
If the match happens, then the \"matches\" should contain the value which is equal to the size of the
array.
We can compare the matches with size and return true if every PIN is matched
OR if any of the PIN is not matched, then return FALSE.
