- Consider the function
int F ( int n )
{
return ( n < 1 ? 1 : 2*n + F(n-1) );
}
Determine the value of F(5).
- 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.
- Define an iterative implementation of
int Search ( const char s[], char c ) .
- 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).
- 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.
- 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.
- Define float xrec ( int n, float c ) which returns x(n),
computed recursively.
- Define float xitr ( int n, float c ) which returns x(n),
computed iteratively.
- 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.
- Give a recursive implementation of Search.
- Give an iterative implementation of Search.
- 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.