Description
In this lab, you will write an application that helps the user budget their coffee consumption for the month. By the end of this lab, you should be able to:
• Accept and validate user input from the keyboard.
• Use the user input to perform and output useful calculations.
• Make decisions based on different conditions.
• Loop (i.e., repeat code) a certain number of times based on different conditions.
• Explicitly format the output of numbers.
1 Problem Statement
Many restaurants have developed a loyalty program to keep customers coming back. For
example, McCafe offers to stamp your loyalty card each time you purchase a coffee. After
seven stamps, you receive a medium coffee for free. This is not new – coupons offered
to customers for purchasing items has been around for a very long time. A little research
on the internet shows that this originated with Coca-Cola in 1887. Cool! This week, you
will write a C program to help Mario budget his coffee consumption for the month. The C
program should accept three inputs:
1. The price of a coffee.
2. How many coupons (i.e., stamps) are needed to redeem a free coffee.
3. How much money the user has available for coffee this month.
You must ensure that each input is valid. If all the inputs are valid, you will output to the
user:
• The number of coffees they can purchase this month.
• The number of additional coffees they can redeem via coupons.
• The amount of money and coupons the user will have left over at the end of the
month.
• The average price the user spent on coffee.
2 Valid Input
You must ensure that each input is valid before proceeding with the rest of the program.
All fractional inputs should be stored with double-precision. If the input is invalid, you
must immediately print “Invalid Input.“ to the screen and exit the program successfully.
These are the constraints on the input:
• The price of a coffee must be at least $0.01.
• The number of coupons needed to redeem a free coffee must be more than one.
• The number of coupons needed to redeem a free coffee must not be fractional.
• The amount of money the user has available for coffee this month must not be negative.
You can make the following assumptions on the input:
• The price of coffee and amount of money entered by the user will be given to, at
most, two decimal places.
• The number of coupons needed to redeem a free coffee will not be fractional.
3 Redeeming Coupons
Note that at McCafe, when you redeem your stamps for a free coffee, you get another
stamp for the coffee you received for free!
For example, if each coffee costs $2 and Mario had $40 this month for coffee, he can purchase 20 coffees. He would also receive 20 stamps. For every 7 stamps, Mario can redeem
one free coffee, so Mario can redeem two coffees with his 20 stamps. However, those two
free coffees provide him with two more stamps. Thus, Mario can redeem one more free
coffee this month, leaving him with two stamps left over.
4 Notes on Output
Every sentence must end with a new line. Below are some sample runs of the application.
4.1 Valid Input and Free Coffee
A medium coffee at McCafe costs $1.88 and 7 stamps are required to obtain a free coffee.
Below is an example (with input shown in bold for your convenience) of Mario budgeting
$30 for coffee this month.
How much does a coffee cost?
1.88
How many coupons does it take to get a free coffee?
7
How much money do you have to spend on coffee this month?
30
This month, you can purchase 15 coffees at $1.88 each.
You will be able to redeem an additional 2 coffee(s) from coupons.
At the end of the month, you will have $1.80 and 3 coupon(s) remaining.
On average, you spent $1.66 on each cup of coffee this month.
2
4.2 Valid Input and No Free Coffee
A medium coffee at McCafe costs $1.88 and 7 stamps are required to obtain a free coffee.
Unfortunately, Mario doesn’t have enough money to redeem any free coffees. This makes
Mario a sad panda. Note the difference in output (difference shown in bold for your
convenience).
How much does a coffee cost?
1.88
How many coupons does it take to get a free coffee?
7
How much money do you have to spend on coffee this month?
10
This month, you can purchase 5 coffees at $1.88 each.
You will not be able to redeem any additional coffees from coupons this
month.
At the end of the month, you will have $0.60 and 5 coupon(s) remaining.
On average, you spent $1.88 on each cup of coffee this month.
4.3 Invalid Input
In this example, the user has provided an invalid number for one of the three inputs.
How much does a coffee cost?
1.88
How many coupons does it take to get a free coffee?
-3
Invalid Input.
5 Submission Instructions
In a file called Lab3.c, write your solution to the problem. There are ten (10) marks
available in this lab.
5.1 TA Grading (4 marks)
Your solution will be marked by a Teaching Assistant during your scheduled lab period for
programming style and your understanding of the code. You can discuss programming
style with your TA and on Piazza. Here are some quick guidelines:
• Good choices for variable names that indicate their purpose.
• A consistent naming convention. Use camelCase for normal variable names.
• Comments that explain code that is difficult to understand.
• Proper indentation.
• Appropriate white space between lines for better readability.
3
5.2 Automated Grading (6 marks)
Your solution must be submitted electronically by the end of your scheduled lab period.
Submission of the lab requires you to use a terminal. In the terminal, you must:
1. Go to the directory that contains Lab3.c (i.e., use the cd command in the terminal).
2. Type in the following command: /share/copy/aps105s/lab3/submit
This command will run an exercise program that will check to make sure everything looks
okay. If it finds a problem, it will ask you if you are sure that you want to submit.
Note that you may submit your work as many times as you want prior to the deadline;
only the most recent submission is marked. You can also run the exerciser on your own
with the following command:
/share/copy/aps105s/lab3/exercise
Finally, you can also check to see if what you think you have submitted is actually there,
for peace of mind, using the following command:
/share/copy/aps105s/lab3/viewsubmitted
You must submit your lab by the end of your assigned lab period. Late submissions
will not be accepted, and you will receive a grade of zero.
6 Obtaining Your Automated Grade
After all lab sections have finished, a short time later, you will be able to run the automarker to determine the auto-marked fraction of your grade on the code you have submitted. To do so run the following command:
/share/copy/aps105s/lab3/marker
This command will compile and run your code and test it with all the test cases used to
determine the auto-mark grade. You will be able to see those test casesâA˘ Z output and ´
what went right or wrong.
4