kaveman50 Posted October 17, 2009 Share Posted October 17, 2009 Hello. I was wondering if anyone can give me hints on how to write this program... The coefficient of a restitution of a ball, a number between 0 and 1, specifiies how much energy is conserved when a ball hits a rigid surface. A coefficient of .9, for instance, means a bouncing ball will rise 90 percent of its previous height after each bounce. Write a program to input a coefficient of restitution and an intial hright in meters, and report how many times a ball bounces when dropped from its initial height before it rises to a height of less than 10 centimeters. Also report the total distance traveled by the ball before this point. The coefficients of restitution of a tennis ball, basketball, super ball, and softball are 0.7, 0.75, 0.9, and 0.3, respectively. Quote Link to comment Share on other sites More sharing options...
Alex Posted October 17, 2009 Share Posted October 17, 2009 To calculate the number of bounces would be very simple. ex: $height = 100; //initial hight in centimeters $restitution = .7; $bounces = 0; while($height > 10) { $height = pow($height, $restitution); $bounces++; } echo $bounces; As for the distance traveled you'd need to factor another force into the system. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 17, 2009 Author Share Posted October 17, 2009 Wow, that was a quick reply. Thank you very much! Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 Perhaps I'm misunderstanding the problem, but isn't "$height = pow($height, $restitution);" incorrect? After all, if you're initial height is 100 centimeters, and based on the coefficient in his problem (new height = old height * coefficient), your line will give a result of 25.119 centimeters after 1 bounce. But by the definition of his problem (new height = 100 * 0.7) the new height should be 70 centimeters. "$height = pow($height, $restitution);" should be "$height = $height * $restitution;" ? Quote Link to comment Share on other sites More sharing options...
Alex Posted October 17, 2009 Share Posted October 17, 2009 Perhaps I'm misunderstanding the problem, but isn't "$height = pow($height, $restitution);" incorrect? After all, if you're initial height is 100 centimeters, and based on the coefficient in his problem (new height = old height * coefficient), your line will give a result of 25.119 centimeters after 1 bounce. But by the definition of his problem (new height = 100 * 0.7) the new height should be 70 centimeters. "$height = pow($height, $restitution);" should be "$height = $height * $restitution;" ? Ahh, yes. I misread the problem. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 17, 2009 Author Share Posted October 17, 2009 How do you delete posts after they're solved? Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 How do you delete posts after they're solved? You can't, your post might be useful to others if they have a similar problem so it's nice to have them around in case people search for a problem like yours. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 17, 2009 Author Share Posted October 17, 2009 When I enter the code below, all it does is display the text boxes and a 7 below. When I enter numbers to test it, nothing happens. Perhaps something is wrong with my html? <form method='post' action=''> Coefficient of Restitution: <input type="text" name="height" id='num1' /> <br/> <br/> Initial Height in Meters: <input type="text" name="restitution" id='num2' /> <input type='Submit' name='Submit' value="Submit" /> </form> <? $height=$_POST['num1']; $restitution=$_POST['num2']; $height = 100; $restitution = .7; $bounces = 0; while($height > 10) { $height = $height * $restitution; $bounces++; } echo $bounces; ?> Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 Change this: <form method='post' action=''> to <form method='post' action=''> Then change your PHP to this: <? $height=(int)$_POST['num1']; $restitution=(int)$_POST['num2']; $bounces = 0; while($height > 10) { $height = $height * $restitution; $bounces++; } echo $bounces; ?> Quote Link to comment Share on other sites More sharing options...
cags Posted October 17, 2009 Share Posted October 17, 2009 And to capture the total distance travelled (assuming your talking vertical movement only. You should only have to put something like... $distance += $height; ... as the first line of the while loop. It may require some tweaking to make sure your including the first and last bounce, but you should be able to work that out. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 17, 2009 Author Share Posted October 17, 2009 Now it just displays 0 the whole time. I'm probably missing some variables? Thanks for helping. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 And to capture the total distance travelled (assuming your talking vertical movement only. You should only have to put something like... $distance += $height; ... as the first line of the while loop. It may require some tweaking to make sure your including the first and last bounce, but you should be able to work that out. Actually, this will cause it only to measure the "down" vertical distance. If you need to get the total vertical distance traveled: <? $height=(int)$_POST['height']; $restitution=(int)$_POST['restitution']; $distance = height; $bounces = 0; while($height > 10) { $height = $height * $restitution; $distance += $height*2; $bounces++; } echo 'bounces: '.$bounces.'<br/>'; echo 'distance: '.$distance; ?> The fix for the 0 problem is above. Quote Link to comment Share on other sites More sharing options...
cags Posted October 17, 2009 Share Posted October 17, 2009 D'oh, yes, I forgot the reverse of the bounce. That code won't be right either though unless I'm mistaken. The height variable is missing a dollar on line 5 (just a typo I know). Your casting $restitution as an int when its a decimal number. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 Nice catches cags! <? $height=(int)$_POST['height']; $restitution=(float)$_POST['restitution']; $distance = $height; $bounces = 0; while($height > 10) { $height = $height * $restitution; $distance += $height*2; $bounces++; } echo 'bounces: '.$bounces.'<br/>'; echo 'distance: '.$distance; ?> Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 17, 2009 Author Share Posted October 17, 2009 Thanks for the help guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.