Programming Assignment 3 Due February 20, 2015 at 11:59 PM Objectives

advertisement
Programming Assignment 3
Due February 20, 2015 at 11:59 PM
Objectives
This assignment will involve further practice with control structures, including repetition statements (loops).
Task
Write the following programs, each in a separate file. Filenames should be:
• sum.cpp
• numbers.cpp
• pennies.cpp
(Note that the filenames are all lowercase)
Exercise 1
Filename: sum.cpp
Prompt the user and let them enter two integers (we’ll call them x and y, for the purposes of this
writeup). Compute and print the sum of the ingeters in the range x through y (including the
endpoints). Output should look like the sample runs below, and print the addition details from the
smallest to the largest number.
(Note: The user input will not necessarily have the lowest number first – see Sample Run 2. You
must take this into account).
Sample run 1: (user input underlined)
Input two integers: 10 20
Sum of values from 10 through 20 is:
10 11 12 13 14 15 16 17 18 19
= 165
20
Sample run 2: (user input underlined)
Input two integers: 9 -4
Sum of values from -4 through 9 is:
-4 -3 -2 -1 0 1 2 3 4 5 6
= 35
7
8
Sample run 3: (user input underlined)
Input two integers: 7 7
1
9
Sum of values from 7 through 7 is:
7
= 7
Exercise 2
Filename: numbers.cpp
Write a program that will allow the user to enter a set of integers (as many as they want), then
prints out a summary of information, as follows:
• Prompt the user continually to enter a number, until the value 0 is entered, which will be the
signal to stop entering values. (This is sometimes known as a “sentinel” value).
• Once the sentinel value has been entered, print out the following information:
–
–
–
–
The
The
The
The
number of positive values that were input.
number of negative values that were input.
sum of the input values.
average (to two decimal places) of the input values.
• Your output must match mine exactly. See the sample runs below
Sample run 1: (user input underlined)
Input
Input
Input
Input
Input
Input
integer
integer
integer
integer
integer
integer
(0
(0
(0
(0
(0
(0
to
to
to
to
to
to
stop):
stop):
stop):
stop):
stop):
stop):
12
4
-1
-5
18
0
# of positives = 3
# of negatives = 2
Sum = 28
Average = 5.60
Sample run 2: (user input underlined)
Input
Input
Input
Input
Input
Input
Input
Input
integer
integer
integer
integer
integer
integer
integer
integer
(0
(0
(0
(0
(0
(0
(0
(0
to
to
to
to
to
to
to
to
stop):
stop):
stop):
stop):
stop):
stop):
stop):
stop):
4
8
24
94
-1
43
13
0
# of positives = 6
# of negatives = 1
Sum = 185
Average = 26.43
2
Exercise 3
Filename: pennies.cpp
This is a programming challenge that appears in your textbook.
Write a program that calculates how much a person would earn over a period of time if his or her
salary is one penny the first day, two pennies the second day, and continues to double each day.
The program should proceed as follows:
• Ask the user for the number of days worked, and allow them to enter it. You may assume
this will be an integer.
– A valid entry must be 1 or more days, so whenever the user enters an entry that is not
positive (1 or more), print an error message and make them re-enter.
• Display a table showing how much the salary was for each day.
• Display the total pay at the end of the period.
• All monetary outputs should be in dollar and cents notation (not just number of pennies),
in this format: $ D.cc , where D is the number of dollars, and cc is the cents (always two
digits). Examples:
Valid:
$ 1.34 , $ 0.05 , $ 123.45
Sample run 1: (user input underlined)
Please enter the number of days worked: -1
Invalid number of days. Try again
Please enter the number of days worked: 0
Invalid number of days. Try again
Please enter the number of days worked: 5
Day
Amount Earned
------------------------------------------1
$ 0.01
2
$ 0.02
3
$ 0.04
4
$ 0.08
5
$ 0.16
Total earnings: $ 0.31
Sample run 2: (user input underlined)
Please enter the number of days worked: 18
Day
Amount Earned
------------------------------------------1
$ 0.01
3
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
$
0.02
0.04
0.08
0.16
0.32
0.64
1.28
2.56
5.12
10.24
20.48
40.96
81.92
163.84
327.68
655.36
1310.72
Total earnings: $ 2621.43
Requirements
• No global variables, other than constants.
• All input and output must be done with streams, using the library iostream.
• You may only use the iostream library (you do not need any others for these tasks).
• When you write source code, make sure it is clear, readable, and well-documented.
Submitting
Program submissions should be done through Blackboard under the Assignments tab. Make sure
you submit all three .cpp files. Program submissions are due February 20 at 11:59 PM.
4
Download