Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the code shown below, add exception handling statements ( try, throw, and

ID: 3772471 • Letter: U

Question

Using the code shown below, add exception handling statements (try, throw, and catch). You may write your additional statements directly on the given listing. Have your code print an appropriate message for each exception.

Add exception handling for exam scores less than 0 or greater than 100

Add exception handling for average exam scores less than the min. or greater than the max.

#include <iostream>

#include <iomanip>

using namespace std;

const int students = 3;   // number of students

const int exams = 4;      // number of exams

int minimum( int [ ][ exams ], int, int );

int maximum(int [ ][ exams ], int, int );

double average( int [ ], int );

void printArray( int [ ][ exams ], int, int );

int main()

{

   int studentGrades[ students ][ exams ] =

          { { 77, 68, 86, 73 },

            { 96, 87, 89, 78 },

            { 70, 90, 86, 81 } };

   cout << "The array is: ";

   printArray( studentGrades, students, exams );

   cout << " Lowest grade: "

        << minimum( studentGrades, students, exams )

        << " Highest grade: "

        << maximum( studentGrades, students, exams ) << ' ';

   for ( int person = 0; person < students; person++ )

      cout << "The average grade for student " << person << " is "

           << setiosflags( ios::fixed | ios::showpoint )

           << setprecision( 2 )

           << average( studentGrades[ person ], exams ) << endl;

}

// Find the minimum grade

int minimum( int grades[ ][ exams ], int pupils, int tests )

{

   int lowGrade = 100;

   for ( int i = 0; i < pupils; i++ )

      for ( int j = 0; j < tests; j++ )

         if ( grades[ i ][ j ] < lowGrade )

            lowGrade = grades[ i ][ j ];

   return lowGrade;

}

// Find the maximum grade

int maximum( int grades[ ][ exams ], int pupils, int tests )

{

   int highGrade = 0;

   for ( int i = 0; i < pupils; i++ )

      for ( int j = 0; j < tests; j++ )

         if ( grades[ i ][ j ] > highGrade )

            highGrade = grades[ i ][ j ];

   return highGrade;

}

// Determine the average grade for a particular student

double average( int setOfGrades[ ], int tests )

{

   int total = 0;

   for ( int i = 0; i < tests; i++ )

      total += setOfGrades[ i ];

   return static_cast< double >( total ) / tests;

}

// Print the array

void printArray( int grades[ ][ exams ], int pupils, int tests )

{

   cout << "                 [0] [1] [2] [3]";

   for ( int i = 0; i < pupils; i++ ) {

      cout << " studentGrades[" << i << "] ";

      for ( int j = 0; j < tests; j++ )

         cout << setiosflags( ios::left ) << setw( 5 )

              << grades[ i ][ j ];

   }

}

Explanation / Answer

#include <iostream>
#include <iomanip>
using namespace std;

const int students = 3; // number of students
const int exams = 4; // number of exams

int minimum( int [ ][ exams ], int, int );
int maximum(int [ ][ exams ], int, int );
double average( int [ ], int );
void printArray( int [ ][ exams ], int, int );

int main()
{
int studentGrades[ students ][ exams ] =
{ { 77, 68, 86, 73 },
{ 96, 87, 89, 78 },
{ 70, 90, 86, 81 } };

cout << "The array is: ";
printArray( studentGrades, students, exams );
cout << " Lowest grade: "
<< minimum( studentGrades, students, exams )
<< " Highest grade: "
<< maximum( studentGrades, students, exams ) << ' ';

for ( int person = 0; person < students; person++ )
cout << "The average grade for student " << person << " is "
<< setiosflags( ios::fixed | ios::showpoint )
<< setprecision( 2 )
<< average( studentGrades[ person ], exams ) << endl;
}
// Find the minimum grade
int minimum( int grades[ ][ exams ], int pupils, int tests )
{
int lowGrade = 100;

for ( int i = 0; i < pupils; i++ )

for ( int j = 0; j < tests; j++ )

if ( grades[ i ][ j ] < lowGrade )
lowGrade = grades[ i ][ j ];
return lowGrade;
}
// Find the maximum grade
int maximum( int grades[ ][ exams ], int pupils, int tests )
{
int highGrade = 0;

for ( int i = 0; i < pupils; i++ )

for ( int j = 0; j < tests; j++ )

if ( grades[ i ][ j ] > highGrade )
highGrade = grades[ i ][ j ];

return highGrade;
}
// Determine the average grade for a particular student
double average( int setOfGrades[ ], int tests )
{
int total = 0;

for ( int i = 0; i < tests; i++ )
{
   try
   {
       if(setOfGrade[i]<0 && setOfGrade[i]>100)
       throw setOfGrade[i];
   }
total += setOfGrades[ i ];
}
catch (int num)
{
   cout<<"Marks are not correct";
}
return static_cast< double >( total ) / tests;
}
// Print the array
void printArray( int grades[ ][ exams ], int pupils, int tests )
{
cout << " [0] [1] [2] [3]";

for ( int i = 0; i < pupils; i++ ) {
cout << " studentGrades[" << i << "] ";

for ( int j = 0; j < tests; j++ )
cout << setiosflags( ios::left ) << setw( 5 )
<< grades[ i ][ j ];
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote