HOMEWORK 05 - CALCULATOR
Concepts
This assignment exercises your ability to define functions and make function calls.
Calculator Functions
One common application for simple programs is to perform conversions or calculations for the user. In this homework assignment, you will write a program that gives the user a few choices for calculations. Specifically, you will write functions to calculate someone's Body Mass Index (BMI), convert from pounds to kilograms, convert from feet to meters, and one additional calculation of your choice.
For each calculation in your program, you should write (at least) two functions. One function should perform the calculation and return the result, and the other function should accept necessary data from the user, call the calculation function, and then display the result. You should also have a menu function which presents the user with the possible choices for calculations (e.g., user can choose BMI calculation or convert from pounds to kilograms).
Why do we divide your program into functions this way? The primary reason for using functions is so that code is more easily identified and reused. This division ensures that your calculation is universal and can be reused elsewhere.
Get Started
To begin, create an empty project. For the main()
, copy/paste the
following into your homework project.
// DO NOT change this main function
Menu();
return 0;
} // end of Main Function
Note that the main function calls a Menu
function, which you
need to write. Note also that you are not allowed to change the main
function. Thus, your Menu
function will need to call another
function which is associated with the calculation chosen by the
user. Fortunately, calling a function from another function works
the same way as calling a function from main (as main is also a function).
You should place your function definitions below your
main()
. To do that, think about what you need to add above
your main()
to give your functions global scope that
can be called from everywhere. (We will discuss function prototypes on
Friday.)
The Functions
Your program must have the following functions that perform the described tasks:
- Menu, which has no parameters and no return value. This function displays the list of available calculations to the user, and then accepts a choice from the user. For practice, use a switch statement to decide which function gets called.
- PoundsToKilograms, which has no parameters and no return value. This function asks the user to input the number of pounds to convert, calls DoPoundsToKilograms to do the calculation, and then outputs the result returned.
- DoPoundsToKilograms, which has one double parameter (pounds) and returns a double (kilograms). This function does the pounds to kilogram conversion, and then returns the results. Note: 1 pound is about 0.454 kilograms.
- FeetToMeters, which has no parameters and no return value. This function asks the user to input the number of feet to convert, calls DoFeetToMeters to do the calculation, and then outputs the result returned.
- DoFeetToMeters, which has one double parameter (feet) and returns a double (meters). This function does the feet to meter conversion, and then returns the results. Note: 1 foot is about 0.3048 meters.
- BodyMassIndex, which has no parameters and no return value. This function asks the user to input height (in feet) and weight (in pounds). Then, this function calls DoPoundsToKilograms and DoFeetToMeters to do conversions, and then calls DoBMI to do the calculation. Finally, this function outputs the result returned.
- DoBMI, which has two double parameters (kilograms and meters) and returns a double (BMI). This function calculates the BMI, and then returns the results. Note: BMI is calculated as (weight (kg)) / (height (m) * height (m)).
- Add (at least) two more functions to your program that calculates something you think would be useful for your calculator (i.e., (1) an input function with no parameters and no return value and (2) a calculation function with at least one parameter used in the calculation). Make this function accessible from your Menu function and test it completely.
Submission
You need to submit your solution of this homework (HW05) with two lab assignments: Lab05A and Lab05B Detailed generic instructions for submitting homework assignments are available. For homework due this Wednesday, follow these specific steps:
- create a directory called week05.
- within week05, create three subdirectories: Lab05A, Lab05B, and HW05.
- within your new week05/Lab05A directory, copy in your main.cpp file from your Lab05A solution.
- within your new week05/Lab05B directory, copy in your main.cpp file from your Lab05B solution.
- within your new week05/HW05 directory, copy in your main.cpp file from your HW05 solution.
- compress the week05 directory (see Step 3 here for details).
- submit the week05.zip file to Blackboard (see Steps 5-10 here for details).
- after you submit, download the file and double check it contains all that you think it contains!