-
Help with Reverse Array
I've tried and tried to solve this problem, but I cannot do it. Here is the question:
Write the definition of a function, isReverse , whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)
Here is what I've tried:
Code:
bool isReverse (int arr1 [], int arr2 [], int n) {
for (int k = 0; k < n; k++)
if (arr1 [k] == arr2 [n - 1 - k])
return true;
else
return false;
}
My professor tells me it returns true for the first time, but what about the other times
This is the message the lab gives me:
It seems that your function fails to check the ends of the
arrays. Please revise your code.
I don't know what else to try. I would appreciate somebody's help, please!
-
You don't want to return after checking the first element. You can return false if they don't match because if the first two don't match then the arrays aren't reverses of each other. But just because the first two match doesn't mean that the rest match. You can't return true yet. You have to continue in your loop. You can only return true after you have run the full loop.
-
Thank you so much, I got it right now :)