Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Exercise 3.5
- //Create 2 vectors that can track student names and student grades
- //Assume that grades is a fixed number
- //we will assume 3
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using std::cout; using std::cin; using std::endl;
- using std::vector; using std::string; using std::sort;
- int main(){
- const int homework_count = 3;
- string name;
- double grade;
- double midterm;
- double final;
- double homework;
- //make vectors
- vector<string> names;
- vector<double> homeworks;
- vector<double> midterms;
- vector<double> finals;
- cout << "Enter Student names. To exit enter EOF: ";
- while(cin >> name){
- names.push_back(name);
- //get mid terms
- cout << "Input mid term grade for " << name << ": ";
- cin >> midterm;
- midterms.push_back(midterm);
- //get finals
- cout << "Input final grade for " << name << ": ";
- cin >> final;
- finals.push_back(final);
- //get homeworks
- cout << "Input " << homework_count << " homework grades for " << name << ": ";
- for (int i = 0; i < homework_count; i++){
- cin >> homework;
- homeworks.push_back(homework);
- }
- }
- //Now that we have all grades and names, we can compute each final grade for each student
- vector<double>::size_type student_size = names.size();
- //need to find the median value
- int mid = homework_count / 2;
- for(int i = 0; i < student_size; i++){
- int offset = i * homework_count;
- double median;
- //sort the homework grades for this student
- sort(homeworks.begin() + offset, homeworks.begin() + offset + homework_count);
- if(homework_count % 2){
- median = homeworks[mid + offset];
- }else{
- median = (homeworks[mid + offset] + homeworks[mid + offset - 1]) / 2;
- }
- cout << names[i] << " median hws: " << median << " average : " << .2 * midterms[i] + .4 * finals[i] + .4 * median << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement