Question
GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the
GPA calculator:
Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work.
We will create a textual menu that shows 3 choices.
1. Enter course grade and units. 2. Show GPA. 3. Quit.
The user is expected to enter 1, and then enter a course grade and its number of units. They can then choose 1 and enter another course grade and units, or choose 2, and the code displays their GPA based on all the courses entered so far. If the user enters 3, the system quits. After the user chooses options 1 or 2, the code executes that menu option then shows the menu again. When the user chooses menu option 3, we do not repeat the menu.
This requires us to write code that follows this logic:
- Write a function named display which has no arguments, and shows the following to the screen: 1. Enter course grade and units. 2. Show GPA. 3. Quit. then asks the user for a number to enter 1,2, or 3. If the user types 1,2, or 3 the function returns that value, otherwise the function displays an error message, and returns -1.
- Write code for a function named main, that does the following - (main has no arguments):
- Call the function display (from step 1), and if the return value is 1
- Get a grade and units from the user. Pass each to a function named legal (see below) to check if the grade is legal (between 0 and 4), and the units are legal (between 1 and 5).
- If legal returns a true value on grade and its valid range, and legal returns a true value on units and its valid range, then accumulate grade*units to a global variable, and accumulate the units to another global variable. Otherwise (either grade or units is illegal) display an error message.
- If from step 1 above, display returns 2, show the current GPA which is totalPoints/totalUnits.
- Call main after the user enters 1 or 2 (yes main will call itself - this is recursion in its simplest form). This allows the code to display the menu again and the user gets to pick to enter more grades, or see their GPA, or quit.
- What do you need to do if display from step 1 return -1 so the user sees the menu and gets to try again?
- If step 1 above returns 3, then show a goodbye message. Make sure NOT to call main in this case.
- The function legal takes 3 integers: amount, upperBound and lowerBound. The function returns a true value if amount is more than or equal to lowerBound and less than or equal to the upperBound. Otherwise the function returns a false value.
- What do you need to do to show how many courses the user took? Write that code.
It is common practice to place the main function code at the bottom and place the other functions above main. If you write more functions that call other functions, then the function that gets called is placed towards the top while the function that calls other functions is placed towards the bottom.
NOTE: DO NOT USE WHILE LOOP AND MAKE SURE THAT YOU FOLLOW THE INSTRUCTIONS CAREFULLY OR YOU WILL SURELY GET A THUMBS DOWN
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started