C++ 如何在结构数组中显示GPA高于平均水平的学生姓名和ID

C++ 如何在结构数组中显示GPA高于平均水平的学生姓名和ID,c++,arrays,data-structures,struct,C++,Arrays,Data Structures,Struct,我有一项任务,显示平均成绩高于平均水平的学生的姓名、ID、GPA #include <iostream> #include <string> using namespace std; struct student{ string name; char stdid[20]; int age; char gender; float gpa; }; int main() { int amount; float

我有一项任务,显示平均成绩高于平均水平的学生的姓名、ID、GPA

#include <iostream>
#include <string>

using namespace std;

struct student{

    string name;
    char stdid[20];
    int age;
    char gender;
    float gpa;

};


int main() {

    int amount;
    float sum=0.0, average;

    struct student std[40];

    struct student *ptr = NULL;

    ptr = std;

    cout<<"enter the amount of students you want to input"<<endl;
    cin>>amount;


    for (int i=0;i<amount;i++)
    {
        cout<<"please enter details such as name, ID, age, gender and GPA for student "<< (i+1)<<endl;
        cout<<"Student ID : "<<endl;
        cin>>ptr->stdid;
        cout<<"Name : "<<endl;
        cin>>ptr->name;
        cout<<"Age : "<<endl;
        cin>>ptr->age;
        cout<<"Gender : "<<endl;
        cin>>ptr->gender;
        cout<<"GPA : "<<endl;
        cin>>ptr->gpa;
        sum += ptr->gpa;
    }

    average = sum/amount;
    cout << "Average GPA = " << average;


return 0;
}
#包括
#包括
使用名称空间std;
结构学生{
字符串名;
char-stdid[20];
智力年龄;
性别;
浮动gpa;
};
int main(){
整数金额;
浮动总和=0.0,平均值;
结构学生标准[40];
结构student*ptr=NULL;
ptr=std;

CUT

我建议您考虑存储< <代码>学生>代码>对象的容器和计算平均值的算法:

std::vector<student> students;

// ...
// fill the vector of students
// ...

// calculate the sum of all GPAs
float sum = std::accumulate(students.begin(), students.end(), 0.0f,
                            [](float acc, const student& elem) {
                               return acc + elem.gpa;       
                            }
);

// calculate the average GPA
auto avg = sum / students.size();
for (auto const& student: students)
   if (student.gpa > avg)
      std::cout << student; // call the output stream operator defined above
然后,您可以迭代整个
student
对象集合,只显示GPA高于平均值的对象:

std::vector<student> students;

// ...
// fill the vector of students
// ...

// calculate the sum of all GPAs
float sum = std::accumulate(students.begin(), students.end(), 0.0f,
                            [](float acc, const student& elem) {
                               return acc + elem.gpa;       
                            }
);

// calculate the average GPA
auto avg = sum / students.size();
for (auto const& student: students)
   if (student.gpa > avg)
      std::cout << student; // call the output stream operator defined above
for(自动常量和学生:学生)
如果(student.gpa>avg)

STD::CUT< P>我建议您考虑存储< <代码>学生>代码>对象的容器和计算平均值的算法:

std::vector<student> students;

// ...
// fill the vector of students
// ...

// calculate the sum of all GPAs
float sum = std::accumulate(students.begin(), students.end(), 0.0f,
                            [](float acc, const student& elem) {
                               return acc + elem.gpa;       
                            }
);

// calculate the average GPA
auto avg = sum / students.size();
for (auto const& student: students)
   if (student.gpa > avg)
      std::cout << student; // call the output stream operator defined above
然后,您可以迭代整个
student
对象集合,只显示GPA高于平均值的对象:

std::vector<student> students;

// ...
// fill the vector of students
// ...

// calculate the sum of all GPAs
float sum = std::accumulate(students.begin(), students.end(), 0.0f,
                            [](float acc, const student& elem) {
                               return acc + elem.gpa;       
                            }
);

// calculate the average GPA
auto avg = sum / students.size();
for (auto const& student: students)
   if (student.gpa > avg)
      std::cout << student; // call the output stream operator defined above
for(自动常量和学生:学生)
如果(student.gpa>avg)

std::难道你永远不能递增
ptr
来指向
std
中的以下学生吗?使用
std
作为变量名也是一个坏主意:可能与
命名空间std
冲突。我稍后会查找原因。但即使编译器没有与名为
std
的标识符混淆,人类也可能会。好的!我有现在将std变量更改为stdntWelcome以避免堆栈溢出。在进一步执行此项目之前,您必须学会在数组上迭代。您从未在
std
中增加
ptr
以指向以下学生。将
std
用作变量名也是一个坏主意:可能与
命名空间std
冲突。我将好的,稍后再解释原因。但是,即使编译器没有与名为
std
的标识符混淆,人类也可能会混淆。好的!我现在已经将std变量更改为stdntWelcome以防堆栈溢出。在继续此项目之前,您必须学会在数组上迭代。