How to solve Half Practice Problem from CS50's? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I'm struggling to solve this problem from CS50's introduction to computer science. It might seem simple but I still couldn't get it why it's not working properly.
So, I have to prompt a bill amount (type float) before tax and tip, calcule tax and tip using its percentage and then add them all and split it in two. The user prompt is a bill amount (type float) before taxes, tip and tax percentages. I have to complete a given function as shown below.
#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)
{
return 0.0;
}
And we should have
Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
You will owe $8.17 each!
https://cs50.harvard.edu/x/2023/problems/1/half/
What I've been trying
#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: ");
float bill = bill_amount;
float tax = tax_percent / 100;
int tip = tip_percent / 100;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * tax + bill;
float bill_after_tax_tip = bill_after_tax * tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}

Your task is to complete the function half, not to make changes in the function main. Therefore, the changes you made to the function main violate the constraints of the task.
For this reason, you should divide tax and tip by 100 in the function half, not in the function main:
#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));
}
float half(float bill, float tax, int tip)
{
float bill_after_tax = bill * ( tax / 100.0 ) + bill;
float bill_after_tax_tip = bill_after_tax * ( tip / 100.0 ) + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}
Note that it is important to write ( tip / 100.0 ) instead of ( tip / 100 ), because the former is a floating-point division, whereas the latter is an integer division, which always has an integer result. This is because tip and 100 are both of type int. For example, ( 20 / 100.0 ) evaluates to the floating-point number 0.2, whereas ( 20 / 100 ) evaluates to the integer 0. You want the result to be 0.2, not 0.

The type of tip should be float since you are dividing it by 100 which results in a decimal number.
#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: ");
float bill = bill_amount;
float tax = tax_percent / 100.0;
float tip = tip_percent / 100.0;
printf("You will owe $%.2f each!\n", half(bill, tax, tip));
}
// TODO: Complete the function
float half(float bill, float tax, float tip)
{
float bill_after_tax = bill \* tax + bill;
float bill_after_tax_tip = bill_after_tax \* tip + bill_after_tax;
float split = bill_after_tax_tip / 2;
return split;
}

Related

cs50 return gives wrong value

When I run this code I expect the return give me (100-percent)/100 - price
#include<stdio.h>
#include<cs50.h>
float discount(int prec , float pri);
int main (void){
int percent = get_float("enter the present here ");
float price = get_float("enter the price ");
float function1 = discount(percent , price);
printf("the new number is %.2f\n", function1);
}
float discount(int prec , float pri){
return (100 - prec)/100 * pri ;
}
but I got this return and I don't know what the problem is
$ make lesson
$ ./lesson
enter the present here 15
enter the price 100
the new number is 0.00
when I use this code
#include<stdio.h>
#include<cs50.h>
float discount(int prec , float pri);
int main (void){
int percent = get_float("enter the present here ");
float price = get_float("enter the price ");
float function1 = discount(percent , price);
printf("the new number is %.2f\n", function1);
}
float discount(int prec , float pri){
return pri * (100 - prec)/100 ;
}
I got the correct value but I want to know why the first example didn't work
$ make lesson
$ ./lesson
enter the present here 14
enter the price 100
the new number is 86.00

Output coming back as 0.00

