c语言用数组,统计3个学生4门课程的考试成绩,要求输出每个学生的总成绩,每个学生的平均成绩
时间: 2024-05-15 15:14:47 浏览: 15
好的,这是一个关于C语言数组的问题。可以使用双重循环进行统计。具体代码如下:
```c
#include <stdio.h>
int main(){
int score[3][4] = {{85, 75, 90, 80},
{80, 80, 85, 75},
{95, 90, 80, 85}};
int i, j, sum;
for(i = 0; i < 3; i++){
sum = 0;
for(j = 0; j < 4; j++){
sum += score[i][j];
}
printf("第%d名学生的总成绩为:%d,平均成绩为:%d\n", i+1, sum, sum/4);
}
return 0;
}
```
上面的代码中,我们使用一个二维数组score来存储3个学生4门课程的考试成绩。接着使用了两个for循环,外层循环用来遍历每个学生,内层循环用来遍历每个学生的4门课程。在每次遍历内层循环时,我们使用sum变量来统计每门课程的成绩,遍历完成后,输出学生的总成绩和平均成绩即可。
相关问题
C语言编程:有5位学生的三门课程的考试成绩,计算每门课程的平均成绩、最高分、最低分
好的,这是一个关于 C 语言编程的问题,可以回答。以下是计算每门课程的平均成绩、最高分、最低分的代码示例:
```c
#include <stdio.h>
int main() {
float scores[5][3]; // 用二维数组存储五位学生的三门成绩
float sums[3] = {0}; // 分别存储三门课程的总成绩
float maxs[3] = {0}; // 分别存储三门课程的最高分,初始化为 0
float mins[3] = {100}; // 分别存储三门课程的最低分,初始化为 100
// 输入五位学生的三门成绩
for (int i = 0; i < 5; i++) {
printf("请输入第 %d 位学生的三门成绩:\n", i+1);
for (int j = 0; j < 3; j++) {
scanf("%f", &scores[i][j]);
sums[j] += scores[i][j]; // 累加总成绩
if (scores[i][j] > maxs[j]) maxs[j] = scores[i][j]; // 更新最高分
if (scores[i][j] < mins[j]) mins[j] = scores[i][j]; // 更新最低分
}
}
// 计算平均成绩
float avgs[3];
for (int j = 0; j < 3; j++) {
avgs[j] = sums[j] / 5;
}
// 输出结果
printf("每门课程的平均成绩分别为:%.2f, %.2f, %.2f\n", avgs[0], avgs[1], avgs[2]);
printf("每门课程的最高分分别为:%.2f, %.2f, %.2f\n", maxs[0], maxs[1], maxs[2]);
printf("每门课程的最低分分别为:%.2f, %.2f, %.2f\n", mins[0], mins[1], mins[2]);
return 0;
}
```
在这个示例中,我们使用了一个二维数组 `scores` 来存储五位学生的三门课程成绩,用三个一维数组 `sums`、`maxs`、`mins` 分别累加总成绩、更新最高分、最低分。最后计算平均成绩,输出结果。注意要用 `.2f` 格式控制符保留两位小数。
用c语言写一个统计学生期末考试成绩的程序
好的,以下是一个简单的用 C 语言写的统计学生期末考试成绩的程序:
```c
#include <stdio.h>
int main() {
int num_students;
float total_score = 0.0, average_score;
printf("Enter the number of students: ");
scanf("%d", &num_students);
float scores[num_students];
for (int i = 0; i < num_students; i++) {
printf("Enter the score of student %d: ", i+1);
scanf("%f", &scores[i]);
total_score += scores[i];
}
average_score = total_score / num_students;
printf("The average score is: %.2f\n", average_score);
return 0;
}
```
这个程序首先要求用户输入学生数量,然后使用一个数组 `scores` 存储每个学生的分数。程序循环读取每个学生的分数,并将总分数累加到 `total_score` 变量中。最后,程序计算平均分数,并输出结果。
请注意,这个程序假设用户输入的是有效的整数和浮点数,没有进行错误处理。在实际应用中,需要添加适当的错误处理代码来确保程序的健壮性。