You are on page 1of 7

COSC 1437 (DL) Spring 2017

Program Set #2
Total Points: 30

Choose 3 of the 6 problems below for full credit. See 1437 Grading Program Sheet for
grading/submission information. Partial credit will be given. (10 points each)

1. Write a C++ program that can sort words in QWERTY order- that is, the order of the letter layout on a
standard QWERTY keyboard. The full sequence is: QWERTYUIOPASDFGHJKLZXCVBNM Input from the
keyboard one word per line until a . (period) is entered. Output the sorted words in QWERTY order one
per line. Make sure to convert output to all capital letters. Output should look similar to below.

Sample Run:

Enter a word: arrest


Enter a word: SUBDIVISION
Enter a word: DISCONTENT
Enter a word: SUPERIOR
Enter a word: Trunk
Enter a word: subdue
Enter a word: .

Words sorted in QWERTY order:

TRUNK
ARREST
SUPERIOR
SUBDUE
SUBDIVISION
DISCONTENT

Name the program: QWERTYSortXX.cpp, where XX are your initials.

2. Word processing programs such as MS Word spilt


words across lines using hyphenation. The word
processor knows how to break words into basic
syllables used for hyphenating words. Write a C++
program that accepts a list of words (a consecutive
string of non-whitespace characters) as input and
prints each word, one per line, with hyphens
inserted at each possible hyphenation point
following simple rules:
If you see the pattern vowel-consonant-
consonant-vowel, hyphenate between the

1
two consonants. The vowels are: a, e, i, o, u and y. The letter y will always be treated as a
vowel in this problem.
If you see the pattern vowel-consonant-vowel, hyphenate before the consonant unless the
second vowel is an e and occurs at the end of the word.
The following character sequences are never divided by hyphens: "qu", "tr", "br", "str",
"st", "sl", "bl", "cr", "ph", "ch".
For the purpose of applying bullets 1 and 2, these are all considered to be a single consonant.
Upper and lower-case distinctions are ignored for applying the above rules, although the case in
the input word must be preserved in the output.
Must use C++ strings/functions in your program. Output should look similar to below.

Sample Run:

Enter the text: The rules given in this problem are a bit crude. But they represent a
good starting point.

The
ru-les
gi-ven
in
this
pro-blem
are
a
bit
cru-de.
But
they
rep-re-sent
a
good
star-ting
point.

Name the program: HyphenationXX.cpp, where XX are your initials.

3. You and your classmates decide to start messaging to help each other, however they must be
encrypted. The problem is Professor C. is fluent in Pig Latin, Klingon, Morse code and the Matrix. There
isn't a common foreign language spoken in the class either. Then you discover that Professor C. cannot
speak Zombie! Write a C++ English-to-Zombie translator so you and your classmates can encrypt
messages! Translation rules (All rules should be executed in this order):
All occurrences of characters e i o u (case insensitive) are replaced with r
All characters other than z h r g b m n a . ! - ? and space (case-insensitive) are stripped
Lower-case r at the end of words replaced with rh
An a (case insensitive) by itself will be replaced with hra

2
All capitalization should be retained, meaning any word in the English phrase that is capitalized
should remain capitalized in the translation. For example, Panama will translate to Anama.
Finally, ask the user if he/she wishes to run the program again. Use the C++ string class. Output should
look similar to below.

Sample Runs (2):

Enter English text: The quick brown fox jumped over the lazy dog.

Zombie Translation: Hrh rrh brrn rh rmrh rrrh hrh az rg.

Run Again (Y/N): y

Enter English text: A man, a plan, a canal, Panama. Palindrome!

Zombie Translation: Hra man hra an hra ana Anama. Arnrrmrh!

Run Again (Y/N): N

Name the program: ZombieSpeakXX.cpp, where XX are your initials.

