help with recursion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 20
Reputation: skeet123 is an unknown quantity at this point 
Solved Threads: 0
skeet123 skeet123 is offline Offline
Newbie Poster

help with recursion

 
0
  #1
Sep 13th, 2005
I am having a problem with a recursion problem. I have been reading alot of threads on this subject here and other places. I think I understand what recursion is. I am just having a problem trying to figure out this problem. I am suppose to write a helper function for maxarray.


Searching an array Find the maximum element in an unsorted array
if (anArray has only one item)
maxArray(anArray) is that item
else if (anArray has more than one item)
maxArray(anArray) is maximum of
maxArray(left half of anArray) and
maxArray(right half of anArray)


In the call tree it looks like this:
1st level:
maxArray(<1,6,8,3>)
return max(maxArray(<1,6>), maxArray(<8,3>))

2nd level
maxArray(<1,6>) maxArray(<8,3>)

return max(maxArray(<1), maxArray(<6>)) return max(maxArray(<8), maxArray(<3>))

3rd level
maxArray(<1>) maxArray(<6>) maxArray(<8>) maxArray(<3)

I am suppose to write the helper function max. I have no idea how to get started. Any help on this would be much appreciated. Thank you.

you can see more info an call tree at this link:http://www.cs.wm.edu/~debbie/cs241/r...recursion.html


#12 searching an array
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: help with recursion

 
0
  #2
Sep 13th, 2005
You don't necessarily need to make a max function, you could actually use a define like the following:
  1. #define MAX(a, b) ((a) > (b) ? (a) : (b))
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: help with recursion

 
0
  #3
Sep 13th, 2005
Hi,
Following should work fine


  1. int max(int *array, int len)
  2. {
  3. int n1,n2;
  4. if(len == 1) return array[0] ;
  5.  
  6.  
  7. n1 = max(array , len/2);
  8. n2 = max(array + len/2 , len - len/2) ;
  9. return (n1 > n2 ? n1 : n2) ;
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15. int A[] = {2,7,91,8,9,11} ;
  16. int B[] = {2,7,91,8,9} ;
  17.  
  18. printf("\n%d ",max(&A[0], 6));
  19. printf("\n%d\n ",max(&B[0], 5));
  20. }
<< moderator edit: added [code][/code] tags >>
cheers,
aj.wh.ca

-------------------------------------------
www.swiftthoughts.com
-------------------------------------------
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 20
Reputation: skeet123 is an unknown quantity at this point 
Solved Threads: 0
skeet123 skeet123 is offline Offline
Newbie Poster

Re: help with recursion

 
0
  #4
Sep 13th, 2005
Thanks both of you for your reply.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC