steamengines

Static 2D Array

Apr 16th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int NUMB_STUD = 5;    // Number of student records (ROWS)
  6. const int NUMB_GRADES = 3;  // "   " grades per student (COLUMNS)
  7.  
  8. void displayGrades(const int arr[][NUMB_GRADES], const int rowSize);
  9. void getAverage(const int arr[][NUMB_GRADES], const int rowSize);
  10. void getHighest(const int arr[][NUMB_GRADES], const int rowSize);
  11.  
  12. int main()
  13. {
  14.    
  15.     int grades[NUMB_STUD][NUMB_GRADES] = { { 96, 91, 100 },
  16.                                             {88, 93, 84},
  17.                                             {70, 75, 83},
  18.                                             {67, 62, 67},
  19.                                             {98, 85, 86}
  20.                                                         };
  21.     displayGrades(grades, NUMB_STUD);
  22.     getAverage(grades, NUMB_STUD);
  23.     getHighest(grades, NUMB_STUD);
  24.  
  25.  
  26.     system("Pause");
  27.     return 0;
  28. }
  29.  
  30. void displayGrades(const int arr[][NUMB_GRADES], const int rowSize)
  31. {
  32.     cout << "STUDENT GRADES" << endl;
  33.  
  34.     for (int r = 0; r < rowSize; r++)
  35.     {
  36.         cout << "Student " << r + 1 << " Grades: ";
  37.        
  38.         for (int c = 0; c < NUMB_GRADES; c++)
  39.             cout << setw(5) << arr[r][c];
  40.         cout << endl;
  41.     }
  42.     cout << endl << endl;
  43. }
  44.  
  45. void getAverage(const int arr[][NUMB_GRADES], const int rowSize)
  46. {
  47.     float sum = 0.0;
  48.     float average;
  49.    
  50.     cout << "AVERAGE OF GRADES FOR EACH STUDENT" << endl;
  51.     cout << setprecision(1) << fixed;
  52.     for (int r = 0; r < rowSize; r++)
  53.     {
  54.         cout << "Student " << r + 1 << " Grades: ";
  55.    
  56.         for (int c = 0; c < NUMB_GRADES; c++)
  57.  
  58.             sum += arr[r][c];
  59.  
  60.             average = (sum / NUMB_GRADES);
  61.             cout << setw(5) << average;
  62.             cout << endl;
  63.     }
  64.     cout << endl << endl;
  65. }
  66.  
  67. void getHighest(const int arr[][NUMB_GRADES], const int rowSize)
  68. {
  69.     cout << "HIGHEST GRADE FOR EACH EXAM" << endl;
  70.  
  71.     int max;
  72.  
  73.     for (int c = 0; c < NUMB_GRADES; c++)
  74.         cout << setw(8) << right << "Exam " << c + 1;
  75.     cout << endl;
  76.     for (int c = 0; c < NUMB_GRADES; c++)
  77.     {
  78.         max = arr[0][c];
  79.  
  80.  
  81.         for (int r = 0; r < rowSize; r++)
  82.             if (max < arr[0][c])
  83.             {
  84.                 max = arr[0][c];
  85.             }
  86.             cout << setw(9) << max;
  87.     }
  88.         cout << endl << endl;
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment