Write a program where you: Use the following StudentInfo structure type to store
ID: 3676534 • Letter: W
Question
Write a program where you:
Use the following StudentInfo structure type to store student record: name and grade.
struct StudentInfo
{
string name;
int grade;
//need to overload the > operator here
}
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.
Explanation / Answer
// Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
struct StudentInfo
{
string name;
int grade;
//need to overload the > operator here
bool operator<(const StudentInfo& rhs) const
{
if (name.compare(rhs.name) <= 0)
{
return true;
}
return false;
}
};
list<int> multiples(int base, int n){
list<int> L;
for(int i=base; i<=n; i++){
if(i%base == 0){
L.push_back(i); // adding to list
}
}
return L;
}
int main()
{
list<StudentInfo> students;
StudentInfo s[] = {{"pravesh", 7},
{"mukesh",8},
{"vikesh", 9},
{"rupesh", 8},
{"alex", 4},
{"bob",5}
};
for(int i=0; i<6; i++)
students.push_back(s[i]);
students.sort();
// printing content of list
list<StudentInfo>::iterator i;
for(i=students.begin(); i != students.end(); ++i)
cout << (*i).name << " - "<<(*i).grade<<endl;
cout << endl;
return 0;
}
/*
alex - 4
bob - 5
mukesh - 8
pravesh - 7
rupesh - 8
vikesh - 9
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.