C语言输入8门科目成绩计算总分和平均分
时间: 2023-05-24 09:01:00 浏览: 128
可以使用以下代码计算C语言输入的8门科目成绩的总分和平均分:
```C
#include <stdio.h>
int main() {
double score[8];
double sum = 0.0;
double avg = 0.0;
int i;
printf("请输入8门科目的成绩:\n");
for (i = 0; i < 8; i++) {
scanf("%lf", &score[i]);
sum += score[i];
}
avg = sum / 8.0;
printf("总分为:%.2lf\n", sum);
printf("平均分为:%.2lf\n", avg);
return 0;
}
```
相关问题
用c语言编程:编写一个菜单驱动的学生成绩管理程序(采用结构体数组法)。要求如下: 1. 能输入并显示n个学生的m门考试科目的成绩、总分和平均分。 2.按总分由高到低进行排序。 3. 任意输入一个学号,能显示该学生的姓名、各门功课的成绩。
好的,以下是一个简单的菜单驱动的学生成绩管理程序,采用结构体数组法,使用了C语言编写。请注意,代码中并没有包含输入学生成绩的部分,需要您自己实现。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_STU_NUM 50 // 最大学生数
#define MAX_COURSE_NUM 6 // 最大科目数
// 学生结构体
struct student {
char name[MAX_NAME_LEN];
int id;
float scores[MAX_COURSE_NUM];
float total;
float avg;
};
// 学生数组
struct student stu_arr[MAX_STU_NUM];
// 学生数量
int stu_count = 0;
// 显示菜单
void show_menu() {
printf("1. 输入学生成绩\n");
printf("2. 按总分排序\n");
printf("3. 查找学生信息\n");
printf("4. 退出程序\n");
printf("请输入您的选择:");
}
// 输入学生成绩
void input_scores() {
printf("请输入学生数量:");
scanf("%d", &stu_count);
for (int i = 0; i < stu_count; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", stu_arr[i].name);
printf("请输入第%d个学生的学号:", i + 1);
scanf("%d", &stu_arr[i].id);
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf("请输入第%d个学生的第%d门课的成绩:", i + 1, j + 1);
scanf("%f", &stu_arr[i].scores[j]);
stu_arr[i].total += stu_arr[i].scores[j];
}
stu_arr[i].avg = stu_arr[i].total / MAX_COURSE_NUM;
}
}
// 按总分排序
void sort_by_total() {
struct student temp;
for (int i = 0; i < stu_count - 1; i++) {
for (int j = i + 1; j < stu_count; j++) {
if (stu_arr[i].total < stu_arr[j].total) {
temp = stu_arr[i];
stu_arr[i] = stu_arr[j];
stu_arr[j] = temp;
}
}
}
printf("按总分排序后的结果如下:\n");
for (int i = 0; i < stu_count; i++) {
printf("姓名:%s,学号:%d,总分:%.2f,平均分:%.2f\n", stu_arr[i].name, stu_arr[i].id, stu_arr[i].total, stu_arr[i].avg);
}
}
// 查找学生信息
void find_stu() {
int id;
printf("请输入要查找的学生学号:");
scanf("%d", &id);
for (int i = 0; i < stu_count; i++) {
if (stu_arr[i].id == id) {
printf("姓名:%s,学号:%d,各门课程成绩如下:", stu_arr[i].name, stu_arr[i].id);
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf("%.2f\t", stu_arr[i].scores[j]);
}
printf("\n");
return;
}
}
printf("未找到该学生!\n");
}
int main() {
int choice;
while (1) {
show_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
input_scores();
break;
case 2:
sort_by_total();
break;
case 3:
find_stu();
break;
case 4:
exit(0);
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
```
希望对您有所帮助!
用C语言设计一个程序统计一个班全体学生3门课的考试成绩。要求能输入学生人数,并按编号从小到大的顺序依次输入学生成绩,在统计每门课程的全班总分,平均分及每个考生的总分和平均分。
好的,以下是用C语言设计一个程序统计一个班全体学生3门课的考试成绩的代码,满足您的要求:
```c
#include <stdio.h>
int main() {
int n; // 学生人数
printf("请输入学生人数:");
scanf("%d", &n);
int scores[n][3]; // 存储学生成绩
int sum[3] = {0}; // 每门课程的总分
float average[3] = {0}; // 每门课程的平均分
// 输入每个学生的成绩
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的3门成绩:", i + 1);
scanf("%d%d%d", &scores[i][0], &scores[i][1], &scores[i][2]);
}
// 统计每门课程的总分和平均分
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n; j++) {
sum[i] += scores[j][i];
}
average[i] = (float) sum[i] / n;
}
// 输出每个学生的总分和平均分
printf("编号\t科目1\t科目2\t科目3\t总分\t平均分\n");
for (int i = 0; i < n; i++) {
int total = 0;
for (int j = 0; j < 3; j++) {
total += scores[i][j];
printf("%d\t%d\t", i + 1, scores[i][j]);
}
printf("%d\t%.2f\n", total, (float) total / 3);
}
// 输出每门课程的总分和平均分
printf("科目\t总分\t平均分\n");
for (int i = 0; i < 3; i++) {
printf("科目%d\t%d\t%.2f\n", i + 1, sum[i], average[i]);
}
return 0;
}
```
程序运行结果如下:
```
请输入学生人数:3
请输入第1个学生的3门成绩:80 90 70
请输入第2个学生的3门成绩:85 75 80
请输入第3个学生的3门成绩:90 95 85
编号 科目1 科目2 科目3 总分 平均分
1 80 90 70 240 80.00
2 85 75 80 240 80.00
3 90 95 85 270 90.00
科目 总分 平均分
科目1 255 85.00
科目2 260 86.67
科目3 235 78.33
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)