Help in stopping distance

Hi all
I really need help down here I had wrote the whole problem correctly without errors but Part b) I couldn't solve it because I didn't get it can any body help me in part b) please ?
.
.
.


If you follow too close behind another car and that other car stops suddenly, you will not be able to stop in time, and you will crash into the rear of that other car. The following formula gives the distance between you and the car in front which allows you to stop safely:

stoppingDistance = speed * (2.25 + speed / 21)

a) Write a program which determines whether a vehicle’s tailgating distance is safe. The program should read in the speed of the vehicle and the vehicle’s tailgating distance. The program should then calculate the distance that would be required to stop the vehicle.

If the stopping distance is less than the tailgating distance, your program should print “No problem.” If the stopping distance equals the tailgating distance, your program should print “Minor wreck.” If the stopping distance is greater than the tailgating distance, your program should print “Major wreck!”

Notice that the expression on the right side of the formula above will force the compiler to convert everything to type double. Use type double for your working variables.

Write your program so that it is able to produce the following screen display:

Sample session:

Enter your speed (in mph): 60.0
Enter your tailgate distance (in feet): 240.0
Major wreck!

b) Notice that one of the tests is a comparison for equality. Since the probability of two real numbers being exactly equal is zero, if you use floating point numbers there will never be an input that produces a minor wreck. Modify the program so that it allows a range of uncertainty of plus or minus 20 feet, within which the result will be a minor wreck. To facilitate adjustment of this range of uncertainty, use a named constant, that is:

RANGE = 40.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int option;
double spead, distance, stoppingDistance;
cout << "Enter 1 for mph/feet and 2 for kmph/m" << endl;
cin >> option;


switch(option)
{
case 1: 


cout << "Enter your spead (in mph)" << endl;
cin >> spead;
cout << "Enter your tailgate distance (in feet)" << endl;
cin >> distance;

stoppingDistance = spead * ( 2.25 + spead / 21 );
if (stoppingDistance < distance)
{
cout << "No problem" << endl;
}
else if (stoppingDistance == distance)
{
cout << "Minor wreck !" << endl;
}
else 
{
cout << "Major wreck !" << endl;
}

break;

case 2:
     
cout << "Enter your spead (in kmph)" << endl;
cin >> spead;
spead = spead / 1000;
cout << "Enter your tailgate distance (in m)" << endl;
cin >> distance;
distance = distance * 3.28;

stoppingDistance = spead * ( 2.25 + spead / 21 );
if (stoppingDistance < distance)
{
cout << "No problem" << endl;
}
else if (stoppingDistance == distance)
{
cout << "Minor wreck !" << endl;
}
else 
{
cout << "Major wreck !" << endl;
}

default: cout<<" "<<endl;
}    

system("PAUSE");
return 0;
}

Last edited on
You didn't post your code but I assume once you receive the user input you put it into the equation and then test the answer with the conditions that tell if it is safe or a major wreck.

Part B is asking you to test a condition after the other tests. You need to calculate the point (I'll call it crash) at which there would be a collision. If the answer you got is less than or equal to crash + (RANGE/2) or greater than or equal to crash - (RANGE/2), then it is in the range of uncertainty.

I am not sure how you wrote your code and tested the conditions so the conditions I gave might be swapped. However the concept is there and I hope it helps!
Last edited on
Thank you very much sir but I still need to know where do I put these things you said
I reposted my code up there.
So the word crash in your code is it the word stopingDistance in mine ?
I was giving a conceptual answer. Do you see how you have

1
2
3
4
5
6
7
8
9
10
11
12
if (stoppingDistance < distance)
{
cout << "No problem" << endl;
}
else if (stoppingDistance == distance)
{
cout << "Minor wreck !" << endl;
}
else 
{
cout << "Major wreck !" << endl;
}


The test with your range constant has to happen here first. Replace the (stoppingDistance == distance) since you are no longer testing it with the range you are testing
Topic archived. No new replies allowed.