Advertisement
userxbw

Students using vectors subject average c++

Aug 11th, 2022 (edited)
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector <int> getGrades(vector<string> students,
  8. vector<string> subjects);
  9.  
  10. vector<double> getStudentAverage(
  11. vector<int> grades,int sub,int stu);
  12.  
  13. vector<double> getSubjectAverage(
  14. vector<int>grades,int sub,int stu);
  15.  
  16. void printSubjectAverage(
  17. vector<string>subjects,
  18. vector<double>subjectAverage);
  19.  
  20. void printStudentAverage(
  21. vector<string>students,
  22. vector<double>studentAverage);
  23.  
  24. void printBestStudentAverage(
  25. vector<string>students,
  26. vector<double>studentAverage);
  27.  
  28.  
  29. int main(){
  30.  
  31. vector <string>
  32. students {
  33. "Johnny", "Billy",
  34. "Dave", "Sam", "Mike"},
  35. subjects {
  36. "Physics", "Math","Arabic",
  37. "English", "History", "Biology"};
  38.  
  39. vector<int> grades;
  40. vector<double>
  41. studentAverage,
  42. subjectAverage;
  43.  
  44. grades = getGrades(students,subjects);
  45. studentAverage = getStudentAverage(grades,
  46. subjects.size(),students.size());
  47. subjectAverage = getSubjectAverage(grades,
  48. subjects.size(),students.size());
  49. printSubjectAverage(
  50. subjects,
  51. subjectAverage);
  52. printStudentAverage(
  53. students,
  54. studentAverage);
  55. printBestStudentAverage(
  56. students,
  57. studentAverage);
  58.  
  59. return 0;
  60. }
  61.  
  62. vector <int>  getGrades(vector<string> students,
  63. vector<string> subjects)
  64. {
  65.    int grade;
  66.    vector <int> g;
  67.   for ( auto e : students){
  68.       cout<< e << endl;
  69.      for(auto sb : subjects){
  70.         cout<<sb<<endl;
  71.         cin>>grade;
  72.         g.push_back(grade);
  73.     }
  74.      cout<<"\n\n";
  75.     }
  76.  
  77. return g;
  78. }
  79. /*
  80. use i to control switch
  81. reset i for each student
  82. **/
  83.  
  84. vector <double>  getStudentAverage(
  85. vector<int> grades,
  86. int sub,int stu)
  87. {
  88.    vector<double> sum (stu);
  89.    int i=0;
  90.   for(auto g : grades){
  91.         switch(i){
  92.     case 0:
  93.     sum.at(0)+=g;
  94.     break;
  95.     case 1:
  96.      sum.at(1)+=g;
  97.     break;
  98.     case 2:
  99.      sum.at(2)+=g;
  100.     break;
  101.     case 3:
  102.     sum.at(3)+=g;
  103.     break;
  104.     case 4:
  105.         sum.at(4)+=g;
  106.     break;
  107.      }//end switch
  108.      i++;
  109.     if(i>=sub)
  110.        i=0;
  111.  
  112.  }
  113. // create average
  114. for(int i=0;i<sum.size();i++)
  115.        sum.at(i)/=sub;
  116.  
  117. return sum;
  118. }
  119. /*
  120. same reasioning as above
  121. only this time it's using
  122. subjects with the i controling
  123. the switch
  124. **/
  125. vector<double> getSubjectAverage(
  126. vector<int>grades,int sub,int stu)
  127. {
  128.   vector<double>sum(sub);
  129.   int i=0;
  130.      for(auto g : grades){
  131.         switch(i){
  132.     case 0:
  133.     sum.at(0)+=g;
  134.     break;
  135.     case 1:
  136.     sum.at(1)+=g;
  137.     break;
  138.     case 2:
  139.     sum.at(2)+=g;
  140.     break;
  141.     case 3:
  142.     sum.at(3)+=g;
  143.     break;
  144.     case 4:
  145.     sum.at(4)+=g;
  146.     break;
  147.     case 5:
  148.     sum.at(5)+=g;
  149.     break;
  150.      }//end switch
  151.     i++;
  152.     if(i>=sub)
  153.        i=0;
  154.    } //end for
  155. for(int i=0;i<sum.size();i++)
  156.     sum.at(i)/=stu;
  157.  
  158. return sum;
  159. }
  160.  
  161. /*
  162.  prints, they're matching
  163.  sizes so use only one
  164.  to control printing
  165.  out both
  166. **/
  167.  
  168. void printSubjectAverage(
  169. vector<string>subjects,
  170. vector<double>subjectAverage){
  171.  
  172. cout<<"\nSubject Average\n";
  173. for(int i=0;i<subjects.size();i++){
  174.       cout<<subjects.at(i)<<" ";
  175.       cout<<subjectAverage.at(i)<<"\n";
  176.     }
  177. cout<<"\n";
  178. }
  179.  
  180. void printStudentAverage(
  181. vector<string>students,
  182. vector<double>studentAverage){
  183.  
  184. cout<<"\nStudent Average\n";
  185. for(int i=0;i<students.size();i++){
  186.     cout<<students.at(i)<<" "
  187.     <<studentAverage.at(i)<<"\n";
  188.     }
  189. cout<<"\n";
  190. }
  191.  
  192. /*
  193. got max number
  194. needed a way to know
  195. which student had that
  196. number.
  197.  
  198. simple, loop check,
  199. get student element number
  200. on match, print it.
  201. **/
  202.  
  203. void printBestStudentAverage(
  204. vector<string>students,
  205. vector<double>studentAverage){
  206.  
  207. double max = *max_element(studentAverage.begin(),
  208. studentAverage.end());
  209. int i=0;
  210. for(auto m : studentAverage){
  211.     if(max == m){
  212.      cout<<students.at(i)<<" has the best "
  213.      <<"average "<<" "<<max<<endl;
  214.     }
  215.       i++;
  216.  }
  217. cout<<"\n";
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement