Skip to content

Instantly share code, notes, and snippets.

@KeitetsuWorks
Last active May 29, 2018 14:38
Show Gist options
  • Save KeitetsuWorks/66fdfd4a497c3234db968a33914aa475 to your computer and use it in GitHub Desktop.
Save KeitetsuWorks/66fdfd4a497c3234db968a33914aa475 to your computer and use it in GitHub Desktop.
実数を降順に並べ、平均値以上の値を表示するプログラム
/**
* @file q11175050116.c
* @brief 実数を降順に並べ、平均値以上の値を表示するプログラム
* @author Keitetsu
* @date 2017/06/10
* @copyright Copyright (c) 2017 Keitetsu
*/
#include <stdio.h>
#define X_NUM 10 /**< 入力値の数 */
int main(void)
{
double x[X_NUM], total, average, tmp;
int i, j;
/* 入力値格納と合計値算出 */
total = 0.0;
for (i = 0; i < X_NUM; i++) {
printf("x[%d] = ", i);
scanf("%lf", &x[i]);
total += x[i];
}
/* 平均値算出 */
average = total / X_NUM;
printf("平均値 = %f\n", average);
/* 降順ソート */
for (i = 0; i < (X_NUM - 1); i++) {
for (j = i + 1; j < X_NUM; j++) {
if (x[i] < x[j]) { /* x[i]がx[j]よりも小さい場合 */
tmp = x[i]; /* x[i]とx[j]をスワップ */
x[i] = x[j];
x[j] = tmp;
}
}
}
/* 平均値以上の値を降順表示 */
printf("平均値以上の入力値\n");
for (i = 0; i < X_NUM; i++) {
if (x[i] >= average) { /* x[i]が平均値以上の場合 */
printf("x[%d] = %f\n", i, x[i]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment