Advertisement
cd62131

statistics

Jan 14th, 2019
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 100
  4. static int a[N] = {};
  5. static int input(void) {
  6.   puts("整数を入力して下さい。(0で終了)");
  7.   int n = 0, e;
  8.   for (int s = scanf("%d", &e); s == 1 && e != 0; s = scanf("%d", &e)) {
  9.     if (N <= n) {
  10.       exit(1);
  11.     }
  12.     a[n++] = e;
  13.   }
  14.   return n;
  15. }
  16. #undef N
  17. static double avg(int n) {
  18.   double sum = 0.;
  19.   for (int i = 0; i < n; ++i) {
  20.     sum += a[i];
  21.   }
  22.   return sum / n;
  23. }
  24. static int max(int n) {
  25.   int m = a[0];
  26.   for (int i = 1; i < n; ++i) {
  27.     if (m < a[i]) {
  28.       m = a[i];
  29.     }
  30.   }
  31.   return m;
  32. }
  33. static int min(int n) {
  34.   int m = a[0];
  35.   for (int i = 1; i < n; ++i) {
  36.     if (a[i] < m) {
  37.       m = a[i];
  38.     }
  39.   }
  40.   return m;
  41. }
  42. int main(void) {
  43.   int n = input();
  44.   if (n < 1) {
  45.     exit(1);
  46.   }
  47.   {
  48.     printf("入力データ数: %d\n", n);
  49.     printf("元データ: ");
  50.     for (int i = 0; i < n; ++i) {
  51.       printf("%s%d", (i == 0 ? "" : " "), a[i]);
  52.     }
  53.     puts("");
  54.   }
  55.   { printf("平均値:%.1f 最大値:%d 最小値:%d\n", avg(n), max(n), min(n)); }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement