How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Steve Your Own Question
Steve, Consultant
Category: Programming
Satisfied Customers: 292
Experience:  Steve is a consultant in the areas of computer software and programming, information management and networking.
Type Your Programming Question Here...
Steve is online now

I have a recursion program in C that is giving the incorrect

This answer was rated:

I have a recursion program in C that is giving the incorrect return value. Could you find the error? Link:

Hi there, my name is***** me have a look and I'll get back to you in a few minutes.

Customer: replied 1 year ago.

1) You should only have to pass three parameters: a, b and size. If size is different between a and b, then they don't have the same elements.

2) Your base case needs to be zero. If you get to 0, then everything matched.

3) You want to compare the elements and if there's a match, you swap the b values and then you do the same thing, only with size-1 this time.

Have a look at what I attached. I added print statements so you could see what it was doing and included some comments. Let me know if you have questions about it.

If I've answered your question and resolved your issue, can you please accept the answer and rate me 5 stars? I don't get paid anything for helping you unless you accept the answer. If not, please chat me back and I'll get you the information you need to resolve your issue. Thanks and have a great day!

P.S. Don't name your function "function." Give it a meaningful name. The word "function" is a reserved word in many other programming languages, so it's best to get in the habit of not using it as a function or variable name.

Here's a little more in-depth explanation:

Say you have:
a = {1, 2, 3, 4, 5}
b = {5, 4, 3, 2, 1}
size = 5
int same_elements(a, b, size)
so we call the function with a, b, 5
Now, we know that all C arrays start with zero, so the top of the array is always going to be size-1
for (i=size-1; i>=0; i--) we start at the top of the array and go to the bottom
if (a[size-1] == b) we compare the element at the top of a with b
so we'll compare a[4] to b[4], not equal; a[4] to b[3], not equal; a[4] to b[2], not equal; a[4] to b[1], not equal; a[4] to b[0] EQUAL.
So what we do is take that element in b and move it to the same position as it's found in a. We basically get rid of it so it doesn't get in the way. So now we have:
a = {1, 2, 3, 4, 5}
b = {4, 3, 2, 1, 5}
Now we want to check the remaining elements in a. So we call the same function, but instead of passing a size of 5, we pass a size of 4, or size-1. We only need to check the 4 remaining elements because we know the top one is the same, and we moved the b element that matched to the top of it.
The next time you match, you end up moving the 4 in b and you have:
a = {1, 2, 3, 4, 5}
b = {3, 2, 1, 4, 5}
Then you check with size-1 again.
If you get to where you pass size-1 and size-1 is zero, then everything matched.
When you're making your comparisons in the for loop, if you get to the end of the loop and you didn't find a match, then the return value is false.

Steve and other Programming Specialists are ready to help you
Customer: replied 1 year ago.
Thanks so much! My error was just calling the function within the function without prefixing 'return'. I'll rate you 5 stars. Thanks once again.