Question
Can you code this for me? * Dizzy Dean is trying to cross a rope bridge that is 10 meters * long. Due to his
Can you code this for me? * Dizzy Dean is trying to cross a rope bridge that is 10 meters * long. Due to his vertigo, he is as equally likely to step backward * as he is to step forward in trying to get cross the bridge. * If he is at the start of the bridge he always steps forward. How * many steps on average should it take Dean to cross the bridge? * * An "average" is found by simulating the scenario many times, * adding up all the steps and dividing by the number of times * the simulation was run. The more times you run the simulation * the more accurate your average will be. * * PART 1
* Imagine a bridge with steps numbered 1 to 10. Start * Dizzy Dean on step 1. Move Dean randomly forward or backward * by 1 step until he moves past step 10 and off the bridge. Note * from the specification that if Dean is on step 1, he always * moves forward. Your program should print out his location after * every step, all on a single line. For example, it might look * like this: * * 2 3 2 1 2 3 4 5 4 5 6 5 4 5 6 7 8 9 8 9 10 9 10 * * Note that the starting step and the step when Dean crosses the * bridge (step 11 in this case) are NOT printed to the screen. * * PART 2
* Ask the user "How big should the bridge be?" Your * program will have to be able to correctly exit when he crosses. * For example, if the bridge is 10 steps long, Dean crosses on * step 11. If the bridge is 5 steps long, Dean crosses on step 6. * Here is a sample execution for a bridge 5 steps long: * * How big should the bridge be? * 5 * 2 3 2 1 2 1 3 4 3 2 3 4 5 4 5 * * PART 3
* After getting the size of the bridge, run the simulation * 100,000 times, and output the AVERAGE number of steps taken to cross * the bridge.
* ***NOTE: For Part 3 you don't have to print Dean's location after
* every step. Your code will be much faster skipping this output***.
* Here is a sample execution for an 8 step bridge: * * How big should the bridge be? * 8 * Average steps: 64 * * NOTE: To check your algorithm, bridges of the following sizes * should have the following averages: * 5 step bridge => 24 or 25 step average * 8 step bridge => 63 or 64 step average * 10 step bridge => 99 or 100 step average
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