c - Why is my function not calling itself again when it is finished (recursion)? -
i finished code. reason recursive phone call @ end isn't beingness triggered? there sort of special code missing chance?
int max(int arr[], int start, int end) { int greatest = arr[start]; if(start < end) { if(greatest<arr[start]) { greatest = arr[start]; } start++; max(arr, start, end); // doesn't seem triggering since returns 8 } homecoming greatest; } int main() { int greatest; int arr[10] = {8,1,2,3,4,5,6,7,8,9}; int start = 0; int end = 10; greatest=max(arr, start, end); pintf("%d\n", greatest); }
your algorithm little messed up. instance, greatest<arr[start] never true, because set greatest = arr[start]. here's commented working algorithm:
#include <stdio.h> int max(int arr[], int start, int end) { if ( start >= end ) { /* shouldn't here, homecoming 0 if */ homecoming 0; } else if ( end - start == 1 ) { /* 1 element, it's maximum definiton */ homecoming arr[start]; } else { /* find maximum of rest of list... */ int greatest = max(arr, start + 1, end); /* ...and homecoming greater of it, , first element */ homecoming arr[start] > greatest ? arr[start] : greatest; } } int main(void) { int arr[] = { 8, 1, 2, 3, 12, 5, 6, 7, 8, 9 }; int start = 0; int end = 10; int greatest = max(arr, start, end); printf("%d\n", greatest); } with output:
paul@thoth:~/src/sandbox$ ./max 12 paul@thoth:~/src/sandbox$ let's go through simple list, {1, 5, 3}. each indentation level represents 1 of recursive function calls. every time see max(), go level - every time see return, go level.
in main(), int greatest = max(list, 0, 3) in phone call 1: (end - start == 1) false in phone call 1: int greatest = max(list, 1, 3) in phone call 2: (end - start == 1) false in phone call 2: int greatest = max(list, 2, 3) in phone call 3: (end - start == 1) true in phone call 3: homecoming arr[2], 3 in phone call 2: greatest equals 3 in phone call 2: arr[1] == 5, > 3, homecoming arr[1], 5 in phone call 1: greatest equals 5 in phone call 1: arr[0] == 1, not > 5, homecoming greatest, 5 in main(), greatest equals 5 remember, every time see int greatest, that's different variable that's beingness created. have same name, since they're in separate scopes, they're still different. int greatest in phone call 2, instance, separate int greatest in phone call 1, int greatest in phone call 1 separate int greatest in main().
edit: comments, if want index of maximum value well, this'll it:
#include <stdio.h> int max_index(int arr[], int start, int end) { if ( start >= end ) { homecoming 0; } else if ( end - start == 1 ) { homecoming start; } else { int greatest = max_index(arr, start + 1, end); homecoming arr[start] > arr[greatest] ? start : greatest; } } int max(int arr[], int start, int end) { if ( start >= end ) { /* shouldn't here, homecoming 0 if */ homecoming 0; } else if ( end - start == 1 ) { /* 1 element, it's maximum definiton */ homecoming arr[start]; } else { /* find maximum of rest of list... */ int greatest = max(arr, start + 1, end); /* ...and homecoming greater of it, , first element */ homecoming arr[start] > greatest ? arr[start] : greatest; } } int main(void) { int arr[] = { 8, 1, 2, 3, 12, 5, 6, 7, 8, 9 }; int start = 0; int end = 10; int greatest = max(arr, start, end); int index = max_index(arr, start, end); printf("greatest value %d @ index %d\n", greatest, index); } output:
paul@thoth:~/src/sandbox$ ./max greatest value 12 @ index 4 paul@thoth:~/src/sandbox$ c
No comments:
Post a Comment