CSCI 131 Techniques of Programming--Spring 2014

    Home | | Schedule | | Assignments | | Lectures | | Resources

    Solution to Lab 3 grades2.cc

    //**********************************************************************
    // grades2.cc
    //     Author:  King/Royden
    //     Date:    February 10, 2014
    //     Class:   CSCI 131
    //     Purpose: This program determines whether a student is passing
    //              based on the average of his or her three grades
    //     Input :  (from standard input) The three grades, as integers
    //     Output:  (to standard output) The student's average
    //**********************************************************************
    
    #include <iostream>
    
    using namespace std;
    
    int main ( void )
    {
       float test1;         // Grade #1
       float test2;         // Grade #2
       float test3;         // Grade #3
       float average ;      // Average of a student's three grades
       int averageRounded;  // Rounded Average
       char grade;          // Overall Grade
    
       // Prompt the user for the input
    
       cout << "Enter 3 grades for a student: ";
       cin >> test1 >> test2 >> test3;
    
       // Calculate average and rounded average
    
       average = (test1 + test2 + test3)/(3.0);
       averageRounded = int(average + 0.5); 
       
       // Determine the letter grade
    
       if ((test1 < 0) || (test2 < 0) || (test3 < 0)) {
          cout << "Invalid data." << endl;
          return 1; 
       }
       else if (averageRounded >= 90) {
          grade = 'A';
       }
       else if (averageRounded >= 80) {
          grade = 'B';
       }
       else if (averageRounded >= 70) {
          grade = 'C';
       }
       else if (averageRounded >= 60) {
          grade = 'D';
       }
       else {
          grade = 'F';
       }
    
       // Print results
           
       cout << endl << "Exams: " << test1 << " "  << test2 << " " << test3 << endl;
       cout << "Average: " << averageRounded << "    " << "Grade: " << grade << endl;
    
       return 0;
    } // end of main
    

    Home | | Schedule | | Assignments | | Lectures | | Resources



    CSCI 131: Techniques of Programming
    Last Modified: February 10, 2014
    Page Expires: February 10, 2015