Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/max.c
Last active Jun 13, 2016

Embed
What would you like to do?
Finding Max Value with Recursion
#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
You can’t perform that action at this time.