The problem
A moving car is being braked at a constant acceleration.
Compute whether or not the car has come to a complete halt. Read
(the initial velocity),
(the acceleration--which is
negative to represent braking), and
(the amount of braking
time) from the keyboard. Use appropriate prompts to for each of
these. You should note in your prompts that
and
should
be positive, and
should be negative. If the quantity
(
times the absolute value of
) is greater than or equal to
the initial velocity
, the car has come to a complete stop;
otherwise the final velocity is
. Print the final velocity
or the message, ``The car has come to a complete stop."
Next, your program should print out the distance the car has travelled while braking. If the car has come to a complete stop, this distance is given by
Sample runs
Here are two sample runs of my program:
<pegasus> a.out Enter an initial velocity (a positive number): 10 Enter an acceleration for braking (a negative number): -2 Enter the time allowed for braking (a positive number): 3 The final velocity is 4. The distance the car has travelled while braking is 21. <pegasus>
<pegasus> a.out Enter an initial velocity (a positive number): 8 Enter an acceleration for braking (a negative number): -5 Enter the time allowed for braking (a positive number): 2 The car has come to a complete stop. The distance the car has travelled while braking is 6.4. <pegasus>
How to do it
You should use an if else statement to separate the two
cases.
Hints
The square of a number x may be represented by x
* x or by pow(x,2). (The function pow is contained
in the header file math.h or cmath.)
The variables
,
and
should be double type.
Recall that the absolute value of a double variable v0
is given by fabs(v0). You will need to include one of the
header files math.h or cmath to use the function
fabs. You probably also want to use variables v_final and
dist to represent the final velocity and the distance
travelled while braking.
Handing in your program
To be announced.
Program requirements
Your program must compile on pegasus using our usual
g++ compiler.
Programs which do not compile will not be graded. Your program should be documented with meaningful variable names, and comments where necessary. Your program should begin with a comment giving your name and a brief description of the program. Your program must contain:
Running my solution
You can run my solution by typing
~loftin/cs101/solution2
at the <pegasus> prompt.