I would like a review for this simple recursive algorithm which returns the largest int in an array of integers. I would like feedback on the transfer of state via the third parameter specifically. Please also note 2 things:
- It must be recursive
- This is not for production usage, but rather recursion practice
#include <stdio.h>
#include <stdlib.h>
int max_val(int arr[], int length, int curr_max)
{
if(length <= 0)
return curr_max;
if(curr_max < *arr)
{
curr_max = *arr;
}
arr++;
return max_val(arr,length-1,curr_max);
}
int main()
{
int arr[] = {2,73,39,5,8,62,9};
printf("Max: %d\n", max_val(arr,7,0));
return EXIT_SUCCESS;
}
arr[] = {-1, -2, -3}\$\endgroup\$