Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

struct StudentInfo { string name; int grade; //need to overload the > operator h

ID: 3845585 • Letter: S

Question

struct StudentInfo
{
     string name;
     int grade;

     //need to overload the > operator here
}

Use the following StudentInfo structure type to store student record: name and grade.

In a function named populateStudentRecord(), use a list data structure to populate the student data. Make up the data by yourself using some constant name and grade values in the function (do not ask the user to provide data).

In the main() function:

Create a list of objects of StudentInfo type.

Call the populateStudentRecord() to populate the list object.

Use the built-in sort algorithm from STL to sort the list by name and display the information in this format "name - grade". Remember, you need to overload the < operator for the structure so that the StudentInfo structure type variables can be compared.

Calculate the maximum and minimum grades and the class average and display them.

struct StudentInfo
{
     string name;
     int grade;

     //need to overload the > operator here
}

Explanation / Answer


#include <algorithm>
#include <iostream>
#include <list>
#include <string>

using namespace std;

typedef struct StudentInfo {
string name;
int grade;

} StudentInfo;

StudentInfo *createRecord(string name, int grade) {
StudentInfo *new_record = new StudentInfo;
new_record->name = name;
new_record->grade = grade;
return new_record;
}

std::list<StudentInfo *> populateStudentRecord() {
std::list<StudentInfo *> info;
info.push_front(createRecord("Ujjwal", 5));
info.push_front(createRecord("Tjjwal", 6));
info.push_front(createRecord("Twal", 1));
info.push_front(createRecord("Ujuoal", 20));
return info;
}

int main() {
std::list<StudentInfo *> info;
info = populateStudentRecord();


sort( info.begin( ), info.end( ), [ ]( const StudentInfo* lhs, const StudentInfo* rhs )
{
return lhs->name < rhs->name ;
});

for (std::list<StudentInfo *>::iterator it=mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << (*it)->name<<'-'<<(*it)->grade;

  
  
}