By the time you have completed this lab, you should be able to
If your assigned partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.
This lab's first pilot should log in, create ~/cs16/lab05/ and make that your current directory.
Here is a skeleton of the program you will work on for this lab: feetmeters.cpp - download a copy of it now, and store it in the first pilot's lab05 directory.
The program is not ready to make executable until you define the four functions that are declared above the main function. But you can read the relatively short main function, and know what is supposed to happen:
int main() { int feet = 0, inches = 0, meters = 0, centimeters = 0; double dfeet; get_length(feet, inches); dfeet = feet_plus_fraction(feet, inches); convert(dfeet, meters, centimeters); show_results(meters, centimeters); return 0; }That's procedural abstraction in a nutshell: the main function is complete, assuming the four functions it uses are done and correct.
Here are the four function prototypes with comments explaining what each is supposed to do:
void get_length(int &feet, int &inches); // prompts user to enter feet and inches, and stores user input // Post-condition: feet and inches are set to user input data double feet_plus_fraction(int feet, int inches); // returns double value equal to feet with additional inches // e.g., feet_plus_fraction(3, 9) evaluates to 3.75 void convert(double dfeet, int &meters, int ¢imeters); // Post-condition: meters and centimeters are set to be the // metric equivalent of the length in feet stored in dfeet void show_results(int meters, int centimeters); // Neatly prints results and a newline to cout. // e.g., "conversion: 14 meters and 63 centimeters"That is all a client (i.e., the writer of function main) must know to properly use these procedures.
Finally, here are two sample runs from our solution (user input is boldface):
-bash-4.3$ ./feetmeters enter feet and inches 8 4 conversion: 2 meters and 54 centimeters -bash-4.3$ ./feetmeters enter feet and inches 100 0 conversion: 30 meters and 48 centimeters
One of the best and most important benefits of using procedural abstraction is the ability to test each function as a separate unit to know it is working properly. Normally this testing process involves separate "driver" programs for each function. Today you will just use the existing main function to drive the tests, but you will do that with each function in turn by inserting temporary print statements. After testing the fourth function successfully, you will remove these temporary print statements before submitting the program for credit.
get_length
- this function must prompt the user with cout exactly as follows
(including the newline at the end of prompt, and no trailing spaces):
enter feet and inchesAfter the prompt, use cin to store the user's input in the two reference variables, feet and inches. When you finish that, test the function before continuing by inserting the following statement (or one essentially like it) into main right after the call to get_length:
cout << "*** user entered: << feet << " and " << inches << endl;Then use make to compile the program and run it, just to verify that the user's input was successfully stored in those variables. If not, fix your code and try again until you know this function works properly.
feet_plus_fraction
- this function should return a double value that is
equal to the number of feet, plus the number of inches divided by 12. You must
use floating point, not integer division to perform this calculation. Remember
the static_cast operator that converts from one type to another - for example,
the following operation would produce a double value equal to 7.0:
static_cast<double>(7)Make sure this function works properly by inserting another print statement into the main function right after the call to feet_plus_fraction to verify the value of dfeet.
convert
- this function must calculate the number of meters and then
the number of additional centimeters that are (roughly) equivalent to the
number of feet represented by the double parameter dfeet. There are 0.3048
meters in one foot. Use what you've learned about integer division to find
these two metric lengths, and store the results in the reference variables
named meters and centimeters. Again, test your function by inserting another
temporary print statement in main right after the call to convert - verify
your results are correct before continuing.show_results
- this function must show the results exactly like they
are shown in the sample runs above. Verify the results are correctly shown.Remove (or use "//" to comment out) the temporary print statements before submitting your code in the next step.
If you are still logged on, and your current directory is still the same as your two programs, then the quickest way to submit is by entering the following command (suggest you copy/paste):
~submit/submit -p 955 feetmeters.cppOtherwise you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser - submit feetmeters.cpp for Lab05. A perfect score is 50/50 points.
If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.
Each student must accomplish the following to earn full credit [50 total points] for this lab:
The deadline for submitting your work for credit is tonight by 11:59 pm.
Optional Extra Challenges
Do not change feetmeters.cpp in response to these challenges, and do not resubmit it. Write new progams instead to accomplish the following. These are not being done for credit right now, just for practice. But you should be able to use some of this work for PA4.
- Write a program that will do the opposite conversion for a user, one that converts meters and centimeters entered by the user to feet and inches. Name it something like metersfeet.cpp. You can probably use many parts of feetmeters.cpp to begin, and then just make the specific changes to parts that need changing.
- Write another version of feetmeters.cpp (or metersfeet.cpp or both) that lets the user continue to convert lengths as long as the user wants to continue. This enhancement just involves putting the main function's code inside a loop, and adding some user interaction to find out whether or not the user wants to continue.
Prepared by Michael Costanzo.