943,935 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2275
  • C RSS
Sep 13th, 2005
0

help with recursion

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
skeet123 is offline Offline
20 posts
since Nov 2004
Sep 13th, 2005
0

Re: help with recursion

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))
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 13th, 2005
0

Re: help with recursion

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 >>
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
aj.wh.ca is offline Offline
53 posts
since Mar 2005
Sep 13th, 2005
0

Re: help with recursion

Thanks both of you for your reply.
Reputation Points: 10
Solved Threads: 0
Unverified User
skeet123 is offline Offline
20 posts
since Nov 2004

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: procedure and object-oriented !
Next Thread in C Forum Timeline: Help me with this Simple Problem plss





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


Follow us on Twitter


© 2011 DaniWeb® LLC