0
Follow
1
View

C language to obtain 3 students of 4 courses total score and average

xiong_sir 注册会员
2023-02-28 20:20
< div class = " md_content_show“数据- v - 3967 e397 = " " >

< div class = " aw-list-img " > img

dengkun1981922 注册会员
2023-02-28 20:20

You're welcome


#include 
int main()
{
    int student1[4];
    int student2[4];
    int student3[4];
    int total1, total2, total3;
    int average1, average2, average3;
    int i;
    printf("请输入学生1的4门课程成绩:\n");
    for (i = 0; i < 4; i++)
    {
        scanf("%!d(MISSING)", &student1[i]);
    }
    printf("请输入学生2的4门课程成绩:\n");
    for (i = 0; i < 4; i++)
    {
        scanf("%!d(MISSING)", &student2[i]);
    }
    printf("请输入学生3的4门课程成绩:\n");
    for (i = 0; i < 4; i++)
    {
        scanf("%!d(MISSING)", &student3[i]);
    }
    total1 = student1[0] + student1[1] + student1[2] + student1[3];
    total2 = student2[0] + student2[1] + student2[2] + student2[3];
    total3 = student3[0] + student3[1] + student3[2] + student3[3];
    average1 = total1 / 4;
    average2 = total2 / 4;
    average3 = total3 / 4;
    printf("学生1的4门课程总分是:%!d(MISSING)\n", total1);
    printf("学生1的4门课程平均分是:%!d(MISSING)\n", average1);
    printf("学生2的4门课程总分是:%!d(MISSING)\n", total2);
    printf("学生2的4门课程平均分是:%!d(MISSING)\n", average2);
    printf("学生3的4门课程总分是:%!d(MISSING)\n", total3);
    printf("学生3的4门课程平均分是:%!d(MISSING)\n", average3);
    return 0;
}
dengfangjian 注册会员
2023-02-28 20:20
  • array a[3][4] When i = 3 or j = 4, this creates an out-of-bounds access to the two-dimensional array, There are various unexpected output cases
  • modified as follows [index access from 0]
#include 
#define M 3
#define N 4
int main()
{
    int a[M][N];
    float sum[M], ave[M];
    int i, j;
    for (i = 0; i < M; i++) // 索引 从 0开始
    {
        sum[i] = 0;
        for (j = 0; j < N; j++) // 索引 从 0开始
        {
            scanf("%d", &a[i][j]);
            sum[i] = sum[i] + a[i][j];
        }
        ave[i] = sum[i] / 4;
        printf("sum[%d]=%-5.2f,p=%-5.2f\n",i, sum[i], ave[i]);
    }
    return 0;
}

About the Author

Question Info

Publish Time
2023-02-28 20:19
Update Time
2023-02-28 20:19

Related Question

silicon laboratories ide

Android Selector失效

属性“clipboard”在类型“Navigator”上不存在

错误代码3012,当试图将WSOL转移到SPL令牌,如ETH与Phantom

为什么JavaScript同时将vector定义为vector和undefined ?

Laravel - MorphTo关系返回null

与idea有关的tomcat使用问题

Tare_Planner的学习笔记(一)protobuf 的版本

elementor form表单提示如何更改

Apache Flink: JMXReporterFactory无法找到