fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct {
  5. int id;
  6. double height;
  7. double weight;
  8. } Body;
  9.  
  10. int main(void) {
  11. Body data[5] = {
  12. {1, 165, 60},
  13. {2, 170, 68},
  14. {3, 160, 50},
  15. {4, 180, 75},
  16. {5, 175, 80}
  17. };
  18.  
  19. int n = 5;
  20.  
  21. // 身長の低い順にソート(単純交換法)
  22. for (int i = 0; i < n - 1; i++) {
  23. for (int j = i + 1; j < n; j++) {
  24. if (data[i].height > data[j].height) {
  25. Body temp = data[i];
  26. data[i] = data[j];
  27. data[j] = temp;
  28. }
  29. }
  30. }
  31.  
  32. // 並べ替え後の表示
  33. printf("身長の低い順に並べ替えたデータ:\n");
  34. for (int i = 0; i < n; i++) {
  35. printf("id:%d height:%.0f weight:%.0f\n", data[i].id, data[i].height, data[i].weight);
  36. }
  37.  
  38. // 下から3名(身長が高い3人)
  39. double sum = 0.0;
  40. for (int i = n - 3; i < n; i++) {
  41. sum += data[i].height;
  42. }
  43. double ave = sum / 3.0;
  44.  
  45. double var = 0.0;
  46. for (int i = n - 3; i < n; i++) {
  47. var += pow(data[i].height - ave, 2);
  48. }
  49. double std = sqrt(var / 3.0);
  50.  
  51. printf("\n下から3名(身長が高い3名)の平均身長:%.1f cm\n", ave);
  52. printf("標準偏差:%.1f cm\n", std);
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 5320KB
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

下から3名(身長が高い3名)の平均身長:175.0 cm
標準偏差:4.1 cm