944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3224
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 28th, 2007
0

help with recursion

Expand Post »
can someone tell me what is wrong with this code? We are supposed to use recursion to see if the two arrays are equal.

C++ Syntax (Toggle Plain Text)
  1. int Equal(int a[], int b[], int left, int size)
  2. {
  3. if (a == b)
  4. {
  5. return Equal(a, b, left + 1, size);
  6. }
  7. else
  8. return false;
  9. }
  10.  
  11. int Equal2(int a[], int c[], int left, int size)
  12. {
  13. if (a == c)
  14. {
  15. return Equal2(a, c, left + 1, size);
  16. }
  17. else
  18. return false;
  19.  
  20. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ammochck21 is offline Offline
20 posts
since Feb 2007
Feb 28th, 2007
0

Re: help with recursion

Try running through your code on paper, as though you were the computer. You'll see that you're not using the left variable for anything, except the +1 before recursing. And you'll recurse infinitely unless a and b are different. Which they almost always will be, unless you're comparing an array to itself. You need to index the arrays (using left) and check for bounding errors to make sure you don't go over the length of the arrays.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 28th, 2007
0

Re: help with recursion

Why are you using 2 separate functions to perform the recursion?

Your comparison is likely to be what's causing the problem -- you can't just compare the starting memory addresses for 2 arrays to check if the contents are equal, you have to individually check each element.

A few other points:
  • You need to make sure that the index value is not larger than the size of the array (I'm guessing that left is your index value). If it is, return immediately.
  • The method described only compares arrays of equal size. Hopefully this is what your teacher wants.
  • Recursion like this example isn't really going to be that useful. The problem here could have been solved with a loop, but regardless, you still have to do your assignments.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 28th, 2007
0

Re: help with recursion

I kind of see what you are saying- and when i ran through it, i did it right- which obviously means that i wrote the program wrong. I dont really understand what you mean by i need to index the arrays using left. I also added a while loop saying while its not at the end of the array but then i got a weird number for Equal 2.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ammochck21 is offline Offline
20 posts
since Feb 2007
Feb 28th, 2007
0

Re: help with recursion

Yes i would rather use a for loop for this too but we arent supposed to. the arrays are all of equal size and the first and third arrays are identical and the second one is one number off. I guess i dont understand how to transform a for loop into a recursive function.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ammochck21 is offline Offline
20 posts
since Feb 2007
Feb 28th, 2007
0

Re: help with recursion

>I dont really understand what you mean by i need to index the arrays using left.
In other words:
C++ Syntax (Toggle Plain Text)
  1. if (a[ left ] == b[ left ]) {
  2. // element matches
  3. }
  4. else {
  5. // element doesn't math; return false
  6. }

Arrgh, those stupid [ left ] tags keep parsing out!
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 28th, 2007
0

Re: help with recursion

i actually had a(left) == b(left) but when i originally posted it they went away sorry
Last edited by ammochck21; Feb 28th, 2007 at 1:33 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ammochck21 is offline Offline
20 posts
since Feb 2007
Feb 28th, 2007
0

Re: help with recursion

Click to Expand / Collapse  Quote originally posted by ammochck21 ...
i actually had a(left) == b(left) but when i originally posted it they went away sorry
Well, then it should just be a matter of implementing bounds-checking, and then your program should work, unless I'm totally missing something here...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 28th, 2007
0

Re: help with recursion

I actually kind of figured out what was wrong but i cant seem to figure out how to implement it.

C++ Syntax (Toggle Plain Text)
  1. int Equal(int a[], int b[], int left, int size)
  2. {
  3. if (left <= size)
  4. {
  5. if (a(left) == b(left))
  6. {
  7. return Equal(a, b, left + 1, size); //need to have a return true here or somewhere
  8. }
  9. else
  10. return false;
  11. }
  12. }
Last edited by ammochck21; Feb 28th, 2007 at 2:05 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ammochck21 is offline Offline
20 posts
since Feb 2007
Feb 28th, 2007
0

Re: help with recursion

You need to do your bounds-checking. If the last elements are equal, you return true, will which will transfer through all the calls of the function.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help me?!
Next Thread in C++ Forum Timeline: quick question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC