Answered step by step
Verified Expert Solution
Question
1 Approved Answer
def calculate _ total _ distance ( initial _ height, bounciness _ index, num _ bounces ) : Calculate the total distance
def calculatetotaldistanceinitialheight, bouncinessindex, numbounces:
Calculate the total distance traveled by a bouncing ball.
Parameters:
initialheight: Initial height from which the ball is dropped.
bouncinessindex: Ratio of bounce height to drop height.
numbounces: Number of times the ball is allowed to bounce.
Returns:
Total distance traveled by the ball.
totaldistance initialheight # Initial drop
# FOR loop implementation
for in rangenumbounces:
bounceheight initialheight bouncinessindex
totaldistance bounceheight # Distance to the floor Distance back up
initialheight bounceheight
return totaldistance
def calculatetotaldistancewhileinitialheight, bouncinessindex, numbounces:
Calculate the total distance traveled by a bouncing ball using a WHILE loop.
Parameters:
initialheight: Initial height from which the ball is dropped.
bouncinessindex: Ratio of bounce height to drop height.
numbounces: Number of times the ball is allowed to bounce.
Returns:
Total distance traveled by the ball.
totaldistance initialheight # Initial drop
bouncecount
# WHILE loop implementation
while bouncecount numbounces:
bounceheight initialheight bouncinessindex
totaldistance bounceheight # Distance to the floor Distance back up
initialheight bounceheight
bouncecount
return totaldistance
# User inputs
initialheight floatinputEnter the initial height from which the ball is dropped in feet:
bouncinessindex floatinputEnter the bounciness index eg for bounce:
numbounces intinputEnter the number of bounces:
# Calculate and display total distance using FOR loop
totaldistancefor calculatetotaldistanceinitialheight, bouncinessindex, numbounces
printfTotal distance traveled using FOR loop: totaldistancefor:f feet"
# Calculate and display total distance using WHILE loop
totaldistancewhile calculatetotaldistancewhileinitialheight, bouncinessindex, numbounces
printfTotal distance traveled using WHILE loop: totaldistancewhile:f feet"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started