Advertisement
userxbw

Students using 2d arrays subject average c++

Aug 11th, 2022
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. // Amount of  subjects
  7. #define ROW 6
  8. // Amount of grades taken in per subject
  9. #define COL 1
  10. //for holding both student
  11. //and subjects average
  12. #define ACOL 2
  13. #define SIZE_ARRAY(array) (sizeof(array)/sizeof(array[0]))
  14. #define SIZE2D_ARRAY(array) (sizeof(array[0])/sizeof(array[0][0]))
  15. //proto typesj
  16. void
  17. sortRow(int list[][COL],int r,int c);
  18.  
  19. void
  20. getGrades(string *s,
  21. int p[][COL],int m[][COL],
  22. int a[][COL],int e[][COL],
  23. int h[][COL],int b[][COL],
  24. string *sb, int r,int c);
  25.  
  26. void
  27. Average(string *s,
  28. int p[][COL],int m[][COL],
  29. int a[][COL],int e[][COL],
  30. int h[][COL],int b[][COL],
  31. double av[][ACOL],int r,int c);
  32.  
  33. void
  34. printStudentAverage (string *s,
  35. int p[][COL],int m[][COL],
  36. int a[][COL],int e[][COL],
  37. int h[][COL],int b[][COL],
  38. double av[][ACOL],int r,int c);
  39.  
  40. void
  41. printSubjectAverage(string *sb,
  42. double av[][ACOL],int r);
  43.  
  44. void
  45. largest(string *s,
  46. double arr[][ACOL], int r,int c);
  47.  
  48.  
  49. int main(){
  50.  
  51. //string arrays
  52. string
  53. subjects[]={
  54. "Physics","Math",
  55. "Arabic","English",
  56. "History","Biology"},
  57. students[]={
  58. "Fadi","Salem",
  59. "Rehab","Ahamad",
  60. "Hahahahab"};
  61.  
  62. /*
  63. 2D arrays
  64. **/
  65. int physics[ROW][COL];
  66. int math[ROW][COL];
  67. int arabic[ROW][COL];
  68. int english[ROW][COL];
  69. int history[ROW][COL];
  70. int biology[ROW][COL];
  71. double average[ROW][ACOL];
  72.  
  73. cout<<"Hello, please input grades for the "
  74. <<"following stidents\n";
  75. getGrades(students,physics,math,arabic,
  76. english,history,biology,subjects,ROW,COL);
  77.  
  78. Average(students,physics,math,arabic,
  79. english,history,biology,average,ROW,COL);
  80.  
  81. printStudentAverage(students,physics,math,
  82. arabic,english,history,biology,average,
  83. ROW,COL);
  84.  
  85. printSubjectAverage(subjects,average,ROW);
  86.  
  87.  
  88. largest(students,
  89. average,ROW,COL);
  90. cout<<"\n";
  91.  
  92. return 0;
  93. }
  94.  
  95. void printSubjectAverage(string *sb,
  96. double av[][ACOL],int r)
  97. {
  98.  
  99.   cout<<"\nSubject Averages\n";
  100.    int d=0;
  101.    for(int i=0;i<r;i++){
  102.       cout<<sb[d]<<" "
  103.       <<av[i][1]<<endl;
  104.      d++;
  105.   }
  106. }
  107. void getGrades(string *s,
  108. int p[][COL],int m[][COL],
  109. int a[][COL],int e[][COL],
  110. int h[][COL],int b[][COL],
  111. string *sb,int r,int c)
  112. {
  113.     int i,q,ss=0;
  114.  
  115.   for(i=0;i<r-1;i++){
  116.     //students name
  117.     cout<<"\nEnter grades for "<<s[i]<<"\n";
  118.     for(q=0;q<c;q++){
  119.        cout<<sb[ss]<<endl;
  120.        cin>>p[i][q];
  121.        cout<<sb[++ss]<<endl;
  122.        cin>>m[i][q];
  123.        cout<<sb[++ss]<<endl;
  124.        cin>>a[i][q];
  125.        cout<<sb[++ss]<<endl;
  126.        cin>>e[i][q];
  127.        cout<<sb[++ss]<<endl;
  128.        cin>>h[i][q];
  129.        cout<<sb[++ss]<<endl;
  130.        cin>>b[i][q];
  131.        ss=0;
  132.      }
  133.   }
  134. /*
  135.  for cross compiling.
  136.  clear screen after input
  137. **/
  138. #ifdef __linux__
  139.  system("clear");
  140. #elif _WIN32
  141.  system("cls");
  142. #endif
  143. }
  144. void Average(string *s,
  145. int p[][COL],int m[][COL],
  146. int a[][COL],int e[][COL],
  147. int h[][COL],int b[][COL],
  148. double av[][ACOL],int r,int c)
  149. {
  150.   int x,z;
  151.    double sum=0.0;
  152.  for(x=0;x<r;x++){
  153.    for(z=0;z<c;z++){
  154.     sum += p[x][z];
  155.     sum += m[x][z];
  156.     sum += a[x][z];
  157.     sum += e[x][z];
  158.     sum += h[x][z];
  159.     sum += b[x][z];
  160.     }
  161.    av[x][0]=(sum/r);
  162.    sum=0;
  163.   }
  164. // might as well figure out subject average
  165. //
  166. sum=0;
  167.  for(x=0;x<c;x++)
  168.    for(z=0;z<r;z++){
  169.     sum += p[x][z];
  170.     sum += m[x][z];
  171.     sum += a[x][z];
  172.     sum += e[x][z];
  173.     sum += h[x][z];
  174.     sum += b[x][z];
  175.     av[z][1]=(sum/r);
  176.     sum=0;
  177.   }
  178. }
  179.  
  180.  
  181. void
  182. printStudentAverage (string *s,
  183. int p[][COL],int m[][COL],
  184. int a[][COL],int e[][COL],
  185. int h[][COL],int b[][COL],
  186. double av[][ACOL], int r,int c)
  187. {
  188. int k,t;
  189. cout<<"\nStudent Averages\n";
  190.   for(k=0;k<ROW-1;k++){
  191.      cout<<endl<<s[k]<<" ";
  192.      for(t=0;t<COL;t++)
  193.        cout<<av[k][t];
  194.   }
  195.   cout<<"\n";
  196. }
  197.  
  198. void sortRow(int list[][COL],int r,int c){
  199. //loop rows
  200. for(int i=0;i<r;i++){
  201.  //loop column
  202.  for(int j=0; j<c;j++){
  203.   //loop comparison and swap
  204.    for(int k=0;k<c-j-1;k++){
  205. //  if(list[i][k] > list[i][k+1]){
  206.   if(list[i][k] < list[i][k+1]){
  207.        //swap ellrment
  208.        swap(list[i][k],list[i][k+1]);
  209.      }
  210.     }
  211.    }
  212.   }
  213. }
  214.  
  215. void largest(string *s,
  216. double arr[][ACOL], int r,int c)
  217. {
  218.  
  219.    int i,j,hold;
  220.  
  221.     // Initialize maximum element
  222.  
  223.     double max = arr[0][0];
  224.  
  225.       for(i=0;i<r-1;++i){
  226.         for(j=0;j<c;++j)
  227.            if(arr[i][j]>max){
  228.               max=arr[i][j];
  229.               hold=i;
  230.             }
  231.        }
  232.  
  233. cout<<"\nStudent with highest average \n"
  234. <<s[hold]<<" "<<max<<"\n";
  235.  
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement