1 / 20

CS320n –Visual Programming

CS320n –Visual Programming. Introduction to Recursion (Slides 8-1). Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas. What We Will Do Today. Look at the programming technique known as recursion. Repetition. repetition definite, counted -> loop

Download Presentation

CS320n –Visual Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

  2. What We Will Do Today • Look at the programming technique known as recursion Introduction to Recursion

  3. Repetition • repetition • definite, counted -> loop • indefinite, as long as some condition is true -> while • In some situations, we don’t know exactly how many times a block of instructions should be repeated. • repeat until some condition is true • the repetition of step may help get closer to the condition being true • Games with decisions such as checkers or chess. • don’t know how many moves it will take to reach the end of the game or a stalemate Introduction to Recursion

  4. Indefinite Repetition • In programs where a count of repetitions is not known (indefinite), we can use one of two repetition control mechanisms: • While statement, last time • Recursion, today Introduction to Recursion

  5. Recursion • Many of the pieces we use to create a program are identified by using special words. For example, • Do in order • Do together • If/Else • Loop • Recursion is not a program statement with a special word that identifies it as part of the programming language. • Recursion means that a method (or a function) calls itself. Introduction to Recursion

  6. Horse race In repeated moves, one horse is randomly selected to move forward. The selected horse moves straight ahead to the finish line. First horse to the finish line wins Example – horse race Introduction to Recursion

  7. Storyboard race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount do everything again • "do everything again" means that the entire method should be repeated • this is recursion • could this be done with a while loop? Introduction to Recursion

  8. Do everything again? • How do we implement “do everything again” ? • Create a call to the race method itself. • Recursion means that a method calls a copy of itself. race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount call the race method Introduction to Recursion

  9. Stepwise Refinement race If one of the horses has won the winner says, “I won!!!” Else randomly choose one horse and move it forward a small amount call the race method isGameOver? whichHorseWon? moveRandomHorseForward Introduction to Recursion

  10. function is Game Over • world level function • returns a boolean • is the finish line < 0.5 meters in front of any horse? • remember, center point not at the front • if so, game is over • return true Introduction to Recursion

  11. which Horse Won • world level function • returns an object • which horse is within 0.5 meters of finish line? • or which horse is closest to finish line • or which horse is within winning distance • return that horse object Introduction to Recursion

  12. move Random Horse Forward • world level method • pick one of the horses at random and move it forward between 0.05 and 0.15 meters • how to pick a horse at random? • use the world level function Introduction to Recursion

  13. First Attempt Introduction to Recursion

  14. race Method • uses recursion • where is the “way out?” Introduction to Recursion

  15. Testing • Testing a program that used random numbers requires extra caution. • In this example, we ran the program 20 times and found that • racehorse1 won 3 times • racehorse2 won 0 times • racehorse3 won 17 times • Something is wrong! Each horse should win approximately 1/3 of the time. Introduction to Recursion

  16. Removing the bug • The bug in this code is that we have • nested If statements, and • we used a 33% probability for each If statement • What we didn't consider is that if racehorse1 was not selected, then we have a 50% probability of selecting either racehorse2 or racehorse3. Introduction to Recursion

  17. Probability Tree 1/3 of the time Select race horse 1 2/3 s of the time Don’t select race horse 1 2/3s of the time Select race horse 1 1/3 of the time Select race horse 2 1/3 of timerace horse 1moves 2/9 s of timerace horse 2moves 4/9 s of timerace horse 2moves Introduction to Recursion

  18. Correct Version of move Random Horse Forward Introduction to Recursion

  19. Retest • Run Program 20 times • racehorse1 won 6 times • racehorse2 won 6 times • racehorse3 won 11 times • Appears problem is fixed • Each horse should win approximately 1/3 of the time • more than 20 tests to really check program Introduction to Recursion

  20. Modifying the Horse Race • Change the race so that all three horses move a random distance at the same time • easy to change with a Do Together block, but what else needs to change? Introduction to Recursion

More Related