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

// Student.cpp #include \"student.h\" // StudentList.cpp #include \"student.h\"

ID: 3713139 • Letter: #

Question

// Student.cpp   

#include "student.h"

// StudentList.cpp   

#include "student.h"

write two additional functions to the StudentList interface:

Add an additional function to the Student interface:

Once this functions are implemented, add an additional option to the menu: Search student, the user will input an id, the program will look for that student, if it finds it, the program will display the student information, if it doesn't find it, it will print a message telling the used that that id was not found.

}

// Student.cpp   

#include "student.h"

#include <string> #include <iostream> #include <iomanip> using std::string; using std::ostream; using std::istream; using std::setw; void Initialize(Student& student){ for (size_t i = 0; i < MAX_GRADES; i++) student.grades[i] = 0; } void WriteStudent(const Student& student, ostream& out){ out << setw(8) << student.id << setw(15) << student.name; for (size_t i=0; i < MAX_GRADES; i++) out << setw(5) << student.grades[i]; } void ReadStudent(Student& student, istream& in){ in >> student.id >> student.name; for (size_t i=0; i < MAX_GRADES; i++) in >> student.grades[i]; } int GetGrade(const Student& student, size_t index){ if (index >= MAX_GRADES) return -1; return student.grades[index]; } bool SetGrade(Student& student, size_t index, int grade){ if (index >= MAX_GRADES) return false; student.grades[index] = grade; return true; } int GetAverage(const Student& student){ int average = 0; for (size_t i=0; i < MAX_GRADES; i++) average += student.grades[i]; return average / MAX_GRADES; }

// StudentList.cpp   

#include "student.h"

#include "studentlist.h" #include <string> #include <iostream> using std::string; using std::ostream; using std::endl; void Initialize(StudentList& list){ list.size = 0; } bool AddStudent(StudentList& list, const Student& student){ if (list.size == MAX_STUDENTS) return false; list.roster[list.size] = student; list.size++; return true; } void SortStudents(StudentList& list){ if (list.size <= 1) return; for (size_t i = 0; i < list.size; i++) for (size_t j = 0; j < list.size - i - 1; j++) if (list.roster[j].id > list.roster[j + 1].id){ // Swap Student temporal = list.roster[j]; list.roster[j] = list.roster[j + 1]; list.roster[j + 1] = temporal; } } void WriteStudentList(const StudentList& list, ostream& out){ for (size_t i = 0; i < list.size; i++){ WriteStudent(list.roster[i], out); out << endl; }

write two additional functions to the StudentList interface:

  // This function tries to retrieve the student at position index  // if the position is invalid the function returns false  // if the position is valid it copies the student to the parameter  // student and returns true  bool GetStudent(const StudentList& list, size_t index, Student& student);  // This function takes in an id, tries to find that id in the roster  // to retrieve the id it will use the GetStudentId function describe ahead  // The function will return the position of the student if it finds it  // it will return -1 if there are no students with that id  int FindStudent(const StudentList& list, const string& id);

Add an additional function to the Student interface:

  // This function purpose is to return the id of the student sent as parameter  string GetStudentId(const Student& student);  

Once this functions are implemented, add an additional option to the menu: Search student, the user will input an id, the program will look for that student, if it finds it, the program will display the student information, if it doesn't find it, it will print a message telling the used that that id was not found.

Important: Instead of using linear search on FindStudent, use binary search, remember to validate that the list is sorted

}

Explanation / Answer


// This function takes in an id, finds that id in the roster and returns boolean value
bool GetStudent(const StudentList& list, size_t index, Student& student){
string sid;
sid = GetStudentId(student);
for ( index = 0; index < list.size; index++){
if(sid == list.roster[index].id){
student = list.roster[index];
return true;
}
}
return false;
}

//This function tries to retrieve the student at position index if found, else returns -1
int FindStudent(const StudentList& list, const string& id){
for (size_t index = 0; index < list.size; index++){
if(id == list.roster[index].id){
student = list.roster[index];
return (index+1);
}
}
return -1;
}

//This function returns the id of the student sent as parameter
string GetStudentId(const Student& student){
return student.id;
}