L-42 MCS 275 Mon 23 Apr 2001

Review Questions on Chapter 10 and 11

  1. Consider the function
        int F ( int n )
        {
            return ( n < 1 ? 1 : 2*n + F(n-1) );
        }
    
    Determine the value of F(5).

  2. Define a function that returns the position of the last occurrence of a character in a string. For example, Search("book",'o') returns 2. If the character does not occur in the string, then -1 is returned.

    1. Define an iterative implementation of int Search ( const char s[], char c ) .

    2. Define a recursive implementation of int Search ( const char s[], char c, int i, int p ), where i is the current position in the string and p the current last occurrence of the character c. For example, we call the recursive Search like Search("book",'o',0,-1).

  3. Write a function that converts a string of the format lastname, firstname into firstname lastname. For example Convert("Smith, Bob") returns the string "Bob Smith". Do not make any assumptions on the length of the strings.

  4. Consider the recurrence relation
       x(n) = 1 + c*x(n-1) - x(n-2), n >= 2, x(0) = 0, x(1)  = 0
    
    where c is some float.

    1. Define float xrec ( int n, float c ) which returns x(n), computed recursively.

    2. Define float xitr ( int n, float c ) which returns x(n), computed iteratively.

  5. Use binary search to look through an alphabetically sorted list of items. The function int Search ( char *items[], const char *item, int first, int last ) returns -1 if item does not belong to items[first..last], otherwise the index of the matching string in items is returned.

    1. Give a recursive implementation of Search.

    2. Give an iterative implementation of Search.

  6. The function void integ ( float a, float b, float *val, float *err ) returns in val a numerical approximation of the integral of some function f over [a,b]. The error to this approximation is bounded by err. The smaller the interval [a,b], the smaller err.

    Apply divide and conquer and use integ to implement the function float adapint ( float a, float b, float eps ); which returns the value of the integral where every call to integ gave a value for err strictly less than the given eps.

FINAL EXAM in LC C3 on Monday 30 April 2001 at 1:00-3:00PM.