fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct{
  5. int id;
  6. double height;
  7. double weight;
  8. }Body;
  9.  
  10. int main(void) {
  11. Body data[5]={
  12. {1,165,60},
  13. {2,170,68},
  14. {3,160,50},
  15. {4,180,75},
  16. {5,175,80} };
  17. int n=5;
  18.  
  19. for (int i=0;i<n-1;i++){
  20. for(int j=0;j<n-1-i;j++){
  21. if(data[j].height > data[j+1].height){
  22. Body temp=data[j];
  23. data[j]=data[j+1];
  24. data[j+1]=temp;
  25. }
  26. }
  27. }
  28. for (int i=0; i<n; i++) {
  29. printf("%2d|%10.1f|%10.1f\n",data[i].id,data[i].height,data[i].weight);}
  30. double sum=0.0;
  31. int count=3;
  32. int start_index=n-count;
  33. for (int i=start_index;i<n;i++) {
  34. sum+=data[i].height;
  35. }
  36. double ave=sum/count;
  37.  
  38. double var_sum=0.0;
  39. for (int i=start_index;i<n;i++) {
  40. var_sum+=(data[i].height-ave)*(data[i].height-ave);
  41. }
  42. double std=sqrt(var_sum/count);
  43. printf("%.1fcm\n",ave);
  44. printf("std=%.1f\n",std);
  45. // your code goes here
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
 3|     160.0|      50.0
 1|     165.0|      60.0
 2|     170.0|      68.0
 5|     175.0|      80.0
 4|     180.0|      75.0
175.0cm
std=4.1