Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by printing a special message.
CODE::::
#include <stdio.h>
int main()
{
int size = 10;
int array[size];
int firstMax , secondMax;
int i;
printf("Array data:\n");
for(i=0;i<size;i++) {
scanf("%d",&array[i]);
}
firstMax = array[0] , secondMax = array[1];
for(i=0;i<size;i++) {
if (array[i] > firstMax) {
if(firstMax!=array[i]) {
secondMax = firstMax;
}
firstMax = array[i];
} else if (array[i] > secondMax) {
if(firstMax!=array[i]) {
secondMax = array[i];
}
}
}
printf("Maximum value: %d\n", firstMax);
if(firstMax == secondMax) {
printf("All the values are equal\n");
} else {
printf("Second largest value: %d\n", secondMax);
}
return 0;
}
OUTPUT::::
OUTPUT::::
Get Answers For Free
Most questions answered within 1 hours.