Skip to content

Instantly share code, notes, and snippets.

@KeitetsuWorks
Last active March 6, 2021 04:20
Show Gist options
  • Select an option

  • Save KeitetsuWorks/ca854cfc0dbce6ca0123ce678d2e7452 to your computer and use it in GitHub Desktop.

Select an option

Save KeitetsuWorks/ca854cfc0dbce6ca0123ce678d2e7452 to your computer and use it in GitHub Desktop.
n人の学生の身長データをキーボードから入力し,平均値,最大値および最小値を求めるプログラム
/**
* @file q13182776018.c
* @brief n人の学生の身長データをキーボードから入力し,平均値,最大値および最小値を求めるプログラム
* @author Keitetsu
* @date 2017/12/01
* @copyright Copyright (c) 2017 Keitetsu
* @par License
* This software is released under the MIT License.
*/
#include <stdio.h>
#define STUDENT_NUM 10 /**< 学生の人数 */
double dave(double *d, int dNum);
double dmax(double *d, int dNum);
double dmin(double *d, int dNum);
int main(void)
{
double d[STUDENT_NUM];
double dAve, dMax, dMin;
int i;
for (i = 0; i < STUDENT_NUM; i++) {
printf("%2d / %2d 人目の身長データ (cm): ", (i + 1), STUDENT_NUM);
scanf("%lf", &d[i]);
}
dAve = dave(d, STUDENT_NUM);
dMax = dmax(d, STUDENT_NUM);
dMin = dmin(d, STUDENT_NUM);
printf("平均身長: %6.2f (cm)\n", dAve);
printf("最大身長: %6.2f (cm)\n", dMax);
printf("最小身長: %6.2f (cm)\n", dMin);
return 0;
}
double dave(double *d, int dNum)
{
double dSum, dAve;
int i;
dSum = 0.0;
for (i = 0; i < dNum; i++) {
dSum += d[i];
}
dAve = dSum / dNum;
return dAve;
}
double dmax(double *d, int dNum)
{
double dMax;
int i;
dMax = d[0];
for (i = 1; i < dNum; i++) {
if (d[i] > dMax) {
dMax = d[i];
}
}
return dMax;
}
double dmin(double *d, int dNum)
{
double dMin;
int i;
dMin = d[0];
for (i = 1; i < dNum; i++) {
if (d[i] < dMin) {
dMin = d[i];
}
}
return dMin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment