C Exercise 20 - Free-Falling Ball
100 Classic C Language Examples
**Problem:** A ball is dropped freely from a height of 100 meters. Each time it hits the ground, it bounces back to half of its previous height and falls again. Find the total distance traveled when it hits the ground for the 10th time, and how high it bounces on the 10th rebound.
**Analysis:** See the comments below.
Program source code:
Example
#include
<
stdio.h
#define
INITIAL_HEIGHT
100
.0
#define
BOUNCE_COUNT
10
int
main
(
)
{
double
h
=
INITIAL_HEIGHT;
double
s
=
INITIAL_HEIGHT
;
// First rebound height
h
/=
2
;
// Compute subsequent rebound heights and the total distance
for
(
int
i
=
2
;
i
<=
BOUNCE_COUNT
;
i
++
)
{
s
+=
2
h
;
h
/=
2
;
}
// Output the results, specifying 2 decimal places
printf
(
"
At the %d-th landing, the total distance traveled is %.5f meters, and the %d-th rebound height is %.5f meters
\
n
"
,
BOUNCE_COUNT
,
s
,
BOUNCE_COUNT
,
h
)
;
return
0
;
}
The output of the above example is:
第10次落地时,共经过299.60938米,第10次反弹高0.09766米