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. for (int i = 0; i < n - 1; i++) {
  22. for (int j = i + 1; j < n; j++) {
  23. if (data[i].height > data[j].height) {
  24. Body temp = data[i];
  25. data[i] = data[j];
  26. data[j] = temp;
  27. }
  28. }
  29. }
  30.  
  31. printf("身長の低い順に並び替えたデータ:\n");
  32. for (int i = 0; i < n; i++) {
  33. printf("id:%d height:%.0f weight:%.0f\n", data[i].id, data[i].height, data[i].weight);
  34. }
  35. double sum = 0.0;
  36. for (int i = n - 3; i < n; i++) {
  37. sum += data[i].height;
  38. }
  39. double ave = sum / 3.0;
  40.  
  41. double v = 0.0;
  42. for (int i = n - 3; i < n; i++) {
  43. v += pow(data[i].height - ave, 2);
  44. }
  45. double std = sqrt(v / 3.0);
  46.  
  47. printf("\n下から3名(身長が高い3名)の平均身長: %.1f cm\n", ave);
  48. printf("標準偏差: %.2f cm\n", std);
  49. return 0;
  50. }
  51.  
  52.  
  53.  
Success #stdin #stdout 0.01s 5304KB
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.08 cm