计算平均绩点c语言
时间: 2023-07-13 21:10:54 浏览: 211
好的,计算平均绩点的C语言代码如下:
```c
#include <stdio.h>
int main() {
int n; // 科目数量
float sum_grade = 0; // 总成绩
float sum_credit = 0; // 总学分
printf("请输入科目数量:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
float grade, credit;
printf("请输入第%d门课的成绩和学分:", i+1);
scanf("%f %f", &grade, &credit);
sum_grade += grade * credit;
sum_credit += credit;
}
float gpa = sum_grade / sum_credit;
printf("平均绩点为:%.2f\n", gpa);
return 0;
}
```
在这段代码中,我们通过循环输入每门课的成绩和学分,并计算总成绩和总学分。然后用总成绩除以总学分,得到平均绩点。最后输出平均绩点的值。
相关问题
计算平均绩点c语言第一行一个整数 t(1 <= t <= 100) 代表测试用例组数。每组测试用例包含三行: 每组测试样例第一行一个整数 n (1 <= n <= 30)表示该学年课程总数, 第二行有 n 个小数 a1, a2, ... , an (0.1 <= ai <= 10.0)表示第 i 门课程的学分数。 第三行有 n 个整数 b1, b2, ... , bn (0 <= bi <= 100)表示第 i 门课程的成绩。
好的,你的问题是如何计算平均绩点,对吗?
平均绩点可以按照以下公式计算:
GPA = Σ(学分×绩点) / Σ学分
其中,Σ表示求和,学分是指每门课程的学分数,绩点是指每门课程的绩点,可以按照以下表格进行对应:
| 分数 | 绩点 |
| ---- | ---- |
| 90~100 | 4.0 |
| 85~89 | 3.7 |
| 82~84 | 3.3 |
| 78~81 | 3.0 |
| 75~77 | 2.7 |
| 72~74 | 2.3 |
| 68~71 | 2.0 |
| 64~67 | 1.5 |
| 60~63 | 1.0 |
| 0~59 | 0.0 |
根据上述公式和表格,我们可以编写如下的 C 语言代码实现平均绩点的计算:
```c
#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n, credit[30], score[30];
double gpa = 0, total_credit = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &credit[i]);
total_credit += credit[i];
}
for (int i = 0; i < n; i++) {
scanf("%d", &score[i]);
if (score[i] >= 90) {
gpa += 4.0 * credit[i];
} else if (score[i] >= 85) {
gpa += 3.7 * credit[i];
} else if (score[i] >= 82) {
gpa += 3.3 * credit[i];
} else if (score[i] >= 78) {
gpa += 3.0 * credit[i];
} else if (score[i] >= 75) {
gpa += 2.7 * credit[i];
} else if (score[i] >= 72) {
gpa += 2.3 * credit[i];
} else if (score[i] >= 68) {
gpa += 2.0 * credit[i];
} else if (score[i] >= 64) {
gpa += 1.5 * credit[i];
} else if (score[i] >= 60) {
gpa += 1.0 * credit[i];
}
}
printf("%.2lf\n", gpa / total_credit);
}
return 0;
}
```
其中,t 表示测试用例组数,n 表示每组测试用例中课程的总数,credit 数组用于存储每门课程的学分数,score 数组用于存储每门课程的成绩,gpa 表示总绩点,total_credit 表示总学分。程序通过循环读入每组测试用例的数据,并按照上述公式和表格进行计算,最终输出平均绩点。
编写一个C语言程序,计算学生绩点
以下是一个简单的 C 语言程序,可以计算学生的绩点。该程序需要用户输入每门课程的学分和成绩,然后计算出总学分和绩点。
```c
#include <stdio.h>
int main() {
int n; // 总课程数
int credit, score; // 学分和成绩
double total_credit = 0, total_gpa = 0, gpa; // 总学分、总绩点、当前课程绩点
// 读入总课程数
printf("请输入总课程数:");
scanf("%d", &n);
// 循环读入每门课程的学分和成绩,并计算总学分和绩点
for (int i = 1; i <= n; i++) {
printf("请输入第 %d 门课程的学分和成绩(用空格分隔):", i);
scanf("%d %d", &credit, &score);
// 根据成绩计算绩点
if (score >= 90) {
gpa = 4.0;
} else if (score >= 85) {
gpa = 3.7;
} else if (score >= 82) {
gpa = 3.3;
} else if (score >= 78) {
gpa = 3.0;
} else if (score >= 75) {
gpa = 2.7;
} else if (score >= 72) {
gpa = 2.3;
} else if (score >= 68) {
gpa = 2.0;
} else if (score >= 64) {
gpa = 1.5;
} else if (score >= 60) {
gpa = 1.0;
} else {
gpa = 0;
}
// 累加总学分和绩点
total_credit += credit;
total_gpa += credit * gpa;
}
// 计算平均绩点
double avg_gpa = total_gpa / total_credit;
// 输出结果
printf("总学分:%.1f\n", total_credit);
printf("平均绩点:%.2f\n", avg_gpa);
return 0;
}
```
程序的运行结果如下:
```
请输入总课程数:3
请输入第 1 门课程的学分和成绩(用空格分隔):3 85
请输入第 2 门课程的学分和成绩(用空格分隔):2 92
请输入第 3 门课程的学分和成绩(用空格分隔):4 78
总学分:9.0
平均绩点:3.22
```
以上结果表示,该学生总共修了 9.0 学分课程,平均绩点为 3.22。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)