Students

From Userx , 1 Year ago, written in C++, viewed 370 times.
URL http://codebin.org/view/c0f47aff Embed
Download Paste or View Raw
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Amount of students
  6. #define ROW 2
  7. // Amount of grades per subject
  8. #define COL 3
  9. #define SIZE_ARRAY(array) (sizeof(array)/sizeof(array[0]))
  10. #define SIZE2D_ARRAY(array) (sizeof(array[0])/sizeof(array[0][0]))
  11. //proto types
  12. void sortRow(int list[][COL],int r,int c);
  13. void createStudents(string *s, int r);
  14. void enterGrades(string *s,int m[][COL],
  15.  int h[][COL],int e[][COL],int r,int c);
  16. void gradesLowHigh(string *s,int m[][COL],
  17.  int h[][COL], int e[][COL],int r,int c);
  18. void listGradesDescendingOrder(string *s,
  19.  int m[][COL],int h[][COL],int e[][COL],int r,int c);
  20.  
  21. int main(){
  22.  
  23. string students[ROW];
  24.  
  25. /*
  26. 2D arrays
  27. **/
  28. int math[ROW][COL];
  29. int history[ROW][COL];
  30. int english[ROW][COL];
  31. int r=0,c=0,i=0;
  32.  
  33. //string name;
  34. createStudents(students,ROW);
  35. enterGrades(students,math,
  36. history,english,ROW,COL);
  37.  
  38. /*
  39. Just 3 different ways of passing
  40. size of arrays
  41. **/
  42. //use of macro. helps insure proper size
  43. //incase values have been changed elsewhere
  44. sortRow(math,SIZE_ARRAY(math),SIZE2D_ARRAY(math));
  45. //getting sizeof just before pasing the values
  46. r = sizeof(history)/sizeof(history[0]);
  47. c = sizeof(history[0])/sizeof(history[0][0]);
  48. sortRow(history,r,c);
  49.  
  50. // USING DEFINE VALUES
  51. sortRow(english,ROW,COL);
  52.  
  53.  
  54. gradesLowHigh(students,math,history,english,
  55. ROW,COL);
  56.  
  57. listGradesDescendingOrder(students,math,
  58. history,english,ROW,COL);
  59.  
  60.  
  61. double msum=0,hsum=0,esum=0;
  62. double count=0;
  63. double maverage,haverage,eaverage,average;
  64.  
  65.  for(int i=0;i<ROW;i++){
  66.    cout<<"\n Averages \n";
  67.     for(int s=0;s<COL;s++){
  68.        msum += math[i][s];
  69.        hsum += history[i][s];
  70.        esum += english[i][s];
  71.     }
  72.       maverage = msum / COL;
  73.       haverage = hsum / COL;
  74.       eaverage = esum / COL;
  75.       cout<<students[i]<<"\n"
  76.       <<"Average,\n"
  77.       <<"Math "<<maverage<<"\n"
  78.       <<"History "<<haverage<<"\n"
  79.       <<"English "<<eaverage<<"\n";
  80.    maverage=0; haverage=0; eaverage=0;
  81.    msum=0; hsum=0; esum=0;
  82.  }
  83.  
  84. return 0;
  85. }
  86.  
  87. void createStudents(string *s, int r){
  88. // auto create students names
  89.   string name;
  90.     for(int i=0;i<r;i++)
  91.     {
  92.       auto name = "bob " + to_string(i+1);
  93.       s[i] = name;
  94.     }
  95. }
  96. void enterGrades(string *s,int m[][COL],
  97. int h[][COL],int e[][COL],
  98. int r,int c)
  99. {
  100. //enter grades
  101.  for(int i=0;i<r;i++)
  102.  {
  103.    cout<<"Enter "<<c<<" grades percent "
  104.    <<"for\n "<<s[i]<<" "
  105.    <<"\nMath \n";
  106.      for(int g=0;g<c;g++)
  107.       cin>>m[i][g];
  108.     cout<<"History \n";
  109.    for(int p=0;p<c;p++)
  110.       cin>>h[i][p];
  111.    cout<<"English \n";
  112.    for(int k=0;k<c;k++)  
  113.       cin>>e[i][k];
  114.  }
  115. cout<<"\n\n";
  116. }
  117. void gradesLowHigh(string *s,int m[][COL],
  118. int h[][COL],int e[][COL],int r,int c)
  119. {
  120. int ck=0;
  121.     for(int i=0;i<r;i++){
  122.        ck++;
  123.        cout<<"Student Name\n"<<s[i]<<"\n";
  124.      if(ck==1)
  125.       {
  126.         cout<<"Highest Grade\tLowest Grade\n";
  127.         ck=0;
  128.       }
  129.        cout<<"\nMath\n"
  130.        <<m[i][0]<<"\t\t"<<m[i][c-1]<<"\n"
  131.        <<"History\n"
  132.        <<h[i][0]<<"\t\t"<<h[i][c-1]<<"\n"
  133.        <<"English\n"
  134.        <<e[i][0]<<"\t\t"<<e[i][c-1]<<"\n\n";
  135.     }
  136. }
  137.  
  138.  
  139. void listGradesDescendingOrder(string *s,
  140. int m[][COL],int h[][COL],int e[][COL],
  141. int r,int c){
  142.  
  143.   cout<<"\nGrades\n";
  144.   for(int i=0;i<r;i++){
  145.       cout<<"Student Name\n";
  146.       cout<<s[i]<<"\n";
  147.       cout<<"\nMath\n";
  148.      for(int k=0;k<c;k++)
  149.          cout<<m[i][k]<<" ";
  150.          cout<<"\nHistory\n";
  151.        for(int j=0;j<c;j++)
  152.            cout<<h[i][j]<<" ";
  153.            cout<<"\nEnglish\n";
  154.          for(int n=0;n<c;n++)
  155.              cout<<e[i][n]<<" ";
  156.              cout<<"\n";
  157.    }
  158. }
  159. void sortRow(int list[][COL],int r,int c){
  160. //loop rows
  161. for(int i=0;i<r;i++){
  162.  //loop column
  163.  for(int j=0; j<c;j++){
  164.   //loop comparison and swap
  165.    for(int k=0;k<c-j-1;k++){
  166. //  if(list[i][k] > list[i][k+1]){
  167.   if(list[i][k] < list[i][k+1]){
  168.        //swap ellrment
  169.        swap(list[i][k],list[i][k+1]);
  170.      }
  171.     }
  172.    }
  173.   }
  174. }
  175.  

Reply to "Students"

Here you can reply to the paste above