fork download
  1. #include <stdio.h>
  2. #define ID 5
  3.  
  4. typedef struct{
  5. int id,weight,height;
  6. }Body;
  7.  
  8. void swap(Body *a,Body *b);//*があることでa[]の中身をいじってる
  9. //*がないとa[]のコピーを作りいじることになりa[]の中身をいじることはできない
  10.  
  11. int main(void) {
  12. Body a[]={
  13. {1,65,169},{2,73,170},{3,59,161},{4,79,175},{5,55,168}
  14. };
  15. for(int i=0;i<ID-1;i++){
  16. for(int j=0;j<ID-1-i;j++){
  17. if(a[j].height<a[j+1].height){
  18. swap(&a[j],&a[j+1]);// 住所を渡して中身を入れ替える
  19. }
  20. }
  21. }
  22.  
  23. for(int n=0;n<ID;n++){
  24. printf("%d,%d,%d\n",a[n].id,a[n].weight,a[n].height);
  25. }
  26. return 0;
  27. }
  28.  
  29. void swap(Body *a,Body *b){
  30. Body g=*a;
  31. *a=*b;
  32. *b=g;
  33. }
  34.  
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
4,79,175
2,73,170
1,65,169
5,55,168
3,59,161