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, z, u;
  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 < t){
  49. t = a[i].height;
  50. z = i;
  51. }
  52. if(a[i].weight < s){
  53. s = a[i].weight;
  54. u = i;
  55. }
  56. }
  57.  
  58. if (a[i].height < Height_avg || a[i].weight < Weight_avg){
  59. printf("%s", a[z].name);
  60. printf("%s", a[u].name);
  61. }
  62.  
  63. free(a);
  64. system("pause");
  65. return 0;
  66. }
Runtime error #stdin #stdout 0.01s 2704KB
stdin
Standard input is empty
stdout
Standard output is empty