#include #include // std::round(...) // Query the user for a floating-point grade between 0 and 100, // inclusive, and assign it to 'grade'. If the grade is outside // this range, print "Invalid grade" and return -1. // // First, determine and display the corresponding letter grade // according to the following rules: // // grade < 50 F // 50 <= grade < 60 D // 60 <= grade < 70 C // 70 <= grade < 80 B // 80 <= grade < 90 A // 90 <= grade A+ // // Next, calculate the corresponding grade-point value on a // scale from 0.0 to 4.0. // // Some universities convert percentage grades into grade points, // which are then averaged to calculate a grade-point average // (GPA). Thus, the value calculated in this exercise is the grade // point for one grade; a GPA is the average of the grade points // from multiple courses. // // For this exercise, use the following simplified approximation: // // grade < 50 grade point = 0.0 // 50 <= grade < 95 convert the grade linearly // 95 <= grade grade point = 4.0 // // For grades from 50 up to 95, the grade-point value increases // linearly from 0.0 to 4.0. Assign the calculated grade-point // value to the floating-point variable 'ans'. // // Display the grade-point value rounded to one decimal place // using // // std::round( 10.0*ans )/10.0 // // Hint: // // For the letter grade, consider testing the boundaries from // highest to lowest. If you have already determined that the // grade is less than 90, for example, then when you test whether // it is at least 80, you do not also have to test that it is // less than 90. // // For the grade-point value between 50 and 95, first subtract 50 // so that a grade of 50 corresponds to 0. Then determine what // factor will make a grade of 95 correspond to 4.0. int main(); int main() { // This rounds 10*gpv to an integer, and then divides by 10. // - For example, if gpv == 3.7293, then 10*gpv == 34.793, // which rounds to 35, and divided by 10 yeilds 3.5 std::cout << "The grade-point value is " << std::round( 10.0*gpv )/10.0 << "." << std::endl; return 0; } /* * Solution: * * #include * #include * * int main(); * * int main() { * int grade{}; * * std::cout << "Enter a grade: "; * std::cin >> grade; * * if ( grade < 0.0 || grade > 100.0 ) { * std::cout << "Invalid grade" << std::endl; * return -1; * } * * std::cout << "The letter grade is "; * * if ( grade >= 90.0 ) { * std::cout << "A+"; * } else if ( grade >= 80.0 ) { * std::cout << "A"; * } else if ( grade >= 70.0 ) { * std::cout << "B"; * } else if ( grade >= 60.0 ) { * std::cout << "C"; * } else if ( grade >= 50.0 ) { * std::cout << "D"; * } else { * std::cout << "F"; * } * * std::cout << "." << std::endl; * * double gpv{}; * * if ( grade >= 95 ) { * gpv = 4.0; * } else if ( grade >= 50 ) { * gpv = (grade - 50.0)*4.0/45.0; * } else { * gpv = 0.0; * } * * std::cout << "The grade-point value is " * << std::round( 10.0*gpv )/10.0 * << "." << std::endl; * * return 0; * } */