fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct list
  4. {
  5. int id;
  6. char string[20];
  7. float w;
  8. float h;
  9. float bmi;
  10. struct list *next;
  11. } ;
  12. void print(struct list *head,int t);
  13. int main()
  14. {
  15. int t,c;
  16. scanf("%d",&t);
  17. struct list *head,*ne,*last;
  18. struct list l1;
  19. head = &l1;
  20. scanf("%d %s %f %f",&l1.id,&l1.string[0],&l1.w,&l1.h);
  21. l1.bmi = l1.w/(l1.h * l1.h);
  22. for(c=2;c<=t;c++)
  23. {
  24. ne = (struct list *) malloc(sizeof(struct list));
  25. if(c==2) { l1.next = ne;}
  26. scanf("%d %s %f %f",&ne->id,&ne->string[0],&ne->w,&ne->h);
  27. ne->bmi = ne->w/(ne->h * ne->h);
  28. if( c < t )
  29. {
  30. last->next = ne;
  31. }
  32. last = ne;
  33. if(c == t)
  34. {
  35. last->next = NULL;
  36. }
  37. }
  38. print(head,t);
  39. return 0;
  40. }
  41. /*sorting and printing function*/
  42. void print(struct list *head,int t)
  43. {
  44. int c1,c2,c3;
  45. struct list *ptr,*ptr1,*ptr2,*temp1,*temp2,*temp3;
  46. struct list *pri;
  47. for(c1=1;c1<=t;c1++) /*logic correction needed*/
  48. {
  49. ptr= head;
  50. ptr1=head->next;
  51. ptr2=ptr1->next;
  52. for(c2=1;c2<=t;c2++)
  53. {
  54. temp1=ptr->next;
  55. temp2=ptr1->next;
  56. temp3=ptr2->next;
  57. if(ptr2->bmi > ptr1->bmi)
  58. {
  59. ptr1->next = temp3;
  60. ptr2->next = temp1;
  61. if(ptr == head)
  62. {ptr = temp2;}
  63. else
  64. {ptr->next = temp2;}
  65. ptr2=ptr1->next;
  66. ptr=ptr->next;
  67. }
  68. else
  69. {
  70. ptr=ptr1;
  71. ptr1=temp2;
  72. ptr2=temp3;
  73. }
  74. if(ptr1->next == NULL) break;
  75. }
  76. }
  77. /*printing*/
  78. pri=head;
  79. for(c3=1;c3<=t;c3++)
  80. {
  81. printf("%d %s %.2f %.2f %.2f",pri->id,pri->string[0],pri->w,pri->h,pri->bmi);
  82. pri = pri->next;
  83. }
  84. }
Runtime error #stdin #stdout 0s 2424KB
stdin
5
2 Lisa 68 1.65 
5 Riya 50 1.5 
15 Shyam 70 1.7 
35 Raj 48 1.45 
12 Divya 65 1.6
stdout
Standard output is empty