I'm trying to find the index of the biggest number in array, by using a recursive function, but it doesn't work for me.
I wrote this code in "Online C Complier":
#include <stdio.h>
int max(int arr[], int n){
if (n==0) {
return 0;
}
int temp = max(arr, n-1);
if (arr[temp] > arr[n]) {
return temp;
}
else {
return n;
}
}
int main()
{
int arr[] = {20,2,44,6,1,15,25,40};
printf("The index is: %d\n", max(arr, 8));
return 0;
}
The out put is sometimes 8
which is wrong and sometimes 2
which is correct.
thanks u all!