04 recursive_max in C (#278)

This commit is contained in:
gweno
2024-03-22 21:04:27 +01:00
committed by GitHub
parent 11a2de7473
commit cfb59254fb

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
int max(int *arr, int index, int size){
//base case
if (index == size - 1)
return arr[index];
int sub_max = max(arr, index + 1, size);
return (arr[index] > sub_max) ? arr[index] : sub_max;
}
int main(void){
int arr[] = {2,3,6,5,5};
// the C way of calculating the size of an array
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d", max(arr, 0, size));
return 0;
}