21
The first action the function takes is to check for a very simple case: the
case where the car is already blocked.
Pseudocode for ricochet
void ricochet(Car& moving_car);
22
On the other hand, if the car is not blocked, then some movement is
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
the barrier. In this case, just turn the car around.
void ricochet(Car& moving_car);
23
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
void ricochet(Car& moving_car);
This makes the problem a bit
smaller. For example, if the
100 ft.
24
. . .
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
void ricochet(Car& moving_car);
This makes the problem a bit
99 ft.
25
. . .
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
the barrier. In this case, just turn the car around.
void ricochet(Car& moving_car);
We now have a
smaller version of
99 ft.
26
Once a smaller problem has been created, we can make the key step
of any recursive function:
The ricochet function calls itself to solve the smaller problem!
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
the barrier. In this case, just turn the car around.
void ricochet(Car& moving_car);
Make a recursive
call to solve the
27
Pseudocode for ricochet
void ricochet(Car& moving_car);
99 ft.
28
Pseudocode for ricochet
void ricochet(Car& moving_car);
29
Pseudocode for ricochet
void ricochet(Car& moving_car);
The recursive call
30
Pseudocode for ricochet
void ricochet(Car& moving_car);
The recursive call
Pseudocode for ricochet
if moving_car.is_blocked( ), then the car is already at
void ricochet(Car& moving_car);
The recursive call
32
. . .
Pseudocode for ricochet
void ricochet(Car& moving_car);
33
Pseudocode for ricochet
void ricochet(Car& moving_car);
34
Pseudocode for ricochet
void ricochet(Car& moving_car);