fork(2) 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. /* 構造体配列の初期化 */
  18. Body data[5] = { {1, 65, 169},{2, 73, 170},{3, 59, 161},{4, 79, 175},{5, 55, 168} };
  19.  
  20. int i, j;
  21.  
  22. /* 身長の降順で並び替え */
  23. for (i = 0; i < 5 - 1; i++) {
  24. for (j = i + 1; j < 5; j++) {
  25. if (data[i].height < data[j].height) {
  26. swap(&data[i], &data[j]);
  27. }
  28. }
  29. }
  30.  
  31. /* 結果の出力 */
  32. for (i = 0; i < 5; i++) {
  33. printf("%d, %d, %d\n",
  34. data[i].id,
  35. data[i].weight,
  36. data[i].height);
  37. }
  38.  
  39. return 0;
  40. }
  41.  
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