0

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

2
  • a[n] is also out of bounds. Commented May 22, 2016 at 4:27
  • In other words, all arrays in C are zero based so for(i=1;i<=n;i++) should be for(i=0;i<n;i++). You must also validate the return of scanf each time it is called. Commented May 22, 2016 at 4:37

3 Answers 3

4
max=a[0]=0;

This sets both a[0] and max to 0.

The loop in main() populates the array starting with index 1. Setting a[0] to 0 here accomplishes absolutely nothing. That, in itself, is not a problem, but setting max to 0 is. This is because if all elements in the array that main() initializes are negative, then because max is initially set to 0, the returned maximum value will be 0 instead of the highest negative value in the array.

You should make the following changes.

1) Instead of populating x[1] through x[k], the loop in main() should populate x[0] through x[k-1]. Arrays in C and C++ are, traditionally, 0-based.

2) The loop in your function should be adjusted accordingly as well, and it should not set a[0] to 0, of course. It should simply set max=a[0].

Sign up to request clarification or add additional context in comments.

1 Comment

Starting the array from 1 was my mistake, and yes assigning 0 to max was too. It works fine now. kudos!
0
#include<stdio.h>
int findMax(int i,int *a)
{
     max=a[0]; // you were setting a[0] to zero
     for(i=1;i<n;i++) // there are n numbers beginning from position 0 
     {
       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=0;i<k;i++)
  {
  scanf("%d",&x[i]);
  }
  max=findMax(k,x);
  printf("The maximum element in the array is %d",max);
  return 0;
}

Comments

0

In your code, you have set max=0. Consider the test case when all the numbers are negative, then your code will return 0 which is not correct.

Correct Approach: Assign max to first element of array and check the condition for remaining elements. If first element is maximum then it will be returned otherwise the updated maximum is returned (after being checked through loop).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.