in my code i am supposed to ask the user to enter total sales, there county sales tax, there state sales tax, then return the total tax collected and also return what the state and county sales tax is.
i have no errors but my output is coming back as:
Your county sales tax is 0.00
Your state sales tax is 0.00
your total tax collected is 0.00
here is my code:
#include <stdio.h>
void inputTaxData(int *, float *, float *);
float calculateTaxes(int, float, float);
void displayTaxData(float, float, float);
int main()
{
int totalSales;
float ctax, stax;
float totalTax;
inputTaxData(&totalSales, &ctax, &stax);
calculateTaxes(totalSales, ctax, stax);
displayTaxData(ctax, stax, totalTax);
return 0;
}
void inputTaxData(int *totalSalesPtr,float *ctaxPtr, float *staxPtr)
{
int totalSales;
float ctax, stax;
printf("\nWhat is your total sales for the month?");
scanf("%d", &totalSales);
*totalSalesPtr = totalSales;
printf("\nWhat is your county sales tax?");
scanf("%f", &ctax);
*ctaxPtr = ctax;
printf("\nWhat is your state sales tax?");
scanf("%f", &stax);
*staxPtr = stax;
}
float calculateTaxes(int totalSales, float ctax, float stax)
{
float totalTax = totalSales * ctax + stax;
return totalTax;
}
void displayTaxData(float ctax, float stax, float totalTax)
{
printf("\nYour County Sales tax is %.2f", &ctax);
printf("\nYour State Sales tax is %.2f", &stax);
printf("\nYour total tax collected is %.2f", totalTax);
}
Here are the portions of the code which need correction.
Issue 1:
In the function void inputTaxData(int *totalSalesPtr,float *ctaxPtr, float *staxPtr), you already are passing the addresses(pointers) of the variables.Hence you don't have to re-declare local variables in the function.
Fix:
All you gotta do is to assign the values obtained by scanf to these locations.
Issue 2:
The function float calculateTaxes(int totalSales, float ctax, float stax) returns the totalTax but this returned value is not used in main.
Fix:
Assign the returned value to totalTax in main.
Issue 3:In the function void displayTaxData(float ctax, float stax, float totalTax) you are passing a copy of the literal values of the arguments but are trying to print the addresses of ctax and stax.
Fix:Print the literal values of the arguments passed to this function.
Applying the fixes, your code would look like this.....
#include <stdio.h>
void inputTaxData(int *, float *, float *);
float calculateTaxes(int, float, float);
void displayTaxData(float, float, float);
int main()
{
int totalSales;
float ctax, stax;
float totalTax;
inputTaxData(&totalSales, &ctax, &stax);
totalTax = calculateTaxes(totalSales, ctax, stax);//assign the returned value
displayTaxData(ctax, stax, totalTax);
return 0;
}
void inputTaxData(int *totalSales,float *ctax, float *stax)
{
printf("\nWhat is your total sales for the month?");
scanf("%d", totalSales);//assign the scanned value to the variable
printf("\nWhat is your county sales tax?");
scanf("%f", ctax);//assign the scanned value to the variable
printf("\nWhat is your state sales tax?");
scanf("%f", stax);//assign the scanned value to the variable
}
float calculateTaxes(int totalSales, float ctax, float stax)
{
float totalTax = totalSales * ctax + stax;
return totalTax;
}
void displayTaxData(float ctax, float stax, float totalTax)
{
printf("\nYour County Sales tax is %.2f", ctax);//print value
printf("\nYour State Sales tax is %.2f", stax);//print value
printf("\nYour total tax collected is %.2f", totalTax);
}

Score Reporting problems

Can u guys help me how to make a simple report calculation
1st score x 20%
2nd score x 40%
3rd score x 40%
Sample :
Input :
65 56 100
Output :
75.00
My code :
#include <stdio.h>
#include <math.h>
int main() {
int score1, score2, score3;
float amount;
float n;
float percent1 = 0.2;
float percent2 = 0.4;
float percent3 = 0.4;
scanf("%f %f %f",&n, &n, &n);
score1 = (float)n*percent1;
score2 = (float)n*percent2;
score3 = (float)n*percent3;
amount = score1+score2+score3;
printf("%.2f\n", amount);
getchar();
return 0;
}
my input:
65 56 100
my output:
100.00
u can see it there, the output must be 92.00
is there any mistakes?
Help me pls, ty
You are using the same variable (n) for all three input values. You need a separate variable for each input value. Use the score variables to store the values and then use them in a single calculation.
#include <stdio.h>
#include <math.h>
int main()
{
int score1, score2, score3;
float amount;
/* Variable 'n' not required, since not used. */
float n;
float percent1 = 0.2;
float percent2 = 0.4;
float percent3 = 0.4;
scanf("%f %f %f",&score1, &score2, &score3);
amount = (score1 * percent1) + (score2 * percent2) + (score3 * percent3);
printf("%.2f\n", amount);
getchar();
return 0;
}

"Wrong answer" in HackerRank "Day 2: Operators" challenge

