Download presentation
Presentation is loading. Please wait.
Published byMarcia Randall Modified over 9 years ago
1
CS 241 – Computer Programming II Lab Kalpa Gunaratna – kalpa@knoesis.orgkalpa@knoesis.org http://knoesis.wright.edu/researchers/kalpa/
2
Contact Contact by e-mail kalpa@knoesis.org kalpa@knoesis.org Office hours 376 Joshi Mondays & Wednesday 3:00 pm – 4:00 pm
3
Recursion Most of the things could be done with loops could be done with recursion as well. Sometimes recursion is more meaningful when writing code. Easy to write. Have to do it correctly and difficult to debug and find errors. Memory expensive.
4
Simple exampls Factorial Public int factorial(n) { if(n == 0) { return 1; } else { return n * factorial(n – 1); } Base condition Recursive call
5
In lab Implement String reversal algorithm using loops. Then implement the same with recursion. Hints : you can use string concatenation and cuttong a String using substring(, ). Show the code and demonstrate. Discuss what you feel about recursion in this example and explain advantages and disadvantages of recursion.
6
Post lab Calculate all the permutations of a given string using recursion. For a given String, number of permutations is the factorial of its length. Break the problem into sub problems and then think how you can construct a recursive answer (assuming you already got solution for the sub problem and proceed to next step).
7
Lab 5 reminders and mistakes Read the description on what and how to implement. For example, number of balls should be passed to BallPanel constructor.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.