CS 117
Midterm 1
Ondich
Due ON PAPER 12:00 noon Friday, February 4, 2000
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 (not even
Shaun Reynolds or the lab assistants) about the exam.
- (10 points) Complete the following function.
////////////////////////////////////////////////////
//
// IsCharInArray returns true if the given
// character (ch) is found at least once in
// the array (a) between indices 0 and N-1.
// Otherwise, IsCharInArray returns false.
//
////////////////////////////////////////////////////
bool IsCharInArray( char ch, char a[], int N )
{
}
- (10 points) Consider the following code.
for( int i=0; i < N; i++ )
{
for( int j=N; j > 0; j-- )
{
cout << i << " " << j << endl;
}
}
- Suppose N is 3. What output does this code produce?
- Pretend that this code takes 2.3 seconds to execute
if N is 10000. Approximately how long will it take to
execute if N is 30000? Justify your answer.
- (12 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. Don't forget that an interface includes
both the function's prototype and a comment describing the behavior
of the function. DO NOT WRITE THE FUNCTIONS THEMSELVES.
- Compute the least common multiple of two positive integers.
- Determine whether two strings have any characters in common.
- 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.
- (16 points) Give brief answers for each of the following.
- (2 points) What book should I read next?
- (10 points) Consider the following InsertionSort function,
which you can also find in
sorts.cpp.
- If you wanted InsertionSort to sort the array in decreasing
order, what would you have to change?
- If you wanted to sort an array of strings (in alphabetical
order) rather than an array of integers, what would you have to change?
////////////////////////////////////////////////////////
// InsertionSort sorts the first N elements of the
// given array in increasing order, using an insertion
// sort algorithm.
////////////////////////////////////////////////////////
void InsertionSort( int a[], int N )
{
for( int i=1; i < N; i++ )
{
int j;
int temp = a[i];
for( j=i; j > 0 && temp < a[j-1]; j-- )
{
a[j] = a[j-1];
}
a[j] = temp;
}
}