Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Last active December 26, 2023 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/2bfa9fdd1bc6624b50620cc8bbca60a8 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/2bfa9fdd1bc6624b50620cc8bbca60a8 to your computer and use it in GitHub Desktop.
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;
}
@kunal101994
Copy link

the website is going to die.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment