Last active
December 26, 2023 06:34
-
-
Save CraigRodrigues/2bfa9fdd1bc6624b50620cc8bbca60a8 to your computer and use it in GitHub Desktop.
Finding Max Value with Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
// find the max number in a list/array | |
// n is the last index of the array | |
int arr_max(int arr[], int max, int n) | |
{ | |
//base case | |
if (n == 0) | |
{ | |
if (arr[n] > max) | |
max = arr[n]; | |
return max; | |
} | |
else | |
{ | |
if (arr[n] > max) | |
max = arr[n]; | |
return arr_max(arr, max, n-1); | |
} | |
} | |
int main(void) { | |
// Disable stdout buffering | |
setvbuf(stdout, NULL, _IONBF, 0); | |
int arr[] = {33,2,3,11,5,6,7,8,9}; | |
int max = 0; | |
max = arr_max(arr, max, 8); | |
printf("\nMax is: %d\n", max); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the website is going to die.