Exercise #7a
Suppose we wanted to write a C++ program that prompted the person at the keyboard to enter a saying of some kind which contained ALL lowercase letters and NO punctuation (except for a single period at the end). The program should read in the saying and determine whether or not the saying represents a palindrome. A palindrome is a group of words that contain the same letters in sequence when read forwards or backwards, ignoring spaces. Here are a few example palindromes as they might be entered at the keyboard:
radar.
This is a single word palindrome. Words like "wow" and "noon"
would also qualify.
madam im adam.
Notice the lack of apostrophe in "im" and the lack of
capitalization throughout. Reading backwards we see
the same letters.
a man a plan a canal panama.
This palindrome also lacks punctuation
(commas) and capitalization.
tarzan raised desi arnaz
rat. This is a nonsense
statement, but a
palindrome nonetheless.
Other Examples of Palindromes
able was i ere i saw elba.
was it eliots toilet i saw.
name no one man.
are we not drawn onward we few drawn onward to new era.
norma is as selfless as
i am ron.
sex at noon taxes.
Of course, most sayings are
not palindromes. The idea is to have the program determine whether
or not any given saying qualifies. The program's output should be
a simple "Yes" or "No" indicating that the saying is or is not a palindrome.
Exercise #7b
Suppose we are writing a C++ program in which we have read up to 10,000 numbers from a file into an array named readings. A variable named howMany contains the count of exactly how many numbers were read. We want to slightly rearrange the values in the array so that the largest number is in the first position of the array (position 0) and the smallest number is in the last position (position howMany-1). No values in the array are to be lost in our rearrangement. The value that was in the first position and the one that was in the last position must be moved to other positions in the array when making room for the largest and smallest.
Note: We could sort the array, an act that would put all the values in order from highest to lowest. But, this approach would be doing a great deal more work than is necessary. We should try to do our task efficiently.
How should we go about doing
the task with only a slight rearrangement of the values in the array?