Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active December 19, 2015 19:18
Show Gist options
  • Save chomado/6005214 to your computer and use it in GitHub Desktop.
Save chomado/6005214 to your computer and use it in GitHub Desktop.
課題1。整数データを入力して、平均値、最小値、最大値を表示するCのプログラムを書いてください。 整数データの範囲は0以上1000以下とします。 ファイル名はaverage.cとしてください
/* 課題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;
*/
@func-hs
Copy link

func-hs commented Jul 16, 2013

Cの標準ライブラリ(math.h)に大小比較の関数が既にあるので、それを使うといいと思います。
http://www.cplusplus.com/reference/cmath/fmax/
http://www.cplusplus.com/reference/cmath/fmin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment