Assignment 8 Solutions
Solution gradSchools.cc code:
/*******************************************************************
* Name: Brenda Student
* Date: 4/21/14
* Course: CSCI 132 Section 01
* Assignment: Assignment 8
* Instructor: Royden
* Program: gradSchools.cc
* Purpose: Implements the functions in the GradSchools class
***************************************************************************/
#include "gradSchools.h"
GradSchools::GradSchools () {
/*Pre: None
*Post: Creates a GradSchool object and clears the schools list */
schools.clear();
}
void GradSchools::addSchool (string name, string state, int women, int rateAI, int rateSys,
int rateTheory, int effect, int ratePubs) {
/*Pre: name and state are strings
*Post: Creates a school object with the given data and adds it to the schools list*/
School tempSchool(name, state, women, rateAI, rateSys, rateTheory,
effect, ratePubs);
schools.insert(schools.size(), tempSchool);
}
void GradSchools::printGradSchools () {
/*Pre: none
*Post: Prints out the information about each graduate school in the schools list.
* The schools are listed in order. */
School tempSchool;
cout << "There are " << schools.size() << " schools in the database:" << endl;
for (int i = 0; i < schools.size(); i++) {
schools.retrieve(i, tempSchool);
tempSchool.printSchoolInfo();
}
}
void GradSchools::computeRatings (int weightWomen, int weightAI, int weightSys,
int weightTheory, int weightEffect, int weightPubs) {
/*Pre: None
*Post: Computes the overallRating for each school in the schools list from the
* given factors and ratings of the school. The computed overallRating is
* assigned to the given school. */
School tempSchool;
for (int i = 0; i < schools.size(); i++) {
schools.retrieve(i, tempSchool);
tempSchool.computeRating(weightWomen, weightAI, weightSys, weightTheory,
weightEffect, weightPubs);
schools.replace(i, tempSchool);
}
}
void print_names(Sortable_list<School> &my_schools) {
/*Pre: None
*Post: Prints out the names of each school in the schools list in order. */
School tempSchool;
for (int i = my_schools.size() -1; i >=0; i--) {
my_schools.retrieve(i, tempSchool);
cout << tempSchool.name << endl;
}
}
void GradSchools::rankSchools (int weightWomen, int weightAI, int weightSys,
int weightTheory, int weightEffect, int weightPubs) {
/*Pre: none
*Post: Computes the ratings for all schools in the schools list and then sorts
* the schools using selection_sort. It then prints out the names of the schools
* in order. */
computeRatings(weightWomen, weightAI, weightSys, weightTheory, weightEffect,
weightPubs);
schools.selection_sort();
// print final ranking of schools
cout << "Ranking of Grad School programs, given your preferences:" << endl;
print_names(schools);
cout << endl;
}
void GradSchools::rankSchoolsFactor (string factor) {
/*Pre: factor is one of the strings describing a factor for rating schools.
*Post: All the schools in the schools list are ranked and sorted according to the given
* factor. */
if (factor == "women")
rankSchools(1, 0, 0, 0, 0, 0);
else if (factor == "AI")
rankSchools(0, 1, 0, 0, 0, 0);
else if (factor == "systems")
rankSchools(0, 0, 1, 0, 0, 0);
else if (factor == "theory")
rankSchools(0, 0, 0, 1, 0, 0);
else if (factor == "effectiveness")
rankSchools(0, 0, 0, 0, 1, 0);
else
rankSchools(0, 0, 0, 0, 0, 1);
}
Home | | Syllabus | | Assignments | | Lecture Notes