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.
total_distance(100, 10)→299.609375total_distance(100, 1)→100.0total_distance(100, 3)→250.0double total_distance(double height, int times)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;
}
Run tries the examples. When they look right, Submit.