I am a novice programmer and coder. So, I have taken 30 days of coding challenge in HackerRank, but when I am running the "Day 2: Operators" problem in C, it shows no errors. The code is:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int total;
double meal_cost;
scanf("%lf", &meal_cost);
int tip_percent;
scanf("%i", &tip_percent);
int tax_percent;
scanf("%i", &tax_percent);
double tip = (meal_cost * tip_percent) / 100;
double tax = (meal_cost * tax_percent) / 100;
total = (int)(meal_cost + tip + tax);
printf("The total meal cost is %d dollars.", total);
printf("The total meal cost is %d dollars.", total);
return 0;
}
Input:
12.00
20
8
Expected output:
The total meal cost is 15 dollars.
Actual output:
Wrong Answer
This is more clear program. As it was discovered tips are NOT taxed.
Calculations are done on double to avoid early truncations. Final result is printed as double or as needed by Hackerrank rounded to the int number.
Contribution and help of Weather Vane is greatly appreciated.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("The total meal cost is %.2f dollars.\n",total);
printf("The total meal cost is %d dollars.\n", (int)(total+0.5));
return 0;
}
Output:
12
20
8
The total meal cost is 15.36 dollars.
The total meal cost is 15 dollars.
Running code "Day 2: Operators" problem in C
- Given the meal price (base cost of a meal), tip percent (the
percentage of the meal price being added as tip), and tax percent
(the percentage of the meal price being added as tax) for a meal,
find and print the meal's total cost in a HackerRank
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
double total, meal_cost, tip_percent, tax_percent, tip, tax;
scanf("%lf", &meal_cost);
scanf("%lf", &tip_percent);
scanf("%lf", &tax_percent);
tip = (meal_cost*tip_percent/100.0);
tax = (meal_cost*tax_percent)/100.0;
// Tip taxed:
// tax = (meal_cost + tip) * (tax_percent/100.0);
// no tax on the tip:
tax = meal_cost * (tax_percent/100.0);
total= meal_cost + tip + tax;
printf("%d", (int)(total+0.5));
return 0; }
Input
12.0
20
8
Output
15

Nothing happens when I run program

The guys just helped me with a problem, got solved 100%, but now I have another one.
I compile my files and everything without problems, but when i want to run the program nothing happens.
enter.c : http://pastebin.com/GGzVeAhw
simple_interest.c: http://pastebin.com/XdESrxSk
This is how my header file looks like:
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
PS This forum is really helpful, especially for a beginner like me.
You have a bunch of prototypes in main(), but no function call.
It should be:
int main(void)
{
enter();
return 0;
}
You seem really confused about how to declare and use functions, perhaps you need some tutorial?
simple_interest.h
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
simple_interest.c
//Implement all functions in this file
#include <stdio.h>
double Calc_Interest(double principal, double rate, int term)
{
double interest;
interest = principal * (rate/100) *term;
return interest;
}
double Calc_Amount(double principal, double interest)
{
double total;
total = principal + interest;
return total;
}
double Calc_Payments(double total, int term)
{
double mPay;
mPay= total / (12 * term);
return mPay;
}
void Display_Results(double principal, double interest, double total, double mPay)
{
printf("\n");
printf("Personal Loan Statement\n");
printf("\n");
printf("Amount Borrowed: %.2f\n" , principal);
printf("Interest on Loan: %.2f\n" , interest);
printf("--------------------------------------------------------------------------------\n");
printf("Amount of Loan: %.2f\n" , total);
printf("Monthly Payment: %.2f\n" , mPay);
}
sample.c
#include <stdio.h>
#include "simple_interest.h"
int enter(void)
{
double principal, rate, interest, total, mPay;
int term;
printf("--------------------------------------------------------------------------------\n");
printf("This program calculates the monthly payments due from a personal loan\n");
printf("--------------------------------------------------------------------------------\n");
printf("Enter amount of the loan: ");
scanf("%lf", &principal);
printf("Enter current annual interest rate in \n");
scanf("%lf", &rate);
printf("Enter the number of years of the loan: ");
scanf("%d", &term);
interest = Calc_Interest(principal, rate, term);
total = Calc_Amount(principal, interest);
mPay = Calc_Payments(total, term);
Display_Results(principal, interest, total, mPay);
return 0;
}
int main(void)
{
enter();
return 0;
}
compile like this way
gcc sample.c simple_interest.c
./a.out

Resources