Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 150 Untitled Document
Document Actions

Untitled Document

by admin last modified 2005-05-11 13:30

Here are a few practice problems on recursion, which is discussed in Section 10.10 of the text.

1. The Fibbonacci function in Mathematics is defined as follows:
         fib(0) is 1; fib(1) is 1
         For any number i > 1, fib(i) is the sum of the two previous Fibbonacci numbers:
             fib(i-1) + fib(i-2)

   Write a recursive method that returns the ith Fibbonacci number, and use it to make a table
    of the first 20 values of this function.

2. Write a recursive method

int largest( int nums[], int first, int last)

    that returns the largest value of the array "nums" between index "first" and index "last".

3. Write a recursive method

boolean isPalindrome(String s)

    that returns true if its argument is a palindrome (a string, such as "bob", that doesn't change when you reverse it).
    Note that is s is a String of length "len", then s.substring(1, len-1) is all of s except for its first and last characters.

4. Write a pair of methods to print a LinkedList of integers:

void printForwards( Iterator it);
void printBackwards(Iterator it);

    We did this in class, but it would be good practice for you to try to do it yourself. In each of these methods you
     will print one value, then recurse to print the rest. The difference between these two
     methods is just the order of these steps: whether you print the next element, then recurse (printForwards)
    or grab the next element, recurse, then print the element you grabbed.

 

 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: