用c语言写出本程序
时间: 2023-10-05 09:05:26 浏览: 48
以下是用C语言实现输入5名学生的三门课成绩并计算总分和平均分的程序示例:
```c
#include <stdio.h>
int main()
{
float scores[5][3]; // 定义一个二维数组,用于存储5名学生的三门课成绩
// 读入每个学生的成绩
for(int i=0; i<5; i++) {
printf("请输入第%d名学生的成绩:\n", i+1);
printf("请输入第一门课成绩:");
scanf("%f", &scores[i][0]);
printf("请输入第二门课成绩:");
scanf("%f", &scores[i][1]);
printf("请输入第三门课成绩:");
scanf("%f", &scores[i][2]);
}
// 计算每个学生的总分和平均分
for(int i=0; i<5; i++) {
float total = scores[i][0] + scores[i][1] + scores[i][2];
float avg = total / 3.0;
printf("第%d名学生的总分为%.2f,平均分为%.2f\n", i+1, total, avg);
}
return 0;
}
```
这个程序中,我们同样定义了一个5行3列的二维数组`scores`,用于存储5名学生的三门课成绩。然后使用`for`循环,分别读入每个学生的成绩,并将其存储到`scores`数组中。最后再使用`for`循环,计算每个学生的总分和平均分,并输出结果。需要注意的是,C语言中用`scanf`函数读取浮点数时,需要使用`%f`格式控制符。