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.
- (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?
- (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 )
{
}
- (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++).
- (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.
- Change all the letters in a string to lower case.
- Determine whether two integers have a common factor
greater than 1.
- Given a string of letters and an integer N between 0 and 25,
create and return a new string where each letter is pushed N
steps down the alphabet. For example, if the string is "ZEBU"
and N is 2, then the resulting string is "BGDW". Note that if
a letter is fewer than N letters from the end of the alphabet,
it wraps around to the beginning of the alphabet. This function
should not alter the original string.
- (20 points) Give brief answers to each of the following questions.
- What is the difference between compile-time errors and
run-time errors? Give an example of each.
- Why would you use "const string&" as the type of a parameter
for a function instead of just "string"?
- Under what sort of conditions would you use "string&" as a
parameter type instead of "string" or "const string&"?
- Why does infinite recursion cause a program to stop running
(usually with some sort of error message or bizarre behavior), but
an infinite loop does not?
- What is the difference between a local variable and a
parameter?
- (2 points) Tell me your favorite joke.
- (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++;
}
}
}
}
- For N = 6, 9, and 12, what output does Mystery produce?
- In one sentence, explain the purpose of Mystery.
- Describe briefly the roles of the local variables p and k.
(If you're having trouble with this, insert cout
statements in various places and watch k and p evolve as Mystery(6)
or Mystery(9) perform their work.)