Kodewick
Start learning →
Basic←→0 / 50

Bouncing ball

#025Basictotal += 2 * h6 min

A ball drops from height metres. Each time it lands, it bounces up to half its last height and falls again. Write total_distance(height, times): how far has it gone at landing number times? From 100 metres: the 1st landing is after 100 m; it bounces to 50 m and falls, so the 2nd landing is 200 m in total.

Examples
total_distance(100, 10)→299.609375
total_distance(100, 1)→100.0
total_distance(100, 3)→250.0
The functiondouble total_distance(double height, int times)
main.c
double total_distance(double height, int times) {
    double total = height;
    double h = height / 2;
    for (int i = 2; i <= times; i++) {
        // your code here: add this bounce up and down, then halve the bounce height
    }
    return total;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.