CSCI 159 Examples -- Gara Pruesse's sections
CSCI 159 Example Questions

Example Questions
for CSCI 159, Fall 2024
Computer Science I

  1. Let Fib(0) = 1, Fib(1) = 1, and let Fib(n) = Fib(n-1) + Fib(n-2).
    Write a recursive function that computes and returns the value of Fib(n), where n is a non-negative integer. The prototype for the function would likely be this:

    		int Fib(int n);
    		

  2. Write (without looking at solutions, of which many are provided online!) a C++ function that performs binary search for a key value, where the keys are of type float. The input array is sorted, smallest key first. Specifically the function takes the array, the key, and two indices into the array, right and left, and determines if the key is in the subarray arr[left .. right], and if so returns the index i such that arr[i]=key. The prototype might be:

    		int find( float arr[], int left, int right, float key);
    		


    Assume that the function is never called with right > size-1, where size is the size of the array. If the key is not in the array, the returned value should be -1.

  3. Write a recursive function that finds and prints out all the prime divisors of a positive integer. It should print them out from smallest to largest with an 'x' in between, with duplication if required. Eg., if the input is 140, it prints out "2 x 2 x 5 x 7". (Note: if it is easier (and it is) you can have it print " 2 x 2 x 5 x 7 x")

    The prototype can be this:
    		void printDivisors( int n);
    		

    Write a iterative function that does the same thing.



Useful Links:

Course Outline
Homework and Assignments Gara Pruesse's Homepage
Computing Science Homepage
Vancouver Island University Homepage