fork download
  1. #include <stdio.h>
  2.  
  3. //構造体Body
  4. struct Body {
  5. int id;
  6. int weight;
  7. int height;
  8. };
  9.  
  10. //swap関数 2つのBody構造体の内容を入れ替える
  11. void swap(struct Body *x, struct Body *y) {
  12. struct Body temp;
  13. temp = *x;
  14. *x = *y;
  15. *y = temp;
  16. }
  17.  
  18. int main(void) {
  19. int i, j;
  20.  
  21. //構造体配列aの初期化
  22. struct Body a[5] = {
  23. {1, 65, 169},
  24. {2, 73, 170},
  25. {3, 59, 161},
  26. {4, 79, 175},
  27. {5, 55, 168}
  28. };
  29.  
  30. //身長を逆順にする a[i]の身長がa[j]より低い場合,swap関数で入れ替える
  31. for (i = 0; i < 4; i++) {
  32. for (j = i + 1; j < 5; j++) {
  33. if (a[i].height < a[j].height) {swap(&a[i], &a[j]);
  34. }}}
  35.  
  36. for (i = 0; i < 5; i++) {
  37. printf("%d. %d. %d\n", a[i].id, a[i].weight, a[i].height);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
4. 79. 175
2. 73. 170
1. 65. 169
5. 55. 168
3. 59. 161