Advertisement
Guest User

3.5 C++ Accelerated - Koenig&Moo

a guest
Jan 10th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. //Exercise 3.5
  2. //Create 2 vectors that can track student names and student grades
  3.     //Assume that grades is a fixed number
  4.     //we will assume 3
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10.  
  11. using std::cout;    using std::cin;     using std::endl;
  12. using std::vector;  using std::string;  using std::sort;
  13.  
  14.  
  15. int main(){
  16.  
  17.     const int homework_count = 3;
  18.     string name;
  19.     double grade;
  20.     double midterm;
  21.     double final;
  22.     double homework;
  23.  
  24.     //make vectors
  25.     vector<string> names;
  26.     vector<double> homeworks;
  27.     vector<double> midterms;
  28.     vector<double> finals;
  29.  
  30.     cout << "Enter Student names.  To exit enter EOF: ";
  31.     while(cin >> name){
  32.         names.push_back(name);
  33.         //get mid terms
  34.         cout << "Input mid term grade for " << name << ": ";
  35.         cin >> midterm;
  36.         midterms.push_back(midterm);
  37.         //get finals
  38.         cout << "Input final grade for " << name << ": ";
  39.         cin >> final;
  40.         finals.push_back(final);
  41.         //get homeworks
  42.         cout << "Input " << homework_count << " homework grades for " << name << ": ";
  43.         for (int i = 0; i < homework_count; i++){
  44.             cin >> homework;
  45.             homeworks.push_back(homework);
  46.         }
  47.     }
  48.  
  49.     //Now that we have all grades and names, we can compute each final grade for each student
  50.  
  51.     vector<double>::size_type student_size = names.size();
  52.     //need to find the median value
  53.     int mid = homework_count / 2;
  54.     for(int i = 0; i < student_size; i++){
  55.  
  56.         int offset = i * homework_count;
  57.         double median;
  58.         //sort the homework grades for this student
  59.         sort(homeworks.begin() + offset, homeworks.begin() + offset  + homework_count);
  60.        
  61.         if(homework_count % 2){
  62.             median = homeworks[mid + offset];
  63.         }else{
  64.             median = (homeworks[mid + offset] + homeworks[mid + offset - 1]) / 2;
  65.         }
  66.  
  67.         cout << names[i] << " median hws: " << median << " average : " << .2 * midterms[i] + .4 * finals[i] + .4 * median << endl;
  68.     }
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement