fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9. /* 構造体の入れ替え用 swap 関数 */
  10. void swap(Body *a, Body *b) {
  11. Body temp = *a;
  12. *a = *b;
  13. *b = temp;
  14. }
  15.  
  16. int main(void) {
  17. Body body[5] = {
  18. {1, 65, 169},
  19. {2, 73, 170},
  20. {3, 59, 161},
  21. {4, 79, 175},
  22. {5, 55, 168}
  23. };
  24.  
  25. int i, j;
  26.  
  27. /* 身長の降順に並び替え */
  28. for (i = 0; i < 4; i++) {
  29. for (j = i + 1; j < 5; j++) {
  30. if (body[i].height < body[j].height) {
  31. swap(&body[i], &body[j]);
  32. }
  33. }
  34. }
  35.  
  36. /* 出力 */
  37. for (i = 0; i < 5; i++) {
  38. printf("%d, %d, %d\n",
  39. body[i].id,
  40. body[i].weight,
  41. body[i].height);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
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