Sample Problems
Problem #1
Cash Register
Your task is to write a program that will simulate a cash register. The program will prompt the user to either add an item to the cost of their order or total the order. When entering an item to be added to the order, the user should be prompted to enter the price and what type of item it is (i.e. taxable or non-taxable), so that the proper 6% sales tax can be charged on taxable items. The program will then print out a receipt which displays the total amount of the purchases, how much for sales tax, and the grand total. Your program should prompt the user for the current action and perform the desired computation.
Sample input: User input is in bold
(a)dd an item, (t)otal the order: a
How much is item 1? 3.00
Taxable? N
(a)dd an item, (t)otal the order: a
How much is item 2? 300.00
Taxable? Y
(a)dd an item, (t)otal the order: t
Total price of the 2 items is $ 303.00
Sales tax is $ 18.00
TOTAL WITH TAX IS $ 321.00
NOTE: You must count the number of items and have the numbers correctly formatted to be given credit for this problem.
Problem #2
Consecutive Integer Summation Problem
Write a program that accepts a positive integer number N, such that 0 < N < 32,000, and returns a series of consecutive positive integers that sum to N. Your program should also count how many series of consecutive positive numbers it found that sum to N. For example if the user entered 25 then your program would display:
The consecutive positive numbers from 3 to 7 sums to 25.
The consecutive positive numbers from 12 to 13 sums to 25.
We found 2 such series of numbers that sum to 25.
Notice that 12 + 13 = 25 and 3 + 4 + 5 + 6 + 7 = 25. You must have at least two numbers in your sum, and the numbers must be consecutive. So 10 + 15 does not count.
Problem #5
Four-digit sequence to termination
Given any four digits we can play the following game. Step 1 Form the largest number possible from the sequence, call it the max. Step 2 Form the smallest number possible from the sequence, call it the min. Step 3 subtract the min from the max. This will give you four new digits and the process is repeated. The process stops when we get the same resulting four-digit number two times in a row.
Example:
4321
- 1234
= 3087
now
8730
- 0378
= 8352
now
8532
- 2358
= 6174
and so forth
Write a program that accepts a four-digit integer from the user and displays the termination sequence (See the sample session below) until the same result repeats two times in a row. Count how many subtractions your program performs. The output of your program should be neat and easy to read.
Partial Sample session:
Input the four-digit number please: 4321
Round 1 4321 1234 = 3087
Round 2 8730 0378 = 8352
Round 3 8532 - 2358 = 6174
. And so forth
Problem #8
Peter the Postman
Peter the postman works in the Bloomsburg University post office. In this post office there are 400 mailboxes numbered 1 to 400. One night out of sheer boredom, he opened every mailbox. Then he shut every second mailbox (2,4,6, ... ,400). Then he went to every third mailbox (3,6,9,12...399), if it was open he shut it, if it was closed he opened it. Then he went to every fourth mailbox and did the same thing, then to every fifth mailbox and so on. He continued this odd routine each night. On each round, if a box he selected was open, he shut it, if it was shut he opened it. On the last round of the night, he only considered the 400th mailbox, if it was open, he shut it, and if it was shut he opened it.
In the morning, which mailboxes were open? Display only the open mailboxes.
Problem # 9
Line Justification Problem
A common task of early text editors was to fully justify lines of text for a book. The lines of the book are justified such that the following rules would apply.
Rule 1: There are no spaces to be placed on the left or right side of a line.Rule 2: All characters take an equal amount of space.
Rule 3: The number of spaces between any two words on a line may differ by no more than 1.
Rule 4: If the number of spaces between words on any one line is not all equal, then the following rules should be used to determine which words get the extra spaces.
4a: On even numbered lines the words on the right-hand side should get the extra spaces.
4b: On odd numbered lines the words on the left-hand side should get the extra spaces.
Rule 5. The last line of text is not justified.
Take as input an integer for the size of each line and a collection of words. Produce the words fully justified using as few lines as possible without exceeding the total number of spaces available on a line and without violating any of our rules. In our examples and in your output use the # symbol to represent a space. You must display the ruler before the output to receive credit for this problem.
Example:
How many spaces are we allowed per line? 16
What are the words? This is a test of our system. I hope you get the correct answer.
Sample output: (the first line is the ruler to help us check your answers)
1234567890123456
This##is##a#test
of##our##system.
I##hope##you#get
The######correct
answer.
Notice: That in lines 1 & 3 the words on the left hand side got the extra space. In lines 2 and 4 all spaces are equal. Line 5 is not justified because it is the last line of text.