CS117 Midterm 1, Fall 1999 CS 117
Midterm 1
Ondich
Due ON PAPER 8:30 AM Friday, October 15, 1999

This is an open book, open notes, and open computer test. You may not use any books other than your textbook, and you may not use Internet resources from off campus. If you get stuck, talk to Jeff Ondich, but please don't talk to anyone else about the exam.

  1. (10 points) Write a recursive function that computes the sum of the first N odd integers. Is this a good use of recursion? Why not?

  2. (10 points) Complete the following function.
    
    	////////////////////////////////////////////////////
    	// CountOccurrences returns the number of
    	// times the given integer (N) occurs in the
    	// given array (a) between indices 0 and
    	// arrayLength - 1.
    	////////////////////////////////////////////////////
    
    	int CountOccurrences( int N, int a[], int arrayLength )
    	{
    	}
    


  3. (8 points) Suppose you start with this.
    
    	int		N;
    
    	cout << "Please type a positive integer: ";
    	cin >> N;
    
    Write code to print "Sit on a potato pan, Otis" N-cubed times (that is, N*N*N times). The catch is, you may not use "*" or call any functions. Note that C++ has no exponentiation operator (some languages use "^" for exponentiation, but not C++).

  4. (15 points) For each of the following tasks, design an interface for a function that will perform the task, and give an example of how you would call the function. DO NOT WRITE THE FUNCTIONS THEMSELVES.

  5. (20 points) Give brief answers to each of the following questions.

  6. (2 points) Tell me your favorite joke.

  7. (12 points) Consider the following mystery function.
    
    	void Mystery( int N )
    	{
    		if( N <= 1 )
    		{
    			cout << N << endl;
    		}
    		else
    		{
    			int k = 2;
    			int p = N;
    			while( p > 1 )
    			{
    				if( p % k == 0 )
    				{
    					cout << k << endl;
    					p = p / k;
    				}
    				else
    				{
    					k++;
    				}
    			}
    		}
    	}