Lab 1. Coin-changing and the Calculator
COMP Structured Programming
United International College
Introduction:
Now you are going to be a programming expert by exploring more in C programming! In this lab, you need to complete two tasks: a coin-changing program and a C calculator.
Task 1: Coin-Changing
You will write a coin-changing program for lab1. First, ask the user for the amount in
Hong Kong dollars, such as 56.30. Then find the minimum number of $10, $5, $2, $1, 50 cent, 20 cent, and 10 cent coins to give in return.
For example, your program first prompts the user for the amount in HKD by displaying the following message.
Please input the amount in Hong Kong dollars:
If the input is 56.30, the output should be
10-dollar coin: 5
5-dollar coin: 1
2-dollar coin: 0
1-dollar coin: 1
50-cent coin: 0
20-cent coin: 1
10-cent coin: 1
Task 2: A Calculator
Your second task is to write an integer arithmetic calculator.
Your calculator program should prompt the following UI (user interface) to the user.
-------------------
| 1 | 2 | 3 | + |
|-----------------|
| 4 | 5 | 6 | - |
|-----------------|
| 7 | 8 | 9 | * |
|-----------------|
| 0 | % | / | = |
-------------------
Please input the integer arithmetic expression (format: A op B =)
After the user input the expression in the correct format, your program calculates and displays the result. If the user inputs a illegal operator, your program should print the following message and ask the user to re-input it.
Sorry, you’ve input an wrong operator. Please enter the arithmetic expressions again.
A number can’t be divided by 0, so your program should be able to deal with this properly. When one calculation is completed, your program should ask user to input ‘Y’ or ‘N’ to indicate if anther calculation is needed.
Do you want to calculate again? (Y/N)
If the input is ‘Y’, then prompts the UI again. If the input is ‘N’, then ends the execution.
Hints:
1. Your program needs to decide what operation is to be used by checking the user input. This can be done by the so- “if… elseif…else” statement in C.
if(condition_1)
expression_1;
elseif(condition_2)
expression_2;
elseif(condition_3)
expression_3;
....
else(expression_k)
2. Your program will also need to decide whether the user wants to do another calculation or not. After knowing the answer is ‘Y’, the program needs to jump back to prompt the UI again. This can be done by using the ‘goto’ statement.
Display_UI: printf(“……..”); // this is the first line of displaying the UI
………………………..
goto Display_UI;
Marking Scheme:
Task 1: 30 marks
Task 2: 10 marks for displaying the UI
10 marks for each operator “+, -, *, /, %”
5 marks for division by 0 error processing
5 marks for code layout and proper comments
Deadline: You need to demonstrate your program to Dr. Amy Zhang or me by 5:00PM, Monday, Oct. 15.