CLASSWORK--COMPUTER PROGRAMMING 1--C++
---------------------------------------------------------------------------
Classworks that require you to modify or write a C++ source file must be
designed so that they compile and run successfully via the repl.it site.
The first five lines of every program that you write must be labelled with
your name, today's date, the name of this course, the period of this course,
and the name of your program (following the guidelines specified below).
All source code must be properly and completely commented and formatted as
specified by the instructor. Unless indicated otherwise, no global
variables should be used in any of your programs. Each starting and ending
brace { } should (almost always) be put on its own line. The lines between
each set of braces should be properly indented. All functions, variables,
and constants should be named in accordance with the procedures outlined
by the instructor. One blank line should be placed between each function.
When asking the user for input, get the input on the SAME line as the
request for input.
Every program should have the procedure 'main' as the FIRST function in
your source file. This means that when you write programs that use other
functions, you will need to include function prototypes in your programs.
For the most part, unless told otherwise by the instructor, 'main' should
be used only to call other functions. Any work performed in 'main' should
be extremely minimal. If you are ever unsure as to whether or not you
should create a separate function or perform some work in 'main', it is
always better to err on the side of creating a separate function.
Unless a problem states otherwise, programs that you create do NOT need
to include error trapping for user input. In other words, it is okay
if a program behaves inappropriately due to improper data entered by a
user. However, all other aspects of your programs should perform
flawlessly. Program output should be grammatically and syntactically
correct, neat, presentable, and complete.
---------------------------------------------------------------------------
Each program that you write is to be submitted to me both electronically
via the Canvas learning management system. You will upload/submit a copy
of the source (.cpp) file of your C++ program.
All of your C++ source files are to be named using the following format:
Your username + a hyphen + "c" (for classwork)
+ the classwork # (using three digits) + ".cpp"
For example, if your name is John Doe and this is classwork #17, the
program should be named "john.doe-c017.cpp" (without the quotes). Be sure
to always use three digits for the classwork number and lowercase letters.
---------------------------------------------------------------------------
DUE Thursday, 9-17-2020
01) Write a program that asks the user to enter two integers. Ask for
each number separately, and be sure to declare the two variables
as type 'int' within your program. Then compute and display the
sum, difference, and product of those integers, each on a separate
line as part of a sentence describing what is being displayed.
(10 points)
DUE Sunday, 9-20-2020
02) Write a program that asks the user to enter his/her full name. The
program should then display the name back to the user, along with
the number of characters in the name. The user should be allowed
to enter spaces which, along with any other non-letter characters,
should be counted when computing the length of the name. Be sure
to put quotation marks around the name when displaying it back to
the user.
(10 points)
DUE Thursday, 9-24-2020
03) Write a program that has the user enter two integers (you MUST
declare the two variables as type INT within your program). Then,
if the second integer is NOT zero, the computer should divide the
first integer by the second integer and display the quotient. If
the second integer IS zero, the program should keep asking the
user to re-enter the second integer until it is not zero. Remember
that the quotient of two integers can be a non-integer.
(15 points)
DUE Sunday, 9-27-2020
04) Write a program that asks the user to enter a word (or phrase, so
be sure to allow spaces). The program should then display the
word using all uppercase letters. After that the word should be
displayed with only lowercase letters. Finally, the word should
be shown to the user with the order of the characters reversed
(and with the case unchanged from how the phrase was originally
entered). You MUST use 'for' loops to produce the required
program output.
EXTRA CREDIT OPTION #1: Have the user enter a phrase consisting
of at least three words (separated by one space each, with no
leading or trailing spaces). Then have your program display the
phrase with the order of the words reversed. For example, if the
user's phrase is "Cats are cool!", then your program would display
"cool! are Cats" back to the user.
EXTRA CREDIT OPTION #2: Have the user enter a phrase consisting
of at least three words (separated by one space each, with no
leading or trailing spaces). Then have your program display the
phrase with the words in the phrase shown in random order. For
example, if the user's phrase is "cat and fish", then your program
would display either "cat and fish", "cat fish and", "and cat fish",
"and fish cat", "fish and cat", or "fish cat and" back to the user.
(15 points)
DUE Sunday, 10-4-2020
05) Write a program that has the user enter five real numbers. Then
display the average (mean) of the five numbers. You must create
a separate function to get the five numbers from the user, and
then create another function to compute and display the mean.
The procedure 'main' should contain no more than four lines of
code (unless you are completing some or all of the extra credit
options below, in which case 'main' may contain up to seven
lines). For this specific assignment, it is okay to use global
variables in your program.
EXTRA CREDIT OPTION #1: In addition to displaying the average
of the five numbers, also display their range. The range is
defined as the difference between the largest number and the
smallest number. Remember that the user is allowed to enter
the five real numbers in any order.
EXTRA CREDIT OPTION #2 In addition to displaying the average of
the five numbers, also display the median of the five numbers.
The median of a list of an odd number of numbers is the middle
number when the numbers are arranged in numerical order.
EXTRA CREDIT OPTION #3 In addition to displaying the average of
the five numbers, also display the mode (or modes) of the five
numbers. The mode is the number that occurs most frequently
in the list. There can be multiple modes if there is more than
one number that occurs the most often in the list. Note that,
in a list, if each number appears only once, then the list does
not have any modes.
(20 points)
DUE Tuesday, 10-13-2020
06) Write a program that asks the user to enter an integer. Then tell
the user whether the given integer is odd or even, whether the
integer is positive, negative, or zero, and whether the integer
is prime or composite. How, and in what order you display that
information to the user, is up to you. A prime number is an
integer that is divisible only by 1 and itself. Note that, by
most definitions, numbers less than two are neither prime nor
composite.
For this assignment, in addition to 'main', you must create a
separate function to get and return (via a 'return' statement) the
number from the user, and then create three additional functions
to perform the three tasks listed above. The procedure 'main'
should contain no more than six lines of code. And, you may NOT
use any global variables in your program.
EXTRA CREDIT OPTION #1: Instead of having the user re-run your
program to enter more integers, have your program automatically
ask the user to enter a new number after displaying the required
information about their number. Your program should then keep
asking the user to enter new integers, displaying the required
information after each entry. Your program should finally quit
when the user presses the ENTER key without entering anything.
With this (and the next) extra credit option, it is okay to have
more than six lines of code in 'main'.
EXTRA CREDIT OPTION #2: Instead of asking the user to enter just
one number, have the user enter two integers. Then display the
required information for both of the numbers, followed by the
least common multiple of the two numbers. The LCM of two numbers
is the smallest number into which the two numbers divide with no
remainder. This extra credit option may be implemented before or
after the first extra credit option.
(20 points)
DUE Tuesday, 10-20-2020
07) Write a program that has the user enter a sequence of integers.
After EACH number is entered, the program should compute and
report the mean of the numbers entered so far. (Remember that
the average of integers can be a floating point number.) There
is no limit to the number of integers that may be entered, and
you may NOT ask the user how many numbers will be entered. The
program should quit when the user presses the ENTER key without
entering anything. (Make sure your program does not display a
"bogus" average as it is ending.)
For this assignment, while it is okay to put your main loop in
the procedure 'main', from within the loop you must call a
separate function whose sole purpose is to get and return (via a
'return' statement) all user input. Also called from within the
loop must be an additional function whose purpose is to compute
and return (also via a 'return' statement) the current average
of the numbers entered so far. You will most likely also need
to pass arguments to one or both of these functions.
The current average should always be displayed from within your
main loop (not from within any of the functions called from the
loop). Both the variable that stores the sum of the integers
entered so far, as well as the variable that stores the number
of integers entered so far, should be declared as type 'int'
(not 'double' or 'float'). You may NOT use any global variables
in this program.
EXTRA CREDIT OPTION #1: After the user presses the ENTER key by
itself, before the program ends, display the range of the numbers
entered by the user. The range is defined as the difference
between the largest number and the smallest number. Remember
that the user is allowed to enter numbers in any order.
EXTRA CREDIT OPTION #2: After the user presses the ENTER key by
itself, before the program ends, display the median of the numbers
entered by the user. The medium is the middle number of a list
of numbers arranged in numerical order. If there is an even
number of integers, then the median is the average of the two
middle numbers.
EXTRA CREDIT OPTION #3 After the user presses the ENTER key by
itself, before the program ends, display the mode (or modes) of
the numbers entered by the user. There can be multiple modes
if there is more than one number that occurs the most often in
the list. Note that, in a list, if each number appears only
once, then the list does not have any modes.
(15 points)
DUE Thursday, 10-29-2020
08) Write a program that simulates a slot machine. Every time the
user "pulls the lever" (the main game loop is executed), display
three random words or symbols from a set of at least seven items
that represent what might be seen with a real slot machine.
After being shown the results from each pull of the lever, the
user should be given the choice to either quit the program or
pull the lever again.
After each pull of the lever, in addition to showing the three
random items, the user should also be shown how much money is
left in his/her bank. When the program first starts, the user
should be gifted a certain amount of money of your choosing.
Then, after each lever pull, the user will either win more money
or lose some money, depending on what items have been randomly
selected and shown. While you may choose the amounts that the
user wins and loses, getting "three of a kind" should earn the
user the most amount of money. Getting "two of a kind" should
earn the user less money, and having three different items shown
should cause the user to lose money. You are free to create
additional situations where the user can win or lose money.
Make sure the user cannot play the game if he/she has run out of
money. Also make sure the results of each lever pull, and the
questions posed to the user, are displayed in a neat, organized
manner. The visual presentation of your program (what the user
sees) will play a significant role in determining your score for
this assignment.
Your program should NOT use any global variables. It is okay
for the procedure 'main' to contain an overall game loop, but
'main' should be used mostly just to call other functions in
your program. In your other functions, it is up to you whether
you return values with a 'return' statement or pass all of the
variables to/from your functions as arguments and parameters.
EXTRA CREDIT OPTION #1 Every time the user chooses to play the
game again (pull the lever), before showing a new set of three
random words or symbols, clear the console and diplay the
information in such a way that, to the user, it looks like the
only text being changed on the screen is the text with the three
random words or symbols, the text describing the win or loss,
and the amount of money left in the user's bank.
EXTRA CREDIT OPTION #2 Use different colors when displaying
the text with the random words or symbols displayed each time
the user pulls the lever. Each random word or symbol should be
displayed in a non-white color. While it is okay if some of the
words or symbols have the same colors, be sure to use at least
three different non-white colors for the random data.
EXTRA CREDIT OPTION #3 Instead of displaying the random words
or symbols by themselves or on lines of plain text, have a "box"
drawn around the random data. This box can be made up of ASCII
characters, or it can consist of spaces and highlight/background
colors. Regardless of how you create the box, make sure it is
always the same size every time the user plays the game, no
matter what words or symbols are randomly chosen and displayed.
(25 points)
DUE Thursday, 11-5-2020
09) Write a program that has the user enter two words. Then compare
the words, character-by-character, and report the number of
positions that have identical letters. For instance, if the
user enters "parrots" and "balloons", your program should report
2, since the second and fifth letters are matching. If the user
enters "DOGS" and "soggy", your program should also report 2,
since the second and third letters are identical (ignoring case).
In addition to 'main', you must have a separate function for
getting the words, another function for comparing the letters in
the words, and yet another function for reporting the results to
the user. You may NOT use any global variables in your program,
and you also may NOT use any 'return' statements in your program.
To receive values back from the functions that get the words and
compare the letters, you must pass data into those two functions
by reference, instead of by value. You may assume that the user
will enter strings consisting of only letters.
EXTRA CREDIT OPTION #1 In addition to displaying the number
of matching positions, make a new function which displays a new
"combo-word", which is made up of alternating letters from the
two entered words. Always start with the first letter of the
longest entered word, unless the words are of equal length, in
which case you should start with the first letter of the first
entered word. When creating the combo-word, when the shorter
entered word runs out of letters, finish the combo-word with the
remaining letters in the longer entered word. For example, if
the user enters "desk" and "computer" (in any order), your new
function should display "cdoemspkuter". Or, if the two entered
words are "fooD" and "CaTs" (in that order), then the combo-word
should be "fCoaoTDs".
EXTRA CREDIT OPTION #2 In addition to displaying the number of
letters that have matching positions in the two words, create a
new function which displays the number of letters that the two
words have in common, regardless of their positions in the words.
For example, if the user enters "table" and "computer", your new
function should display a 2, since both of the words share a "t"
and an "e". If the entered words are "ChipmunkS" and "MonKEys",
then your function should display a 4, since, ignoring case, the
two words share the letters "m", "n", "k", and "s". For this new
function, you may assume that each entered word does not have any
duplicate letters.
(15 points)
DUE Sunday, 11-22-2020
10) Write a program that uses a loop to have the user enter a list of
up to ten words, one per line. The user should signal the end of the
list by pressing the ENTER key by itself on an empty line. Your
program must require that the user enter at least one word (by
continuously asking the user to enter words until at least one entry
is made), and it should not allow the user to enter more than ten
words (after the tenth entry it should stop asking for new input).
Your program must eliminate leading and trailing spaces from all user
input.
In addition to the above requirements, your program must use a loop
to prevent the user from entering duplicate words. If the user
enters a word that has already been entered, discard that entry,
tell the user that duplicate entries are not allowed, and then have
the user enter a new word. Your program should not modify the case
of the entered words, but it should ignore case when checking for
duplicate entries.
Finally, after the user has entered up to ten unique words, your
program should use a loop to display the word list back to the user,
one entry per line. The words should be displayed back to the user
in the same order in which they were entered. When displaying the
list, if it contains fewer than ten words, make sure no blank (empty)
lines or NULL entries are displayed.
For this program you must use a built-in C++ array of strings to
store the words. You are allowed to use only one array, and you may
define it globally. However, you may not use any other type of List
or Collection anywhere in your program. Also, the maximum number of
entries allowed (10) should be stored in your program as a globally-
defined integer constant (named using all capital letters), and with
the exception of the line where you define the constant, the number
10 (or 9 or 11) should not appear anywhere in your program.
Other than the global constant and array of strings, you may not use
any global variables for this assignment. In addition to 'main', you
should have a separate function that gets the words from the user,
another function that checks for duplicate entries, and yet another
function that displays the word list back to the user.
EXTRA CREDIT OPTION #1
When displaying the words back to the user, display the words
arranged in order by length (number of characters), with the
shortest word shown first. If the list contains multiple words
with the same number of characters, then that group of words
should be displayed in ascending alphabetical order (within the
larger list of words, which should still be sorted by length).
This extra credit option can be completed along with, or instead
of, the other extra credit option. You may use additional arrays
for this extra credit option.
EXTRA CREDIT OPTION #2
This option can be completed before or after, or instead of, the
other extra credit option. When displaying the words back to the
user, number each word (starting with "1"). Then, after the list
has been displayed, allow the user to change the order of the words
in the list by swapping the positions of any two of the words. The
user should be able to type the numbers of two words in the list to
indicate which words should be swapped, after which the list should
be re-displayed in its new order (with all of the words newly
renumbered). The user should be allowed to continue to change the
order of words indefinitely by swapping sets of two words, with the
list being re-numbered and re-displayed after each swap. There
should also be a way for the user to quit the program at any time.
You may use additional arrays for this extra credit option.
(20 points)
DUE Sunday, 12-13-2020
11) Write a "number guessing" game where the computer randomly generates
a positive integer, and then the user is allowed to try to guess the
number. After each guess, the computer should tell the user whether
the guess was too high, too low, or just right (the correct number).
At the start of the program, the user should be prompted to choose the
lower and upper boundaries (the range from which the randomly-generated
number will be chosen), with a maximum upper boundary of one million.
The user should also specify how many guesses are allowed (up to 20)
before the computer reveals the chosen number (if not correctly guessed
by the user). Finally, after each game, the user should be given a
chance to either quit the program or play another game using the same
user-chosen values, but with a new computer-generated random number.
All user-entered numbers should be positive integers. The computer
should make sure that the upper boundary is larger than the lower
boundary, and that no values exceed the maximums listed above. Invalid
and out-of-bounds user guesses should not cause the remaining number of
allowed guesses to decrease. Your program should include thorough,
user-friendly error trapping routines wherever data are entered by the
user. There should be no way for a user to crash your program or to
cause it to act in an incorrect manner (e.g., by entering words or
decimals or negative numbers or out-of-bounds values).
When getting the initial input from the user, first ask for the lower
boundary, then ask for the upper boundary, and then ask for the number
of allowed guesses. Also, make sure your program stores all of the
data values (low bound, high bound, guesses allowed, user-made guesses,
computer-chosen number, number of guesses used/remaining, etc.) in
integer variables (not as strings). Be sure to use variable names that
are descriptive enough so that their purpose can be determined solely
from their names, with no ambiguity.
Make sure your program uses lots of separate functions, as appropriate,
and do NOT use any global variables. You may use 'return' statements
and/or pass data to functions by value and/or reference, as you see fit.
Also, make sure you place a multi-line comment before every function in
your program explaining the purpose of the function. This includes an
even larger multi-line comment above 'main()' that explains the overall
purpose of the program and how it works.
(35 points)