The program must give the largest element of an array(A) with the help of recursive void TMax.But it's not working.I think that there's an error because of pointers of Max and I can't correct it.Can you help me,please?
#include <stdio.h>
#include <stdlib.h>
void TMax(int A[], int N,int *Max)
{
if(N==0){
*Max=A[0];
}
else
{
*Max=A[N];
if(A[N]>*Max)
{
*Max=A[N];
}
TMax(A,N-1,*Max);
}
}
int main()
{
int A[] = { 1, 2, 999, 4, 20};
int N = sizeof(A) / sizeof(A[0]);
int k=A[N];
TMax(A,N,&k);
printf("%d",k);
}
A[N]leads to undefined behavior since you are accessing beyond the arrayAlimitTMax(A,N-1,*Max);should beTMax(A,N-1,Max);Andint k=A[N];should beint k=A[N-1];int k = A[N]is array out of range, the acceptable range is [0 , N-1]