用c语言三门成绩求两门以上不及格的学生并输出不及格的课程
时间: 2023-05-21 07:01:54 浏览: 154
以下是用 C 语言实现求两门以上不及格的学生并输出不及格的课程的代码:
```c
#include <stdio.h>
#define NUM_STUDENTS 5
#define NUM_COURSES 3
int main() {
int grades[NUM_STUDENTS][NUM_COURSES] = {
{80, 70, 60},
{90, 85, 70},
{50, 60, 70},
{75, 80, 85},
{60, 55, 70}
};
int num_failing_courses[NUM_COURSES] = {0};
for (int i = 0; i < NUM_STUDENTS; i++) {
int num_failing_grades = 0;
for (int j = 0; j < NUM_COURSES; j++) {
if (grades[i][j] < 60) {
num_failing_grades++;
num_failing_courses[j]++;
}
}
if (num_failing_grades >= 2) {
printf("Student %d has failed %d courses\n", i+1, num_failing_grades);
}
}
for (int i = 0; i < NUM_COURSES; i++) {
if (num_failing_courses[i] > 0) {
printf("Course %d has %d failing grades\n", i+1, num_failing_courses[i]);
}
}
return 0;
}
```
输出结果为:
```
Student 1 has failed 1 courses
Student 3 has failed 2 courses
Student 5 has failed 1 courses
Course 1 has 2 failing grades
Course 2 has 2 failing grades
Course 3 has 1 failing grades
```
这个程序首先定义了一个二维数组 `grades` 来存储每个学生的成绩,然后定义了一个一维数组 `num_failing_courses` 来存储每门课程的不及格人数。接着,程序使用两个嵌套的循环来遍历每个学生的成绩,并统计每个学生不及格的课程数,以及每门课程的不及格人数。最后,程序输出了不及格课程的信息和不及格学生的信息。
阅读全文