Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas."— 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 Visual ProgrammingIntroduction to Recursion2 What We Will Do Today Look at the programming technique known as recursion

3 Visual ProgrammingIntroduction to Recursion3 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

4 Visual ProgrammingIntroduction to Recursion4 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

5 Visual ProgrammingIntroduction to Recursion5 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.

6 Visual ProgrammingIntroduction to Recursion6 Example – horse race 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

7 Visual ProgrammingIntroduction to Recursion7 "do everything again" means that the entire method should be repeated this is recursion could this be done with a while loop? 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

8 Visual ProgrammingIntroduction to Recursion8 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

9 Visual ProgrammingIntroduction to Recursion9 Stepwise Refinement isGameOver? whichHorseWon? moveRandomHorseForward 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

10 Visual ProgrammingIntroduction to Recursion10 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

11 Visual ProgrammingIntroduction to Recursion11 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

12 Visual ProgrammingIntroduction to Recursion12 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

13 Visual ProgrammingIntroduction to Recursion13 First Attempt

14 Visual ProgrammingIntroduction to Recursion14 race Method uses recursion where is the “way out?”

15 Visual ProgrammingIntroduction to Recursion15 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.

16 Visual ProgrammingIntroduction to Recursion16 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.

17 Visual ProgrammingIntroduction to Recursion17 Probability Tree 1/3 of the time Select race horse 1 2/3 s of the time Don’t select race horse 1 1/3 of the time Select race horse 2 2/3s of the time Select race horse 1 1/3 of time race horse 1 moves 2/9 s of time race horse 2 moves 4/9 s of time race horse 2 moves

18 Visual ProgrammingIntroduction to Recursion18 Correct Version of move Random Horse Forward

19 Visual ProgrammingIntroduction to Recursion19 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

20 Visual ProgrammingIntroduction to Recursion20 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?


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

Similar presentations


Ads by Google