Hey so I need help with writing a recursion function that that calculates the time needed to travel a given. You input a distance. If the distance is 1 metre or less, then the amount of time will always be 1 minute. If the distance is greater than 1 metre, the function should take the distance, half it, then repeat the function until the distance is less than 1. In doing so, it should keep track of the amount of times the function was executed, or the number of minutes in comparison. However my code only loops through the else if statement one and always outputs 1 and im not sure what's cauing that.
On line 27 you do store the return value of a function call. What do you do on line 15?
What does happen there is this: On the first function call (from main) the function either:
A) returns 1
or
B) sets i=1 and returns i after the recursive call has returned
A food for thought: doesn't walking X meters mean that it takes some time to walk the first X/2 meters and some time to walk the latter X/2 meters?
Why does the function return a float type object, even though you store it to int (line 27), return value 1 (int by default) or i (int variable)?
OOps, the line 27 thing is a mistake. And it returns value 1 when the distance is less than or equal to 1 since any distance will take exactly one minute. The i is trying to count how long it takes to travel a certain. For example if a ship has to travel 10 meters, it will jump 5 meters in the first minute, 2.5 meters the second minute, 1.25 meters the 3rd minute, and 0.625 meters the fourth minute. Finally, the remaining 0.625 meters takes one more minute. Thus the total time to travel 10 meters is 5 minutes.