fork download
  1. #include <stdio.h>
  2. #include <math.h> // sqrt関数を使うためにインクルード
  3.  
  4. // typedefと無名構造体でBody型を定義
  5. typedef struct {
  6. int id;
  7. int height;
  8. int weight;
  9. } Body;
  10.  
  11. int main() {
  12. int n = 5; // メンバー数
  13.  
  14. // 構造体配列の宣言と初期化
  15. Body data[5] = {
  16. {1, 165, 60},
  17. {2, 170, 68},
  18. {3, 160, 50},
  19. {4, 180, 75},
  20. {5, 175, 80}
  21. };
  22.  
  23. // 身長の低い順に整列(バブルソート)
  24. for (int i = 0; i < n - 1; i++) {
  25. for (int j = 0; j < n - 1 - i; j++) {
  26. if (data[j].height > data[j + 1].height) {
  27. Body temp = data[j];
  28. data[j] = data[j + 1];
  29. data[j + 1] = temp;
  30. }
  31. }
  32. }
  33.  
  34. // 整列結果の表示
  35. printf("身長の低い順:\n");
  36. for (int i = 0; i < n; i++) {
  37. printf("ID: %d, Height: %d, Weight: %d\n", data[i].id, data[i].height, data[i].weight);
  38. }
  39.  
  40. // 身長が高いメンバー3名の平均と標準偏差を計算
  41. double sum = 0.0;
  42. for (int i = n - 3; i < n; i++) {
  43. sum += data[i].height;
  44. }
  45. double ave = sum / 3.0;
  46.  
  47. double variance = 0.0;
  48. for (int i = n - 3; i < n; i++) {
  49. variance += pow(data[i].height - ave, 2);
  50. }
  51. double std = sqrt(variance / 3.0);
  52.  
  53. // 結果の表示(小数1桁まで)
  54. printf("\n平均身長: %.1f cm\n", ave);
  55. printf("標準偏差: %.1f cm\n", std);
  56.  
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
身長の低い順:
ID: 3, Height: 160, Weight: 50
ID: 1, Height: 165, Weight: 60
ID: 2, Height: 170, Weight: 68
ID: 5, Height: 175, Weight: 80
ID: 4, Height: 180, Weight: 75

平均身長: 175.0 cm
標準偏差: 4.1 cm