How many laps a runner can make for 45 minutes with increasing 10% of running time in each lap

3 views (last 30 days)
Hello,
I would like to write a program to calculate how many laps a runner can make for 45 minutes with 10% increase of his or her running time in each lap.
The runner run the first lap in 1 minute.
Here is what I have.
lapcounter=1;
running_time=1;
runningtime_total=0;
runningtime_allowed=45;
number_lap=0;
while lapcounter<=runningtime_allowed
lapcounter=lapcounter+(lapcounter*0.10);
running_time=lapcounter+(lapcounter*0.10);
runningtime_total=running_time+(running_time*0.10);
disp("Time: " + runningtime_total);
number_lap=number_lap+1;
end
disp("Number of lap: " + number_lap);
I think I get the idea of how to calculate running time in each lap like running time = lap time*(lap time*0.10), but I need to add the time the runner has been running so that I can know how many laps he or she can make for 45 minutes.
I appreciate your response in advance.
  4 Comments
the cyclist
the cyclist on 7 Nov 2019
Edited: the cyclist on 7 Nov 2019
Without yet thinking too deeply about your code, I'm pretty sure your while condition is wrong.
You don't want
lapcounter<=runningtime_allowed
which would be a comparison like "6 laps is less than or equal to 45 minutes".
You want to compare your total running time to what is allowed, not your number of laps.
In general, your code seems to badly mix up the lap count and the running time.
A statement like
lapcounter=lapcounter+(lapcounter*0.10)
is going to give you a fractional number of laps, but you just want an integer counter for that. (But you also seem to have two variables for tracking laps for some reason.)
I recommend writing out your algorithm in words or pseudocode first, to make sure that it makes sense conceptually. Then convert it to MATLAB code.
Is this a homework problem?
Takashi Fukushima
Takashi Fukushima on 7 Nov 2019
I would like to explain my understanding a bit.
I think I could write a program which calculates how fast the runner runs the each lap, which is...
lap=1;
total=45;
z=0;
while lap<=total
lap=lap+(lap*0.10);
disp("Time: " + lap)
z=z+1;
end
disp("Number of lap: " +z);
This gives me like
Time: 1.1
Time 1.21
Time 1.331...etc
However, I realize that I have to sum up the time for previous lap to find how much the runner took in total. For example, First lap is 1 min, so the second lap is (1 min + 1.1 min). That is why I created
running_time=lapcounter+(lapcounter*0.10);
runningtime_total=running_time+(running_time*0.10);
I thought that I created the formula of (1 min + 1.1 min) by using the above, but it does not show what I want.
I would put runningtime_total for while loop counter instead of lapcounter, but since the calculation is wrong, it did not work.
This is an exercise my professor gave me.

Sign in to comment.

Accepted Answer

David Hill
David Hill on 7 Nov 2019
function laps=runTrack(t,dt,rt)
%first lap time: t
%total run time: rt
%percent increase in each lap: dt
M=cumsum(t*((1+dt)*ones(1,100)).^(0:99));%pick a number of laps that will not be exceeded (I chose 100)
laps=find(M<=rt,1,'last');
end
The output (laps) gives the total number of laps the runner can complete in rt minutes when the runner's first lap time is t, and it increases by dt each lap.
  1 Comment
Takashi Fukushima
Takashi Fukushima on 7 Nov 2019
Thank you very much for your answer.
I know the code would be messed up compared to your solution, but I would like to stick with while loop and make the program to show the time record for each lap.
For example,
Time after lap 1: 1 Next lap time: 1,1
Time after lap 2: 2,1 Next lap time : 1,21
Time after lap 3: 3,31 Next lap time : 1,331
Time after lap 4: 4,641 Next lap time : 1,4641
Time after lap 5: 6,1051 Next lap time : 1,61051 ...
Amount of laps: xxx
I appologize that I did not put this details on the questions, but thank you so much for your response anyway.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!