时间在哪里853 2022-11-12 15:55 采纳率: 82.4%
浏览 1196
已结题

建立一个学生的结构记录,包括学号、姓名和成绩。输入整数n(n<=10),再输入n个学生的基本信息,要求计算并输出他们的平均成绩(保留2位小数)。

#include<stdio.h>
struct student{
int num;
char name[10];
int score;
};
int main()
{
int i,n,sum=0,x;
struct student s[10];
printf("Input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the number,name,score of the %d student:",i+1);
scanf("%d%s%d",&s[i].num,s[i].name,&s[i].score);
sum+=s[i].score;
}
x=sum/n;
printf("The average score is:%.2f",x);
return 0;
}
为啥输出平均分是0.00啊,哪一步出问题了吗


#include<stdio.h>
struct student{
int num;
char name[10];
int score;
};
int main()
{
    int i,n,sum=0,x;
    struct student s[10];
    printf("Input n:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
      {
    printf("Input the number,name,score of the %d student:",i+1);
    scanf("%d%s%d",&s[i].num,s[i].name,&s[i].score);
    sum+=s[i].score;
      }
    x=sum/n;
    printf("The average score is:%.2f",x);
    return 0;
}
  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-11-12 20:52
    关注

    见注释,供参考:

    #include <stdio.h>
    #define N 11
    struct student{
        int  num;
        char name[10];
        int  score;
    };
    int main()
    {
        int i,n,sum=0;
        double x;      //修改
        struct student s[N];
        printf("Input n:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("Input the number,name,score of the %d student:",i+1);
            scanf("%d%s%d",&s[i].num,s[i].name,&s[i].score);
            sum+=s[i].score;
        }
        x=(double)sum/n;//修改
        printf("The average score is:%.2f",x);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月20日
  • 已采纳回答 11月13日
  • 修改了问题 11月12日
  • 修改了问题 11月12日
  • 展开全部

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function