Course List: http://www.c-jump.com/bcc/
Write a C++ program that asks the user,
What's your speed (MPH)?
How many miles do you have to travel?
and then prints the estimated travel time in minutes. For example, the program dialog with the user could look like this (user responses are highlighted):
What's your speed (MPH)? 65 How many miles do you have to travel? 30 Your travel time will be 28 minutes.
For best results, the program should make calculations using fractional numbers. In C++, fractions are typically represented by the double data type.
The program must give an answer with an accuracy to one minute. In other words, while internal calculation may be using fractional numbers, the printed result should be properly rounded to an integer. For example, "27.69 minutes" needs to be rounded to "28 minutes", like it is done in the sample program dialog above.
In chapter 3, section 3.9.2 of the textbook, there is an example of incorrect conversion from a double to an int:
double x = 2.7; // lots of code int y = x;
Unfortunately, double-to-int conversion in C++ truncates (always rounds down) rather than using conventional arithmetic rounding. As a result, the variable y in the above fragment will contain the value 2, not the expected 3.
To remedy the problem, we can make the following changes:
double x = 2.7; // lots of code int y = int( x + 0.5 ); // rounding problem fixed!
The change fixes the rounding problem by adding 0.5, always bringing the rounding floor to a mathematically correct value that can be safely rounded down before double-to-int conversion takes place. It is also better to use the expression
int y = int( x + 0.5 ); // no warning: the programmer is aware of double-to-int conversion
rather than simply
int y = x + 0.5; // warning: possible loss of data
because the latter generates C++ compiler warning about a possible loss of data.
Naturally, the program needs variables for speed (or velocity), distance, and calculated travel time. If you need other variables, feel free to add them as you see fit.
The program calculations are derived from one of the simplest formulas in Physics:
velocity = distance / time
which can also be rewritten as
time = distance / velocity
The program calculates travel time in minutes. There are 60 minutes in one hour:
minutes = hours * 60
To make accurate calculations, you should be using fractional numbers. For example,
double speed; // miles per hour int distance; // miles //... double travel_time = distance / speed; // this gives you travel time in hours // and so on...
Ask the user for one more parameter: the speed limit. Print driving time at the speed limit, driving time at the speed entered by the user, and the difference between the two. Print all results on the screen.
Select Weekly Assignments ->
Assignment a3
and upload your .CPP source file.
Note: the system will display an 8-digit confirmation number once the file is successfully uploaded. You can save the confirmation number for your own records, but this is not a required step and no further action is needed on your part.
PLEASE DO NOT send your EXE files. I only need your source code to grade the assignment.