C program to read n integer numbers into an array and find the average of largest two of the given numbers without sorting the array
Answers
Explanation:
program to read n integer numbers into an array and find the average of largest two of the given numbers without sorting
Answer:Below is the program
Explanation:
#include <stdio.h>
#include <conio.h>
#define MAX 4
void main()
{
int a[MAX], i, l1,l2,temp;
clrscr();
printf("Enter %d integer numbersn", MAX);
for (i=0; i < MAX; i++)
{
scanf("%d", &a[i]);
}
printf("Input interger aren");
for (i=0; i < MAX; i++)
{
printf("%5d", a[i]);
}
printf("n");
l1 = a[0]; /*assume first element of array is the first largest*/
l2 = a[1]; /*assume first element of array is the second largest*/
if (l1 < l2)
{
temp = l1;
l1 = l2;
l2 = temp;
}
for (i=2;i<4;i++)
{
if (a[i] >= l1)
{
l2 = l1;
l1 = a[i];
}
else if(a[i] > l2)
{
l2= a[i];
}
}
printf("n%d is the first largestn", l1);
printf("%d is the second largestn", l2);
printf("nAverage of %d and %d = %dn", l1,l2, (l1+l2)/2);
}