arrays_ex3.c (1135B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define ARRLEN(x) (sizeof(x) / sizeof(x[0])) 5 6 /* Modify arrays_ex2.c to get the array's elements from stdin */ 7 8 int 9 main(int argc, char *argv[]) 10 { 11 float sum, avg; 12 int arr[10], i; 13 int max, min, maxloc, minloc; 14 15 sum = 0; 16 for (i = 0; i < ARRLEN(arr); i++) { 17 do { 18 printf("arr[%d]: ", i); 19 scanf("%d", &arr[i]); 20 getchar(); 21 } while (arr[i] < 0 || arr[i] > 100); 22 23 sum += arr[i]; 24 } 25 26 max = min = arr[0]; 27 maxloc = minloc = 0; 28 for (i = 0; i < ARRLEN(arr); i++) { 29 if (arr[i] < min) { 30 min = arr[i]; 31 minloc = i; 32 } 33 if (arr[i] > max) { 34 max = arr[i]; 35 maxloc = i; 36 } 37 } 38 39 avg = sum / (float)ARRLEN(arr); 40 printf("max: %d | maxloc: %d | min: %d | minloc: %d | avg: %.2f\n", 41 max, maxloc, min, minloc, avg); 42 43 return 0; 44 }