Last active
December 19, 2015 19:18
-
-
Save chomado/6005214 to your computer and use it in GitHub Desktop.
課題1。整数データを入力して、平均値、最小値、最大値を表示するCのプログラムを書いてください。 整数データの範囲は0以上1000以下とします。 ファイル名はaverage.cとしてください
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 課題1 */ | |
/* 整数データを入力して、平均値、最小値、最大値を表示するCのプログラムを書いてください。 整数データの範囲は0以上1000以下とします。 ファイル名はaverage.cとしてください。*/ | |
#include <stdio.h> | |
#include <math.h> | |
int main() { | |
int n, x; | |
int min = 1000, max = 0, count = 0; | |
double ave, sum = 0; | |
while(scanf("%d", &x)!=EOF && (x >= 0 && x <= 1000)) { | |
max = fmax (x, max); | |
min = fmin (x, min); | |
sum += x; | |
count++; | |
} | |
ave = sum / count; | |
printf("平均値: %.1f; 最小値: %d; 最大値: %d;\n", ave, min, max); | |
return 0; | |
} | |
/* 実行例 | |
% ./average | |
20 | |
40 | |
45 | |
33 | |
49 | |
平均値: 37.4; 最小値: 20; 最大値: 49; | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cの標準ライブラリ(math.h)に大小比較の関数が既にあるので、それを使うといいと思います。
http://www.cplusplus.com/reference/cmath/fmax/
http://www.cplusplus.com/reference/cmath/fmin/