0% found this document useful (0 votes)
152 views

Program Set 4

3

Uploaded by

pablo hernandez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

Program Set 4

3

Uploaded by

pablo hernandez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COSC 1437 (DL) Spring 2017

Program Set #4
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. Implement Programming Exercise #12-Chapter 17 (Gaddis), p.1062. Use the driver program
provided by your instructor. Output should be user friendly.

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

2. Write a C++ program that uses recursion to determine a valid C++ identifier. The recursive definition
is as follows:

A C++-Identifier is a: Letter
or a: Letter followed by a C++-Identifier-Substring
A C++-Identifier-Substring is a: Letter
or a: Digit
or a: Letter followed by a C++-Identifier-Substring
or a: Digit followed by a C++-Identifier-Substring

Input the identifier from the keyboard and output if it is or is not a valid identifier. Must use a recursive
function for full credit. Use the C++ string class. Output should look similar to below.

Sample Runs (2):

Enter the C++ identifier: A1Anda2


A1Anda2 is a valid C++ identifier

Enter the C++ identifier: 5cats


5cats is not a valid C++ identifier

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

3. Given a dollar amount between $0.01 and $9.99 write a C++ program to compute the fewest
number of standard American coins needed to produce that sum. The standard coins are:

dollar ($1.00)
half-dollar ($0.50)
quarter ($0.25)
dime ($0.10)

1
nickel ($0.05)
penny ($0.01)

Input the amount from the keyboard (double). Output the number of coins in descending order of value,
using plurals when a specific coin is represented more than once. Include a recursive function to
compute the change. Must use a recursive function for full credit. Error check the input. Output should
look similar to below:

Sample Run:

Enter the amount (double): 1.47

Change returned:
1 dollar
1 quarter
2 dimes
2 pennies

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

4. Given an integer from 1 to 999,999,999 you are asked to write a recursive C++ program to state that
value using English words. For example:

1431 ONE THOUSAND, FOUR HUNDRED THIRTY-ONE


11011011 ELEVEN MILLION, ELEVEN THOUSAND, ELEVEN
900009 NINE HUNDRED THOUSAND, NINE
123456789 ONE HUNDRED TWENTY-THREE MILLION, FOUR HUNDRED
FIFTY-SIX THOUSAND, SEVEN HUNDRED EIGHTY-NINE

Separate words using commas in the same way that you would when writing out in digit form. Thus
900,009 becomes "NINE HUNDRED THOUSAND, NINE" and uses one comma. Note how each value
contains only CAPITAL letters, spaces, hyphens and commas. Hyphens only occur between values such
as TWENTY-ONE and NINETY-EIGHT, that is, for values from 2199 with the exception of values evenly
divisible by ten. Recall that the numbers from 11-19 in English are represented by ELEVEN, TWELVE,
THIRTEEN, FOURTEEN, FIFTEEN, SIXTEEN, SEVENTEEN, EIGHTEEN and NINETEEN. Note that there is
a single space after each comma. Input from the keyboard consists of a single line of no more than 9
digits. The lead character will never be a zero ("0"). The output consists of a single line containing only
CAPITAL letters, spaces, hyphens and commas. Make sure that there are no leading or trailing spaces in
the output. Finally, ask the user if he/she wishes to run the program again. Must use recursive
function(s) for full credit. Output should look similar to below.

2
Sample Run:

Enter a number: 923000000

NINE HUNDRED TWENTY-THREE MILLION

Run Again (Y/N): Y

Enter a number: 119

ONE HUNDRED NINETEEN

Run Again (Y/N): y

Enter a number: 11013016

ELEVEN MILLION, THIRTEEN THOUSAND, SIXTEEN

Run Again (Y/N): N

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

5. A geography class is currently studying Europe, and the next test covers the capital cities. The
test will be fill-in-the-blank questions. Each question will be in one of the following two formats:

Format 1: The capital city of <country> is_____.


Format 2: The capital city of _____ is <city>.

Where placeholders <country> and <city> are provided, and you must fill in the blanks. Here is a
copy of your study sheet:

Country Capital Country Capital


Albania Tirana Liechtenstein Vaduz
Andorra Andorra la Vella Lithuania Vilnius
Armenia Yerevan Luxembourg Luxembourg
Austria Vienna Macedonia Skopje
Azerbaijan Baku Malta Valletta
Belarus Minsk Moldova Chisinau
Belgium Brussels Monaco Monaco
Bosnia Herzegovina Sarajevo Montenegro Podgorica
Bulgaria Sofia Netherlands Amsterdam
Croatia Zagreb Norway Oslo
Cyprus Nicosia Poland Warsaw
Czech Republic Prague Portugal Lisbon
Denmark Copenhagen Romania Bucharest

3
Estonia Tallinn Russia Moscow
Finland Helsinki San Marino San Marino
France Paris Serbia Belgrade
Georgia Tbilisi Slovakia Bratislava
Germany Berlin Slovenia Ljubljana
Greece Athens Spain Madrid
Hungary Budapest Sweden Stockholm
Iceland Reykjavik Switzerland Bern
Ireland Dublin Turkey Ankara
Italy Rome Ukraine Kiev
Kazakhstan Astana United Kingdom London
Kosovo Pristina Vatican City Vatican City
Latvia Riga

You have little interest in geography and are an accomplished programmer. To skirt the opportunity of
genuine learning, you decide to take advantage of your existing abilities and write a C++ program to fill
in the blanks. Input will be from a data file each in the format given in the description above. Blanks will
consist of 5 consecutive underscore characters ('_'). Output will exactly mirror the input except that
the blanks will be replaced with the correct country or capital names from the study sheet. All of the
countries/capitals may or may not be used. Use the C++ string class and any appropriate STL classes. Let
the user input the file name from the keyboard. Output should look similar to below.

Sample File:

The capital city of Italy is _____.


The capital city of _____ is Helsinki.
The capital city of Bosnia Herzegovina is _____.

Sample Run:

Enter file name: capitals.txt

The capital city of Italy is Rome.


The capital city of Finland is Helsinki.
The capital city of Bosnia Herzegovina is Sarajevo.

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

4
6. A popular word game involves finding words from a grid of randomly generated letters. Words must
be at least three letters long and formed from adjoining letters. Letters may not be reused and it is valid
to move across diagonals. As an example, consider the 4x4 grid of letters below.

A B C D
E F G H
I J K L
M N O P

The word "FAB" is valid (letters in the upper left corner) and the word "KNIFE" is valid. The word
"BABE" is not valid because the "B" may not be reused. The word "MINE" is not valid because the "E" is
not adjacent to the "N".
Write a C++ program that uses a 4x4 two-dimensional array to represent the game board. The
program should randomly select letters for the board. Either place a "U" next to a "Q" or to treat "QU"
as a single letter. The program should read the words from a data file and then use an algorithm to
determine if the word may be formed from the letters on the game board. The program should output
the total number of words found and all valid words (10 per line) from the file that are on the game
board. Let the user input the file name from the keyboard. Use any appropriate STL classes and the C++
string class. Output should look similar to below.

Sample Run:

Generated Board:

H A H I
V W O S
E E S E
T I P W

Enter file name: words.txt

Number of words found: 80

awe awes epee eve eves ewe ewes hah have haves
haw haws his hiss hoe hoes hos hose hoses how
hows ohs owe owes pee peso pesos pet pew pews
pie pies pis pit psi see set sew sews shah
shave shaves shoe shoes show shows sieve sip sit site
sos sow sows spew spit spite ssh sweep sweet tee
tees tie ties tip tips tis veep veeps vet wave
waves wee weep weeps wet who whoa whose woe woes

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

You might also like