# CS50-HALF Practice Problem – What can I do to troubleshoot my code, which is functioning properly but not producing the desired output?

CS50 Practice Problem HALF –

Background

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.

Implementation Details

Your function should use the input parameters, `bill`, `tax`, and `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.

## Thought Question

• 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!
``````

This is the code that i wrote –

``````// Calculate your half of a restaurant bill
// Data types, operations, type casting, return value

#include <cs50.h>
#include <stdio.h>

float half(float bill, float tax, int tip);

int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");

printf("You will owe \$%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float total = (bill + (bill * (tax / 100)) + (bill * (tip * .01))) / 2;
return ((int)(total * 100.0 + 0.5) / 100.0);
}
``````

All that has to be done in this problem is to complete the half function the rest was already given. I completed the function and can’t see anything wrong but it is not giving the required output . Every single required output is wrong .

For example the above three examples that are given in the problem outputs-

1-

``````You will owe \$8.05 each!
``````

2-

``````You will owe \$14.34 each!
``````

3-

``````You will owe \$62.13 each!
``````

Here are the outputs that were required and what i got – https://submit.cs50.io/check50/8bbdc3f80441100b5f1a4e842ef6efe1b7f443fd

``````## cs50/labs/2023/x/half
``````

``````### :) half.c exists
``````

Log

``````checking that half.c exists...

### :) half.c compiles
``````

Log

``````running clang half.c -o half -std=c11 -ggdb -lm -lcs50...

### :( Bill of \$50, with 10% tax and 20% tip, creates output of \$33.00
``````

Cause

``````expected "33.00", not "You will owe \$..."
``````

Log

``````running ./half...
sending input 50...
sending input 10...
sending input 20...
checking for output "33.00"...
**Expected Output:**
33\.00**Actual Output:**
You will owe \$32.50 each!

### :( Bill of \$50, with 12.5% tax and 20% tip, creates output of \$33.75
``````

Cause

``````expected "33.75", not "You will owe \$..."
``````

Log

``````running ./half...
sending input 50...
sending input 12.5...
sending input 20...
checking for output "33.75"...
``````

Expected Output:

``````33\.75**Actual Output:**
You will owe \$33.13 each!

### :( Bill of \$100, with 12.5% tax and 15% tip, creates output of \$64.69
``````

Cause

``````expected "64.69", not "You will owe \$..."
``````

Log

``````running ./half...
sending input 100...
sending input 12.5...
sending input 15...
checking for output "64.69"...
``````

Expected Output:

``````64\.69
``````

Actual Output:

``````You will owe \$63.75 each!
``````

enter image description here

### >Solution :

Simply split one lonng hard to read equation into smaller bits:

``````float half(float bill, float tax, int tip)
{
float total = bill * ( 1 + tax/100.0);
total = total * (1 + tip/ 100.0);
}
``````

Isn’t it easier to read and understand? Compiler does not care.

Make your life easier when debug and do not user input – just create some test cases. It will save you plenty time

``````float half(float bill, float tax, int tip);

int main(void)
{
printf("You will owe \$%.2f each!\n", half(50.0f, 10.0f, 20.0f));
printf("You will owe \$%.2f each!\n", half(50.0f, 12.5f, 20.0f));
printf("You will owe \$%.2f each!\n", half(100.0f, 12.5f, 15.0f));
}

// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float total = bill * ( 1 + tax/100.0);
total = total * (1 + tip/ 100.0);