How to write a recursive function that return the largest (max) value of int array in C

1 Answer

0 votes
#include <stdio.h> 

#ifndef max
	#define max(a, b) ( ((a) > (b)) ? (a) : (b) )
#endif

int recursiveMaxValue(int arr[], int n);
  
int main(void)
{
    int numbers[6] = { 10, 40, 60, 30, 20, 50 };
 
    printf("%d\n", recursiveMaxValue(numbers, sizeof(numbers)/sizeof(int))); // 60
    
    return 0;
}
int recursiveMaxValue(int arr[], int n)
{
    if (n == 1)
        return arr[0];
 
    return max(recursiveMaxValue(arr, n - 1), arr[n - 1]);
}
 

 
/*
run:
  
60
 
*/
 


answered Feb 28, 2015 by avibootz

Related questions

...