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;
}
Related
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;
}
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
Reference Image
The output always comes: 6.35 (I think it's a garbage value)
the code is
#include<stdio.h>
#include<math.h>
float formula(float,float);
int main()
{
float l=0,a=0;
printf("enter the length of the pendulum(l)\n");
scanf("%f",&l);
printf("Enter the angle of displacemnt(a)\n");
scanf("%f",&a);
printf("the length is %0.2f\n",l);
printf("the angle of displacemnt is %0.2f\n",a);
printf("the period of pendulum is %0.2f",formula(l,a));
return 0;
}
float formula(float l, float a)
{
float P=0,ran=0,g = 9.8;
ran = (l/g) * (1 + ((1/4)*(pow((float)(sin((double)a/2)),2))));
P = 2 * M_PI * ((float)sqrt((double)ran));
return P;
}
I don't know what is happening 😐
You can simplify this implementation by using doubles or floats exclusively. The 1/4 integer division yields 0. Here's an example:
#include <stdio.h>
#include <math.h>
float formula(float l, float a)
{
float ran = l / 9.8f * (1.0f + powf(sinf(a/2.0f), 2.0f) / 4.0f);
return 2.0f * (float) M_PI * sqrtf(ran);
}
int main(void)
{
float l;
float a;
printf("Enter the length of the pendulum(l): ");
scanf("%f", &l);
printf("Enter the angle of displacemnt(a): ");
scanf("%f", &a);
printf("The length is %0.2f\n", l);
printf("The angle of displacemnt is %0.2f\n", a);
printf("The period of pendulum is %0.2f\n", formula(l,a));
return 0;
}
I have input the values of d = 10 s = 5 and h = 4 and I got a different output than expected.
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",& d,&s,&h);
double E= sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T=sqrt((2*s)/d*h);
printf ("T=%lf\n",T);
return 0;
}
Expected output
E = 5
T = 0.5
What I get
E = 5
T = 2
double T=sqrt((2*s)/d*h);
'/' and '*' has same precedence so the order of evaluation is left to
right.
so first (2xs) = 10 next 10/10 = 1; at last 1x2 = 2;
Hence T is 2.
For more info - https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/#:~:text=Operator%20precedence%20determines%20which%20operator,one%20operators%20with%20different%20precedence.&text=Operators%20Associativity%20is%20used%20when,Right%20or%20Right%20to%20Left.
Try this one I am getting E=5 and T=0.5
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",&d, &s, &h);
double E = sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T = sqrt((2*s)/(d*h));
printf ("T=%lf\n",T);
}
This is the output I got
E=5.000000
T=0.500000
I made a program which accepts input for 30 users (About their age)
and then the array was supposed to be an input in a custom function made by me (avg_age)
However the average printed on screen is 0.0000 (for 30 non-zero values)
That is why I think it does not return anything.
#include <stdio.h>
float avg_age(int age[]);
main()
{
int i=0,age[30]={0},intemp;
do{
printf("Input age for 30 users: ");
scanf("%d",&intemp);
if(intemp>0 && intemp<100)
intemp=age[i];
else i--;
i++;
}while(i<30 || intemp<0 || intemp>100);
printf("\nAverage age of 30 users: %f\n",avg_age(age));
float avg_age(int age[]){
int i,avg=0;
for(i=0;i<30;i++)
avg+=age[i];
avg=(float)avg/30;
return avg;
}
}
Take out the function definition of avg_age out of the main. You declared avg as int but it should be declared float to store float values.
float avg = 0.0;
In main intemp=age[i]; is not storing the inputs to the array age instead assigning 0 each time to intemp. Change it to
age[i] = intemp;
Your modified code: (for 5 users)
#include <stdio.h>
#define N 5
float avg_age(int age[]);
int main(void)
{
int i=0,age[N]={0},intemp;
do{
printf("Input age for %d users: ", N);
scanf("%d",&intemp);
if(intemp>0 && intemp<100)
age[i] = intemp;
else i--;
i++;
}while(i<N || intemp<0 || intemp>100);
printf("\nAverage age of %d users: %f\n",N, avg_age(age));
return 0;
}
float avg_age(int age[]){
int i;
float avg=0.0;
for(i=0;i<N;i++)
avg+=age[i];
avg=(float)avg/N;
return avg;
}
Here is the tested code.
Your avg inside avg_age should be a float
float avg_age(int age[]){
int i; float sum=0.0;
for(i=0;i<30;i++)
sum += age[i];
return sum/30;
}
and of course the above function should be outside of main (preferably before it).
BTW, you should have compiled with all warnings and debugging info (e.g. with gcc -Wall -g). The compiler certainly would have warned you! Also, learn how to use the debugger (e.g. gdb)