Average of Largest Two Numbers

QUESTION

Program to find the largest two numbers from n elements in an array and then calculate and display their average.

“TESTCASE_1”: “5\n2 5 6 4 7\n###—###SEPERATOR—###—\n6.5”, “TESTCASE_2”: “7\n2 5 6 4 7 10 4\n###—###SEPERATOR—###—\n8.5”, “TESTCASE_3”: “4\n50 4 10 2\n###—###SEPERATOR—###—\n30”, “TESTCASE_4”: “2\n10 20\n###—###SEPERATOR—###—\n15”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
int main()
{
int n;
  scanf("%d",&n);
  int arr[n];
  int i,j;
  for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
    if(arr[i]>arr[j])
       {
         int temp=arr[i];
      arr[i]=arr[j];
      arr[j]=temp;
    }
    }
  }
  float sum=arr[n-1]+arr[n-2];
  printf("%.1f",sum/2);
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro