Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define N 100
- static int a[N] = {};
- static int input(void) {
- puts("整数を入力して下さい。(0で終了)");
- int n = 0, e;
- for (int s = scanf("%d", &e); s == 1 && e != 0; s = scanf("%d", &e)) {
- if (N <= n) {
- exit(1);
- }
- a[n++] = e;
- }
- return n;
- }
- #undef N
- static double avg(int n) {
- double sum = 0.;
- for (int i = 0; i < n; ++i) {
- sum += a[i];
- }
- return sum / n;
- }
- static int max(int n) {
- int m = a[0];
- for (int i = 1; i < n; ++i) {
- if (m < a[i]) {
- m = a[i];
- }
- }
- return m;
- }
- static int min(int n) {
- int m = a[0];
- for (int i = 1; i < n; ++i) {
- if (a[i] < m) {
- m = a[i];
- }
- }
- return m;
- }
- int main(void) {
- int n = input();
- if (n < 1) {
- exit(1);
- }
- {
- printf("入力データ数: %d\n", n);
- printf("元データ: ");
- for (int i = 0; i < n; ++i) {
- printf("%s%d", (i == 0 ? "" : " "), a[i]);
- }
- puts("");
- }
- { printf("平均値:%.1f 最大値:%d 最小値:%d\n", avg(n), max(n), min(n)); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement