In C++ Assume that the roster of CS 50 is represented as a list of student objec
ID: 3668377 • Letter: I
Question
In C++
Assume that the roster of CS 50 is represented as a list of student objects where the class student is defined as follows:
class student {
public:
string name;
string id;
float grade;
student(string &n, string &i, float g) :
name(n), id(i), grade(g)
{}
};
The variable grade is assumed to hold a real number between 0.0 and 4.0. Write the following functions:
(a) bool find(const list<student>& roster, const float g)
This function returns true if there is at least one student in the roster whose grade is greater than or equal to g.
bool find(const list<student>& roster, const float g){
bool
}
(b) void insert_sorted(list<student>& roster, student &stu)
Assume that roster is sorted by grade from the highest grade to the lowest grade, this function inserts a student stu into roster such that the roster remains sorted after the insertion.
void insert_sorted(list<student>& roster, student &stu){
}
(c) pair<float,float> low_high(const list<student>& roster)
Assume that roster is sorted by grade from the highest grade to the lowest grade, this function returns a pair whose first member is the lowest grade and second member is the highest grade of the roster.
void insert_sorted(list<student>& roster, student &stu){
}
(d) How would you write the function low high in (c) if the roster is not sorted? Question 3 (20 points)
Explanation / Answer
I have implemented the required functions in a driver cpp program for testing those functions.
Functions are highlighted in bold.
d)
CS50.cpp
#include<list>
#include "Student.h"
using namespace std;
bool find(const list<student>& roster, const float g);
void insert_sorted(list<student>& roster, student &stu);
pair<float, float> low_high(const list<student>& roster);
int main()
{
list<student> roster;
string sName="Jhon";
string sId = "123";
student s(sName,sId, 3.4);
roster.push_back(s);
sName = "Mike";
sId = "124";
student m(sName, sId, 3.7);
insert_sorted(roster, m);
bool x = find(roster, 3.5);
sName = "Jhonny";
sId = "125";
student p(sName, sId, 3.1);
insert_sorted(roster, p);
pair<float, float> gr = low_high(roster);
return 0;
}
bool find(const list<student>& roster, const float g)
{
for (list<student>::const_iterator i = roster.begin(); i != roster.end(); ++i)
{
if (i->grade >= g)
{
return true;
}
}
return false;
}
void insert_sorted(list<student>& roster, student &stu)
{
for (list<student>::const_iterator i = roster.begin(); i != roster.end(); ++i)
{
if (stu.grade>i->grade)
{
roster.insert(i, stu);
return;
}
}
//if the student is to be inserted in the last
roster.push_back(stu);
}
pair<float, float> low_high(const list<student>& roster)
{
pair<float, float> grades;
//front of the list i.e student with highest grade
student s = roster.front();
//back of the list i.e student with lowest grade
student p = roster.back();
grades.first = p.grade;
grades.second = s.grade;
return grades;
}
d)
if the roster is not sorted then we need to find the max and min grade by iterating over the roster
pair<float, float> low_high(const list<student>& roster)
{
pair<float, float> grades;
float max = -1;
float min = 4.0;
for (list<student>::const_iterator i = roster.begin(); i != roster.end(); ++i)
{
if (i->grade>max)
{
max = i->grade;
}
if (i->grade < min)
{
min = i->grade;
}
}
grades.first = min;
grades.second = max;
}