fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9. void swap(Body *a, Body *b) {
  10. Body temp = *a;
  11. *a = *b;
  12. *b = temp;
  13. }
  14.  
  15. void display(Body arr[], int size) {
  16. for (int i = 0; i < size - 1; i++) {
  17. for (int j = 0; j < size - i - 1; j++) {
  18. if (arr[j].height < arr[j + 1].height) {
  19. swap(&arr[j], &arr[j + 1]);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. int main() {
  26. Body bodies[] = {
  27. {1, 65, 169},
  28. {2, 73, 170},
  29. {3, 59, 161},
  30. {4, 79, 175},
  31. {5, 55, 168}
  32. };
  33. int size = sizeof(bodies) / sizeof(bodies[0]);
  34. display(bodies, size);
  35.  
  36. for (int i = 0; i < size; i++) {
  37. printf("%d, %d, %d\n", bodies[i].id, bodies[i].weight, bodies[i].height);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
  43.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
4, 79, 175
2, 73, 170
1, 65, 169
5, 55, 168
3, 59, 161