作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
函数int getMax(student *students, int Entry_size)应该计算所有学生的最高成绩。这里的 student 是结构体,*students 是数组,entry_size 已经可以使用下面所示的函数找到。 getMax 函数的技巧是它应该使用递归;包含数据的文件格式如下:
Name Surname ID Grade
Name Surname ID Grade
...
我已经完成了部分代码,它扫描并返回学生数组:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char firstName[30], secondName[30];
int ID;
int grade;
} student;
typedef struct {
int ID;
int avgGrade;
} topThreeAvg;
student *readStudents(char *fileName, int* entry_size, int *all_ids, int* studentSize){
/*
all_ids is an array for counting the amount of grades per student. studentSize is the actual number of students (for example, in students1.txt it is 2)
*/
FILE *openedFile = fopen(fileName, "r");
if(openedFile == NULL){
printf("Cannot open a file");
exit (0);
}
char entrySize[10];
fscanf(openedFile, "%[^\n]", entrySize);
int numberOfEntries = atoi(entrySize);
*entry_size = numberOfEntries;
student *students;
if ( NULL == ( students = malloc(numberOfEntries * sizeof *students))) {
printf ("Error with memory allocation");
exit (0);
}
int i;
for(i=0; i < numberOfEntries; i++){
if (4 != fscanf(openedFile, "%s %s %d %d"
, students[i].firstName
, students[i].secondName
, &students[i].ID
, &students[i].grade)) {
exit (0);
}
}
return students;
}
我尝试实现该功能,但它给出了非常大的数字:
int max_in(student *students, int entry_size){
int curr_val = students->grade;
if (entry_size == 1) {
return curr_val;
}
int max_in_rest = max_in(&students->ID+1, entry_size-1);
return curr_val > max_in_rest ? curr_val : max_in_rest;
}
如何使函数递归并找到所有学生中的最高成绩?
最佳答案
尝试如下。
int max(int x, int y){
return x > y ? x : y;
}
int max_in(student *students, int entry_size){
if (entry_size <= 0){
return -1;
}
else {
return max(students[entry_size-1].grade, max_in(students, entry_size-1))
}
}
关于c - 如何创建一个计算最大平均成绩的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58766568/
我正在尝试在 JavaScript 中创建一个正则表达式搜索,它会接受以下等级中的一个:A、B+、B、C+、C、D、F、AF 或 WF。 我有以下正则表达式:^A-?|[BCD][+-]?|[AW]?
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。它不符合 Stack Overflow guidelines 。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题
我正在制作一个学生数据库,其中包含类(class) ID、类(class)名称、每门类(class)的学分、通过/失败声明和成绩。我的目标是让按钮显示总和(成绩)/总和(学分) 这是我编写的代码,它给
我正在努力弄清楚如何实现这个计数。模型是用户、测试、等级 用户 has_many 测试,测试 has_many 成绩。 每个等级都有一个计算分数(strong_pass、pass、fail、stron
我是一名优秀的程序员,十分优秀!