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 of the textbook, on page 82, 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...
Submit your code, main.cpp, or hw1.cpp, or whatever is the
name of your file, by emailing it as an attachment sent to your instructor:
Igor Kholodov
Igor.Kholodov@bristolcc.edu
PLEASE DO NOT send any projects, ZIP or EXE files, or any other monstrous artifacts! I only need your source code to grade the assignment.
Make sure your email has a proper subject line:
CIS155 Homework 1 Lastname, Firstname
where Lastname, Firstname is your last and first name.
As a general rule, your email must contain your last name (mandatory!) and first name (or at least the first name initial.) Otherwise I will not know who you are.