4. LISP, or LISt Processing, is a recursive based programming language, used in artificial intelligence
processing, and involves the use of lists to process data. In this program, only the math operations will
be explored. Every list is contained within parentheses, the basic structure in LISP. For example, (ADD 5
8) is simply 5 + 8, or 13. Write a C++ program that evaluates mathematical LISP expressions. Input from
the keyboard will use expressions involving ADD, SUB, MUL, and DIV, each of which are contained in a
list. Each operation begins with a three letter abbreviation, followed by a single space, followed by the
operands involved, also with single space separation. ADD and MUL may have 2 or more operands, while
DIV and SUB will only have 2. Each individual list will have no more than one imbedded list as an
operand, and only at the end of that list, but each expression may have multiple imbedded lists, as can
be seen in the sample runs below. Assume proper formation when entered from the keyboard. Output
the result of the mathematical expression. Use C++ strings. Output should look similar to below.

Sample Runs (2):

Enter the LISP expression: (SUB 6 3)

The result is: 3

Enter the LISP expression: (ADD 4 6 (MUL 4 5 (DIV 8 4)))

The result is: 50

Name the program: LISPExpressionsXX.cpp, where XX are your initials.

3
5. Your paranoid friend is about to go camping and is worried about the various animals that inhabit the
forest. He wants you to write a C++ program that will help him figure out his survivability chances in
various scary scenarios so that he will be prepared if the unthinkable happens. To survive a given
scenario, he needs to make it to an escape position before he is attacked by a vicious squirrel. Design
the program so the woods are represented by rectangular grid composed of the following characters:
S a vicious squirrel
@ a tree (only squirrels can move onto these squares)
# obstruction that is impassable by all
. empty space (anybody can move here)
P your paranoid friend
E Escape position
The program will go as follows:
Your friend and the squirrel will alternate taking turns moving, with your friend moving first.
Your friends moves:
o Your friend can move in any direction (horizontally, vertically or diagonally), but he cannot
go back to a square he has been to before.
o Your friend escapes when he gets to an escape space on any turn.
The squirrels moves:
o The squirrel can move in any direction (horizontally, vertically and diagonally) and can go
back to squares it has been to before
o The squirrel will always move towards your friend.
o If your friends move is to the same row or column as the squirrel, the squirrel will move one
square towards your friend, in that row or column.
o If your friend's move is not to the same row or column as the squirrel, the squirrel will move
diagonally toward your friend.
o Squirrels have no problem moving into an escape space.
o If the move the squirrel wants to make is into an obstacle, it will stay put and look
frustrated.
o If it is the squirrels turn to move and the squirrel is adjacent to your friend, your friend gets
attacked.
If your friend cannot move on his turn because all adjacent spaces have been visited, are obstacles
or contain the squirrel then it is considered an animal attack as the squirrel will eventually find your
friend, lying on the ground in the fetal position.

Input will be from a text file. The first line will contain a single integer n that indicates the number of
escape grids to follow. For each escape grid, the first line will contain 2 integers in the form r c, where r
is the number of rows in the grid and c is the number of columns ( 0 < r, c 5). The next r lines will
contain c characters as described above with no spaces. Each grid is guaranteed to have at least one
escape space, one version of your paranoid friend and one squirrel. For each grid, output the grid
number and "YES- He is free" if there is any chance of escape or "NO- He is lying on the

4
ground" if there isnt. Let the user input the file name from the keyboard. Output should look similar to
below.

Sample File
3
6 5
@@...
..P..
.....
@@...
S....
@...E
4 4
E..P
.@@.
..S.
E...
4 4
E..P
.##.
..S.
E...

Sample Run:
Enter the file name: escape.txt

Grid 1: YES- He is free


Grid 2: NO- He is lying on the ground
Grid 3: YES- He is free

Name the program: MadSquirrelsXX.cpp, where XX are your initials.

