Assignment #3 -- Practice with loops and functions

Due: Thurs, Oct 12

Objective

This assignment will involve practice with loops, as well as writing and calling functions (functions are in exercise 3 only)

Task

Write the following programs, each in a separate file. Filenames should be: (Note that the filenames are all lowercase)

Exercise 1

Filename: pennies.cpp

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:

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

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

Please enter the number of days worked: 18

Day                     Amount Earned
-------------------------------------------
1                       $ 0.01
2                       $ 0.02
3                       $ 0.04
4                       $ 0.08
5                       $ 0.16
6                       $ 0.32
7                       $ 0.64
8                       $ 1.28
9                       $ 2.56
10                      $ 5.12
11                      $ 10.24
12                      $ 20.48
13                      $ 40.96
14                      $ 81.92
15                      $ 163.84
16                      $ 327.68
17                      $ 655.36
18                      $ 1310.72

Total earnings: $ 2621.43

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:

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1

Input integer (0 to stop): 12
Input integer (0 to stop): 4
Input integer (0 to stop): -1
Input integer (0 to stop): -5
Input integer (0 to stop): 18
Input integer (0 to stop): 0

Total: 5
Sum: 28
Average: 5.60
Divisible by 9: 1
Max: 18
Min: -5

Sample Run 2

Input integer (0 to stop): 4
Input integer (0 to stop): 8
Input integer (0 to stop): 24
Input integer (0 to stop): 95
Input integer (0 to stop): 81
Input integer (0 to stop): -1
Input integer (0 to stop): 43
Input integer (0 to stop): 27
Input integer (0 to stop): 44
Input integer (0 to stop): -18
Input integer (0 to stop): 13
Input integer (0 to stop): 0

Total: 11
Sum: 320
Average: 29.09
Divisible by 9: 3
Max: 95
Min: -18

Exercise 3 - This one involves the use of functions

Filename: krusty.cpp
  1. Write a function called OrderTotal that takes in four integer parameters representing: in a customer's order. The function should return the total order cost based on the price of each item presented in the menu. NO user input or output to the screen should occur in this function. The function should simply calculate the order total based on the quantity of each item and return this total as a double.
     
  2. Write a function called menu that takes in no parameters and returns no value. This function is only responsible for printing the following menu to the screen:
    ----------- Menu -----------
    1. Krabby Patty ...... $3.50
    2. Barnicle Fries .... $1.50
    3. Kelp Shake ........ $1.00
    4. Krusty Krab Pizza . $5.00
    
  3. To test these functions, and help facilitate your program, write a main() routine (in the same file) that enters a loop and starts by presenting the customer with the menu (by calling the appropriate function). Allow the customer to specify what they want off the menu. The customer should enter 1, 2, 3, or 4 to order an item, or 0 to exit. If they enter any other value here (besides 0-->4) print an error message. If they enter a valid menu item, ask how many of that item they'd like to order. FORCE the user to enter a correct valid value here (0 or more is valid, negative amounts of items are not). See the sample runs below for more details.

    When the customer is done ordering, print the counts of each item they ordered as well as their order total (by calling the appropriate function to help you calculate the order cost).

Important Note in this requirement

As this exercise involves the use of functions, these tasks must be performed with the functions specified (not just with a single main() routine).
This means your main() will end up calling these functions in the appropriate places.

Sample Runs

(user input is underlined, to distinguish it from output)

Sample Run 1:

Welcome to the Krusty Krab!

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 2
How many: 1

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 4
How many: 1

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 0

Your order:
1 barnacle fries.
1 krusty krab pizzas.

Your total is $6.50
Enjoy the food!

Sample Run 2:

Welcome to the Krusty Krab!

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 8
That isn't on the menu! Try again.

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 2
How many: -5
You can't order a negative amount of food!
Try Again: -7
You can't order a negative amount of food!
Try Again: -9
You can't order a negative amount of food!
Try Again: 1

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 0

Your order:
1 barnacle fries.

Your total is $1.50
Enjoy the food!

Sample Run 3:

Welcome to the Krusty Krab!

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 0
Guess you aren't hungry.

Sample Run 4:

Welcome to the Krusty Krab!

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 1
How many: 3

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 1
How many: 2

----------- Menu -----------
1. Krabby Patty ...... $3.50
2. Barnicle Fries .... $1.50
3. Kelp Shake ........ $1.00
4. Krusty Krab Pizza . $5.00

What would you like? (Enter 0 to finish order!): 0

Your order:
5 krabby patties.

Your total is $17.50
Enjoy the food!

Requirements for all programs


Submitting:

Submit only your source code files with the submission command below. As there are three of them, this will require doing the command three times, once for each of your files.

Format:

  ~myers/csub/submit3 yourfilenamehere
Example:
  ~myers/csub/submit3 pennies.cpp
Do this for each of your files.