fork download
  1. //動態配置結構陣列,
  2. //先輸入人數,再依序輸入n個人的姓名、身高、體重
  3. //輸出身高及體重平均
  4. //輸出身高或體重任一項低於平均的人姓名
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. struct student{
  10. char name[21];
  11. int height, weight;
  12. };
  13.  
  14. int main(){
  15. int i, n;
  16. double t = 10000.00;
  17. double s = 10000.00;
  18. double sum_height, sum_weight, Height_avg, Weight_avg;
  19. sum_height = 0;
  20. sum_weight = 0;
  21.  
  22. printf("請輸入人數: \n");
  23. scanf("%d", &n);
  24.  
  25. struct student *a = (struct student *)malloc(sizeof(struct student)*n);
  26.  
  27. for (i = 0; i < n; i++){
  28. printf("請輸入姓名:\n");
  29. scanf("%s",a[i].name);
  30. printf("請輸入身高:\n");
  31. scanf("%d",&a[i].height);
  32. printf("請輸入體重:\n");
  33. scanf("%d",&a[i].weight);
  34. }
  35.  
  36. for (i = 0; i < n; i++){
  37. sum_height += a[i].height;
  38. sum_weight += a[i].weight;
  39. }
  40.  
  41. Height_avg = sum_height / n;
  42. Weight_avg = sum_weight / n;
  43.  
  44. printf("H_avg: %.2lf\n", Height_avg);
  45. printf("W_avg: %.2lf\n", Weight_avg);
  46.  
  47. for (i = 0; i < n; i++){
  48. if(a[i].height < Height_avg || a[i].weight < Weight_avg){
  49. printf("%s\n", a[i].name);
  50. }
  51. }
  52.  
  53. free(a);
  54. system("pause");
  55. return 0;
  56. }
  57.  
  58.  
  59.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
請輸入人數: 
H_avg: -0.00
W_avg: -0.00