CS 117
Midterm 1
Ondich
Due ON PAPER 8:30 AM Friday, October 13, 2000
This is an open book, open notes, and open computer test.
If you get stuck, talk to
Jeff Ondich, but please don't talk to anyone else (not even
Justin Thomson or the lab assistants) about the exam.
The first several problems refer to the
printwords.cpp program
you used as a starting point for your first programming
assignment. For each problem, start with the original
copy of printwords.cpp.
- (4 points) Remove the interface for CleanWord (that is, delete the line
of code immediately preceding the main program).
What error message do you get when you try to compile the
resulting program? Explain why the absence of the interface
causes the compiler to be confused.
- (4 points) If you remove the ampersand (&) from
the CleanWord interface, and then compile and run the program,
what happens? (You need to remove the ampersand in both
places it appears--above main and down where CleanWord is defined.)
Explain why the removal of the ampersand has this effect.
- (9 points) Modify CleanWord so a hyphen at the beginning or end of the word
is removed, while hyphens appearing in the middle of the word
are retained. For example, "pre-eminent" would be unchanged by
CleanWord, but "pre-" or "-pre" would be changed to "pre".
- (10 points) Modify the main program to print the words in reverse order.
Here's an outline of the strategy you should use:
- Declare an array of enough strings to hold all the words
in whatever file you intend to use to test your program.
Something like "string theWords[1000];".
- In the input loop, don't print the cleaned words. Instead,
store each word in an entry of the array. You will need
an integer variable to keep track of the index of the new
word in the array.
- After the input loop, write another loop starting at
the end of the array and moving towards the beginning.
- (2 points) What is dangerous about the strategy in the previous problem?
- (2 points) Please direct me to an interesting or amusing web site.
- (10 points) Consider the following implementation of IsPalindrome.
bool IsPalindrome( const string& s )
{
int left = 0;
int right = s.length() - 1;
while( left < right )
{
if( tolower( s[left] ) != tolower( s[right] ) )
return( false );
else
return( true );
}
}
- If you try compiling this function along with a suitable
main program, the compiler will give you a warning. What is
the warning? What does it mean? How can you fix it?
- Even if you ignore or fix the warning, this function
does not work properly. What's wrong with it? For what kinds
of strings does it return true? How can you fix the problem?
- (5 points) Write a one-paragraph biography of one of the
following people: Charles Babbage, Grace Hopper, or John Mauchly.