- Work with different data types
- Practice type casting
- Use math operations
- Create a function with input parameters and return value
Suppose you are eating out at a restaurant with a friend and want to split the bill evenly. You may want to anticipate the amount you’ll owe before the bill arrives with tax added. In this problem, you’ll complete a funtion to calculate the amount each of you owes based on the bill amount, the tax, and the tip.
- Note that the tax and tip are input as percents. You may want to change these to decimal values before calculating the total.
- Since the tip is input as an
int, keep in mind that if you divide an
intyou’ll get back an
- Order of operations here is exactly the same as in algebra, where multiplcation and division are performed before addition and subtraction.
- Log into code.cs50.io using your GitHub account.
- Click inside the terminal window and execute
- At the
- Now execute
- Then copy and paste
wget https://cdn.cs50.net/2022/fall/labs/1/half.cinto your terminal to download this lab’s distribution code.
- You are to complete the function,
half, which calculates exactly half of the bill after tax and tip are added, and returns that value.
Your function should use the input parameters,
tip, to calculate the final amount. However, since these values are percentages, you’ll have to do some work to convert these to more appropriate formats to use for your calculation.
The final amount due, should add the tax to the bill before calculating the tip. Finally, you will return exactly half of the full amount, including the bill amount, the tax and the tip.
- Why do you think there are different data types in C?
How to Test Your Code
Your program should behave per the examples below.
half/ $ ./half Bill before tax and tip: 12.50 Sale Tax Percent: 8.875 Tip percent: 20 You will owe $8.17 each!
half/ $ ./half Bill before tax and tip: 23.50 Sale Tax Percent: 7 Tip percent: 15 You will owe $14.46 each!
half/ $ ./half Bill before tax and tip: 100 Sale Tax Percent: 6.25 Tip percent: 18 You will owe $62.69 each!
You can check your code using
check50, a program that CS50 will use to test your code when you submit, by typing in the following at the
$ prompt. But be sure to test it yourself as well!
Green smilies mean your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that
check50 outputs to see the input
check50 handed to your program, what output it expected, and what output your program actually gave.
To evaluate that the style of your code (indentations and spacing) is correct, type in the following at the
How to Submit
No need to submit! This is an optional practice problem.