i'm new to posting questions here but have been looking for answers for a long time. I've created this code to return the maximum element in the array x[]. I did get results but not all the test cases are passed. I have checked it multiple times, but got no clue ion what is to be done to correct it.
I'm doing this on part of my online training so the test case values aren't openly visible. This code generated 85.71% positive outcome. Please help me do 100%. Thank you.
The question is to create a function findMax(int n,int *a) to return the maximum element. WHERE
The first argument corresponds to the number of elements in the array.
The second argument corresponds to the pointer to an array.
#include<stdio.h>
int findMax(int n,int *a)
{
int i,max=a[0]=0;
for(i=1;i<=n;i++)
{
if(a[i]>max)
max=a[i];
}
return(max);
}
int main()
{
int i,x[16],k,max;
printf("Enter the number of elements in the array\n");
scanf("%d",&k);
printf("Enter the elements in the array\n");
for(i=1;i<=k;i++)
{
scanf("%d",&x[i]);
}
max=findMax(k,x);
printf("The maximum element in the array is %d",max);
return 0;
}
I'm a beginner in coding, so a simpler explanation might help. Thanks again
a[n]is also out of bounds.for(i=1;i<=n;i++)should befor(i=0;i<n;i++). You must also validate the return ofscanfeach time it is called.