Min, Max, Average
Completed
Read n integers and print the minimum, maximum and average (2 decimals).
Input
Line 1: n. Line 2: n integers.
Output
Three lines.
Example
Input
5 3 9 -2 7 1
Output
-2 9 3.60
Editor
C
Hints
Start min and max at the first element.
Solution explanation
One pass over the array is enough for all three statistics.
Show solution
#include <stdio.h>
int main() {
int n, a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
int min = a[0], max = a[0], sum = 0;
for (int i = 0; i < n; i++) {
if (a[i] < min) min = a[i];
if (a[i] > max) max = a[i];
sum += a[i];
}
printf("%d\n%d\n%.2f\n", min, max, (double) sum / n);
return 0;
}