6. At the Texas high school championship swim meet, there are preliminary heats conducted for an
event during the day to determine the 16 fastest swimmers in the meet who will swim in the
championship finals and consolation finals at night. The fastest eight swimmers from the preliminary
heats swim in the championship finals race and the next fastest eight swimmers swim in the consolation
finals race. The remaining swimmers do not swim in the night events.
In swimming, the lanes are numbered from 1 to 8 with lane 1 being on the far left and lane 8
being on the far right. The preferred lane numbers for a swimmer are in this order: 4, 5, 3, 6, 2, 7, 1, 8.
By seeding the swimmers in this order, the fastest swimmers have the advantage of swimming in the
smooth water in the middle of the pool and the slower swimmers must fight the wake of the faster
swimmers by swimming in the outside lanes. Write a C++ program that will find the 16 fastest
swimmers from the list of preliminary swimmers and print their lane assignments for the consolation
finals and championship finals races. Input will be from a text file where the first line of input will
contain a single integer n that indicates the number of events to be swum. For each event:
The first line will contain the name of the event.

5
The second line will contain a single integer m that indicates the number of swimmers in that
event.
Each of the following m lines will contain:
o a swimmer's first and last name separated by a space but containing no other spaces,
followed by a space,
o the swimmer's time in the preliminary heat in the form: mm:ss.hh where mm is the number
of minutes, ss is the number of seconds, and hh is the number of hundredths of a second,
but
o if the swimmer's time is DQ, the swimmer was disqualified from the event and cannot swim
in the consolation finals or championship finals events.
Assume that there are at least 10 swimmers that will qualify for the night events and no two swimmers
have the exact same time. For each event output:
Seed the swimmers into their preferred lanes based on the lane order above.
Then print the event and a space followed by CONSOLATION FINALS.
On each of the next 8 lines (or less if there are not enough swimmers):
o print the lane number, in order 1-8, followed by a period and a space,
o print the last name, a comma and a space followed by the first name of the swimmer
assigned to that lane, and then another space, and finally,
o print the seed time of the swimmer as shown in the example on the next page.
o Do not assign a lane to or print the name of any disqualified swimmers.
Print a blank line.
Then print the event and a space followed by CHAMPIONSHIP FINALS.
On each of the next 8 lines (or less if there are not enough swimmers):
o print the lane number, in order 1-8, followed by a period and a space,
o print the last name, a comma and a space followed by the first name of the swimmer
assigned to that lane, and then another space, and finally,
o print the seed time of the swimmer as shown in the example on the next page.
Print a blank line. A blank line is optional after the last data set.
Let the user input the file name from the keyboard. Use any appropriate sort as needed and C++
strings. Output should look similar to below.

Sample File:
1
100 M FREESTYLE
18
JAMES SMITH 01:10.01
TOM JONES 01:20.00
HOWARD COSELL 01:19.30
DON MEREDITH 01:21.28
RON JONES 01:17.30
RICHARD LUCAS 01:09.30
RONNIE WERTH 01:11.38
JACK MOSES 01:19.94
DAN ROGERS 01:15.84
ROGER WILLIAMS 01:15.83
WILLIAM GEORGE 01:12.27
GEORGE JONES 01:13.45

6
MARK CHRISTIE 01:45.12
CHRIS LEOPARD 01:59.29
MAJOR APPLEWHITE DQ
RICK CHARLES 01:12.67
CHARLES SHERMAN 01:13.22
SHERMAN WILLIS 01:17.78

Sample Run:

Enter the file name: swim.txt

100 M FREESTYLE CONSOLATION FINALS


1 MEREDITH, DON 01:21.28
2 MOSES, JACK 01:19.94
3 WILLIS, SHERMAN 01:17.78
4 ROGERS, DAN 01:15.84
5 JONES, RON 01:17.30
6 COSELL, HOWARD 01:19.30
7 JONES, TOM 01:20.00
8 CHRISTIE, MARK 01:45.12

100 M FREESTYLE CHAMPIONSHIP FINALS


1 JONES, GEORGE 01:13.45
2 CHARLES, RICK 01:12.67
3 WERTH, RONNIE 01:11.38
4 LUCAS, RICHARD 01:09.30
5 SMITH, JAMES 01:10.01
6 GEORGE, WILLIAM 01:12.27
7 SHERMAN, CHARLES 01:13.22
8 WILLIAMS, ROGER 01:15.83

Name the program: SwimMeetXX.cpp, where XX are your initials.