fork(1) download
  1. #include <stdio.h>
  2.  
  3. /* 構造体の定義 */
  4. typedef struct {
  5. int id;
  6. int weight;
  7. int height;
  8. } Body;
  9.  
  10. /* swap関数(構造体用) */
  11. void swap(Body *a, Body *b) {
  12. Body temp = *a;
  13. *a = *b;
  14. *b = temp;
  15. }
  16.  
  17. int main(void) {
  18. /* 構造体配列の初期化 */
  19. Body data[5] = {
  20. {1, 65, 169},
  21. {2, 73, 170},
  22. {3, 59, 161},
  23. {4, 79, 175},
  24. {5, 55, 168}
  25. };
  26.  
  27. int i, j;
  28.  
  29. /* 身長の降順で並び替え */
  30. for (i = 0; i < 5 - 1; i++) {
  31. for (j = i + 1; j < 5; j++) {
  32. if (data[i].height < data[j].height) {
  33. swap(&data[i], &data[j]);
  34. }
  35. }
  36. }
  37.  
  38. /* 結果の出力 */
  39. for (i = 0; i < 5; i++) {
  40. printf("%d, %d, %d\n",
  41. data[i].id,
  42. data[i].weight,
  43. data[i].height);
  44. }
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
4, 79, 175
2, 73, 170
1, 65, 169
5, 55, 168
3, 59, 161