#include <stdio.h> #define N 40 int GetAboveAver(int score[], int n); void main() { int score[N], m, n; n = -1; //1 do{ n++; scanf("%d", &score[n]);//1 }while (score[n] >= 0); //e2 m = GetAboveAver(score, n); //2 printf("Students of above average is %d\n", m); } int GetAboveAver(int score[], int n) { int i, sum, aver,count; count = 0;//1 sum = 0;//1 for (i=0; i<n; i++)//1 { sum += score[i];//2 } aver=sum/n;//1 for (i=0; i<n; i++)//1 { if (score[i] >= aver)//2 count++;//1 } printf("Total students are %d\n",n); printf("Average score is %d\n",aver); return count;//1 }
时间: 2024-01-26 12:02:16 浏览: 22
这是一段 C 语言程序,主要实现了以下功能:
1. 从输入中读取学生的分数,直到输入负数为止;
2. 计算所有学生的平均分数;
3. 统计高于平均分数的学生人数,并输出结果。
具体而言,程序定义了一个 GetAboveAver 函数来计算高于平均分数的学生人数,该函数接受两个参数:一个整型数组 score,存储了所有学生的分数;一个整型变量 n,表示学生总数。GetAboveAver 函数首先计算所有学生的平均分数,然后遍历数组 score,统计高于平均分数的学生人数,并将结果返回给主函数。
在主函数中,程序首先定义了一个整型数组 score 和两个整型变量 m 和 n。然后使用 do-while 循环读入学生的分数,并将其存储在数组 score 中,直到读入负数为止。接着调用 GetAboveAver 函数计算高于平均分数的学生人数,并将结果存储在变量 m 中。最后输出结果。
需要注意的是,程序中使用了一些宏定义和注释来增加代码的可读性,这些内容在实际编译中并不会被编译器处理。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 // 学生信息结构体 typedef struct { char num[10]; // 学号 char name[10]; // 姓名 char clas[10]; // 班级 char sex[10]; // 性别 float score; // 成绩 } Student; // 学生信息数组 Student studentList[MAX_SIZE]; int size = 0; 帮我添加一个可以查询学生成绩最大值和最小值的分函数代码
可以添加以下代码来实现查询学生成绩最大值和最小值的函数:
```c
// 查询学生成绩最大值和最小值
void findMinMaxScore() {
float maxScore = 0, minScore = 100;
for (int i = 0; i < size; i++) {
if (studentList[i].score > maxScore) {
maxScore = studentList[i].score;
}
if (studentList[i].score < minScore) {
minScore = studentList[i].score;
}
}
printf("成绩最高的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == maxScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
printf("成绩最低的学生信息:\n");
for (int i = 0; i < size; i++) {
if (studentList[i].score == minScore) {
printf("学号:%s 姓名:%s 班级:%s 性别:%s 成绩:%.2f\n",
studentList[i].num, studentList[i].name,
studentList[i].clas, studentList[i].sex,
studentList[i].score);
}
}
}
```
在主函数中,可以通过调用该函数来查询学生成绩最大值和最小值:
```c
int main() {
// 添加学生信息
// ...
// 查询学生成绩最大值和最小值
findMinMaxScore();
return 0;
}
```
修改以下代码“”“#include<stdio.h> #define N 5 typedef struct student{ long sno; char name[10]; float score[3]; }STU; void fun(char *filename,STU n){ FILE *fp; /**********found**********/ fp=fopen(filename,"rb"); /*********”
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n) {
FILE *fp;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Cannot open file!\n");
return;
}
fread(&n, sizeof(STU), 1, fp);
printf("sno:%ld\nname:%s\nscore[0]:%.2f\nscore[1]:%.2f\nscore[2]:%.2f\n", n.sno, n.name, n.score[0], n.score[1], n.score[2]);
fclose(fp);
}
int main() {
STU s[N] = {
{20210001, "Tom", {89, 78, 92}},
{20210002, "Jerry", {78, 87, 91}},
{20210003, "Mickey", {90, 77, 83}},
{20210004, "Minnie", {94, 92, 90}},
{20210005, "Donald", {85, 88, 82}}
};
FILE *fp;
fp = fopen("test.dat", "wb");
if (fp == NULL) {
printf("Cannot open file!\n");
return 0;
}
fwrite(&s[2], sizeof(STU), 1, fp);
fclose(fp);
fun("test.dat", s[2]);
return 0;
}
```