help with recursion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2007
Posts: 20
Reputation: ammochck21 is an unknown quantity at this point 
Solved Threads: 0
ammochck21 ammochck21 is offline Offline
Newbie Poster

help with recursion

 
0
  #1
Feb 28th, 2007
can someone tell me what is wrong with this code? We are supposed to use recursion to see if the two arrays are equal.

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: help with recursion

 
0
  #2
Feb 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with recursion

 
0
  #3
Feb 28th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: ammochck21 is an unknown quantity at this point 
Solved Threads: 0
ammochck21 ammochck21 is offline Offline
Newbie Poster

Re: help with recursion

 
0
  #4
Feb 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: ammochck21 is an unknown quantity at this point 
Solved Threads: 0
ammochck21 ammochck21 is offline Offline
Newbie Poster

Re: help with recursion

 
0
  #5
Feb 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with recursion

 
0
  #6
Feb 28th, 2007
>I dont really understand what you mean by i need to index the arrays using left.
In other words:
  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!
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: ammochck21 is an unknown quantity at this point 
Solved Threads: 0
ammochck21 ammochck21 is offline Offline
Newbie Poster

Re: help with recursion

 
0
  #7
Feb 28th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with recursion

 
0
  #8
Feb 28th, 2007
Originally Posted by ammochck21 View Post
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...
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 20
Reputation: ammochck21 is an unknown quantity at this point 
Solved Threads: 0
ammochck21 ammochck21 is offline Offline
Newbie Poster

Re: help with recursion

 
0
  #9
Feb 28th, 2007
I actually kind of figured out what was wrong but i cant seem to figure out how to implement it.

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: help with recursion

 
0
  #10
Feb 28th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC