gpt4 book ai didi

c++ - 如何在 C++ 中以结构体中的数组为目标

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:30 24 4
gpt4 key购买 nike

所以我编写了一个程序来使用结构和数组来计算文件中学生的最高/最低成绩(很快就会成为平均成绩)。虽然我确实计算出了最低和最高成绩,但我还想添加哪个学生的成绩最高和最低。我如何找到数组行并显示结构的 .first 和 .last?

#include <iostream>
#include <string>
#include <fstream>
#include <array>

using namespace std;

struct student {

int id;
string first;
string last;
float grade;
};

string getFileName();
void fillArrayFromFile(ifstream&, student[], int&);
float calculateMaximumGrade(student[], int);
float calculateMinimumGrade(student[], int);
float calculateAverageGrade(student[], int);



int main() {

int numofStudents = 5;

const int NUM_VALS = 5;

student studentArray[NUM_VALS];
ifstream filename;
//filename.open(getFileName() + ".txt");

fillArrayFromFile(filename, studentArray, numofStudents);

cout << "Highest grade: " << calculateMaximumGrade(studentArray, numofStudents) << endl;
cout << "Lowest grade: " << calculateMinimumGrade(studentArray, numofStudents) << endl;
//cout << "Average grade: " << calculateAverageGrade(studentArray, numofStudents) << endl;

return 0;
}

string getFileName()
{
// To be completed

return 0;
}

void fillArrayFromFile(ifstream& ins, student studentArray[], int& numberOfStudents)
{
ins.open("input.txt");



for (int i = 0; i < numberOfStudents; i++) {

ins >> studentArray[i].id;
ins >> studentArray[i].first;
ins >> studentArray[i].last;
ins >> studentArray[i].grade;

}

ins.close();

return;
}

// calculate the maximum grade in the array
float calculateMaximumGrade(student studentArray[], int numberOfStudents)
{
float max = 0;
max = studentArray[0].grade;

for (int i = 0; i < numberOfStudents; i++) {

if (studentArray[i].grade > max) {

max = studentArray[i].grade;

}

}
return max;
}

// calculate the minimum grade in the array
float calculateMinimumGrade(student studentArray[], int numberOfStudents)
{
float min = 0;
min = studentArray[0].grade;

for (int i = 0; i < numberOfStudents; i++) {

if (studentArray[i].grade < min) {

min = studentArray[i].grade;

}

}
return min;
}

// calculate the average grade in the array
float calculateAverageGrade(student studentArray[], int numberOfStudents)
{
// To be completed
return 0;
}

最佳答案

您不仅需要在 calculateMaximumGrade 函数中存储 max,还需要存储具有该 max 等级的元素的索引。即:

int calculateMaximumGrade(student studentArray[], int numberOfStudents)
{
float max = studentArray[0].grade;
int index_of_max = 0;

for (int i = 0; i < numberOfStudents; i++) {

if (studentArray[i].grade > max) {
index_of_max = i;
max = studentArray[i].grade;
}

}
return index_of_max;
}

如您所见,您也可以不返回max,而是返回它的index。然后你应该在你的 main 中执行以下操作:

int index_of_max = calculateMaximumGrade(studentArray,     numofStudents);
cout << "Highest grade: " << studentArray[index_of_max].grade << ". Student, who got it: " << studentArray[index_of_max].first << " " << studentArray[index_of_max].last << endl;

您应该对 AverageMin 函数执行相同的操作。

关于c++ - 如何在 C++ 中以结构体中的数组为目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41043956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com