| | |
help with recursion
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 20
Reputation:
Solved Threads: 0
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
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
You don't necessarily need to make a max function, you could actually use a define like the following:
C Syntax (Toggle Plain Text)
#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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
•
•
Join Date: Mar 2005
Posts: 53
Reputation:
Solved Threads: 1
Hi,
Following should work fine
<< moderator edit: added [code][/code] tags >>
Following should work fine
C Syntax (Toggle Plain Text)
int max(int *array, int len) { int n1,n2; if(len == 1) return array[0] ; n1 = max(array , len/2); n2 = max(array + len/2 , len - len/2) ; return (n1 > n2 ? n1 : n2) ; } int main() { int A[] = {2,7,91,8,9,11} ; int B[] = {2,7,91,8,9} ; printf("\n%d ",max(&A[0], 6)); printf("\n%d\n ",max(&B[0], 5)); }
cheers,
aj.wh.ca
-------------------------------------------
www.swiftthoughts.com
-------------------------------------------
aj.wh.ca
-------------------------------------------
www.swiftthoughts.com
-------------------------------------------
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C Forum
- Previous Thread: procedure and object-oriented !
- Next Thread: Help me with this Simple Problem plss
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming stack standard string systemcall testing threads turboc unix user voidmain() wab windows.h





