Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 21 days ago.
Improve this question
It's supposed to be a simple program, I'm learning to program and this is just for fun.
It was to print out the information about a circle.
#include <stdio.h>
float pi;
int radius;
int circumference;
int area;
int main()
{
printf("What radius is your circle?\n");
scanf("%d", &radius);
pi == (3.14);
area == (radius * radius * pi);
circumference == (2 * radius * pi);
printf("The area of your circle is %d, and the circumference is %d\n", area, circumference);
return(0);
}
Looks like you are using comparison operator == instead of assignment operator =. Change these lines:
pi == (3.14);
area == (radius * radius * pi);
circumference == (2 * radius * pi);
by
pi = (3.14);
area = (radius * radius * pi);
circumference = (2 * radius * pi);
Also, try to use float point arithmetic float or double), since your result and constant PI are float point numbers
The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false.
DO NOT use == for assignment. Instead of this operator, use = for assignment. for example :
pi = 3.14;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
Edited: fixed some typos, also add more context
So I tried to put this code:
#include <stdio.h>
int main() {
float ps, ls, ms, es;
printf("Enter the project score: ");
scanf("%d", &ps);
printf("Enter the long exam score: ");
scanf("%d", &ls);
printf("Enter the midterm exam score: ");
scanf("%d", &ms);
90 = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
printf("Final exam score needed: %d", es);
return 0;
}
As I want this equation 90=85(.15)+88(.2)+92(.25)+x(.4)
but it states that "lvalue required as left operand of assignment"
As others point out you need to use %f to read in float.
But here's an example that solves your equation:
Given the scores of the Project, Long Exam and Mid-term what score in the Final Exam is required for a weighted average of 90.
C is an 'imperative' procedural programming language.
You need to specify how to derive the es value.
It can't be expected to solve your equation.
The = means "assign to the variable on the left the value on the right".
It's not a statement of equality or even a logical test. It's an act of assigning a value into a variable.
I'm labouring the point because a 'breakthrough' understanding in programming is that equals "=" doesn't always mean what it does in Maths. In fact rarely does!
If you try 90, 90 and 90 below it will say 90 (which makes sense).
If you try 80, 85 and 90 you need a final score of 93.125.
This code does a back check to show the value calculated gives the right weighted score.
#include <stdio.h>
int main() {
float ps, ls, ms, es;
printf("Enter the project score: ");
scanf("%f", &ps);
printf("Enter the long exam score: ");
scanf("%f", &ls);
printf("Enter the midterm exam score: ");
scanf("%f", &ms);
printf("\nps=%f ls=%f ms=%f\n",ps,ls,ms);
es=(90-(ps * 0.15) - (ls * 0.2) - (ms * 0.25) )/0.4;
printf("Final exam score needed: %f\n", es);
float check=ps*0.15+ls*0.2+ms*0.25+es*0.4;
printf("Giving final score of %f\n",check);
return 0;
}
Typical Output:
Enter the project score: Enter the long exam score: Enter the midterm exam score:
ps=80.000000 ls=85.000000 ms=95.000000
Final exam score needed: 93.125000
Giving final score of 90.000000
For starters you declared variables as having the type float
float ps, ls, ms, es;
So to enter values to the variables you have to use the conversion specifier f instead of d like
printf("Enter the project score: ");
scanf("%f", &ps);
This statement
90 = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
does not make a sense. You may not change an integer constant. Also it is unclear why there is used the magic number 90.
As in the next statement you are trying to output the value of the variable es again using the invalid conversion specifier d instead of f then it seems you mean
es = (ps * 0.15) + (ls * 0.2) + (ms * 0.25) * (es * 0.4);
printf("Final exam score needed: %f\n", es);
However pay into account that the variable es was not initialized. So the program will have undefined behavior.
Maybe you mean something like (though it is difficult to say what actually you mean)
es = (ps * 0.15) + (ls * 0.2) + (ms * 0.25);
es *= 0.4;
You have
90=85(.15)+88(.2)+92(.25)+x(.4)
This should be
// solve equation for x
float x = (90 - (85*(.15)+88*(.2)+92*(.25))) / .4;
printf("Final exam score needed: %f\n", x);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 12 months ago.
Improve this question
#include <stdio.h>
#include <math.h>
double areaTriangle(double sideA,double sideB,double sideC);
double periTriangle(double sideA,double sideB,double sideC);
int main (void){
double side1A,side1B,side1C;
double side2A,side2B,side2C;
double area1, area2;
printf("Enter the side for the first triangle: ");
scanf("%lf %lf %lf",&side1A,&side1B,&side1C);
printf("\nEnter the sides for the second triangle: ");
scanf("%lf %lf %lf",&side2A,&side2B,&side2C);
area1=areaTriangle(side1A,side1B,side1C);
area2=areaTriangle(side2A,side2B,side2C);
printf("Area 1 is %lf and Area 2 is %f",area1,area2);
return 0;
}
double areaTriangle(double sideA,double sideB,double sideC){
double area=0;
periTriangle(sideA,sideB,sideC);
sqrt(periTriangle*(periTriangle-sideA)*(periTriangle-sideB)*(periTriangle-sideC);
}
double periTriangle(double sideA,double sideB,double sideC){
periTriangle=(sideA+sideB+sideC)/3;
return periTriangle;
}
So when I type in any values for any of the triangles I get zero no what I do. For example, I input 10 for all sides in both triangles and all I get is: Area 1 is 0.000000 and Area 2 is 0.000000. Why tho?
periTriangle(sideA,sideB,sideC);
does not save the return value;
It could be double peri = periTriangle(sideA,sideB,sideC);
(and below that use peri where you used periTriangle)
Also, the parens don't match where you call sqrt.
Also, I think you should divide by 2, not 3 in periTriangle, no?
Heron's Formula for Area of a Triangle with three sides known.
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 7 years ago.
Improve this question
Here is my code.
float total,rate;
rate = score / 25;
printf("Total: %f", rate);
But this doesn't work; it always outputs 0.000. Can you help?
I'm going out on a limb here and say that you have score declared as an int. int divided by int will always result in an int. You can fix this by either:
declare score as float
cast it as (float)score
multiply it with a float rate = score * 1.0f / 25
change 25 to 25.0f
Try this:
rate = score / 25.0;
printf("Total: %f", rate);
Or:
rate = (float) score / 25;
printf("Total: %f", rate);
An int divided by an int will always be an int, with everything "after the decimal" truncated.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why does f is 0.0 even after adding 0.1?
#include <stdio.h>
int main()
{
float f=0.0f;
f = f + 0.1f;
printf("f %f \n",&f);
return 0;
}
Sorry for that I messed up with original question
Why these two values are not ? Is this because of precision.
Sorry I have to ask this question here because I'm blocked I can't ask more questions
#include <stdio.h>
int main()
{
float f=0.0f;
int i;
for(i=0;i<10;i++)
{f = f + 0.1f; }
if(f == 1.0f)
printf("f is 1.0 \n");
else
printf("f %f is NOT 1.0\n",f);
return 0;
}
printf("f %f \n",f);
Gives correct output. See here-https://ideone.com/158Zbv
To print address printf(" %p \n",&f); will do it .
Where as your printf statement will give undefined behaviour.
About your second code -
You can re-write your if condition like this -
if(f>0.99f && f<1.01f)
So that it gives you correct output- https://ideone.com/kARx3A
EDIT
While adding in your program value of f is not exactly 1.0f but it may have some different value ,when we see its value with more decimal places it is bit different than 1.0f that's why your code always go to else part .
See here what value f has with every iteration-https://ideone.com/wJ7u1R
you are printing address of variable f by using & operator, remove this & operator.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm doing a exercise from C primer plus that involves working with floats, I can't get the result to also be a float. I got it to read in the input fine (as far as I can tell) but the problem must be in the formula line. Can anyone tell me what I'm doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
float inp1, inp2;
float result;
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
printf("(%.3f - %.3f) / (%.3f * %.3f)\n", inp1, inp2, inp1, inp2);
printf("%f\n", result);
}
The output is giving some random number (I don't know what it's called), for example with an input of 1.255 and 1.023 I get an output of 2656044210243861500000000000000000000.000000 or something similar. The second to last printf displays the input correctly.
What am I doing wrong?
I've tried this, but I don't really get how cast operators work.
result = ((float)inp1 - (float)inp2) / ((float)inp1 * (float)inp2); //formula
and
result = (float)(inp1 - inp2) / (inp1 * inp2);
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
Well, what about putting scanf call before result = ...; assignment statement?
result = (inp1 - inp2) / (inp1 * inp2); /* formula */
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
That's not how things work in a language like C; you don't define a formula for result that's automatically computed when you read your inputs. Rather, you first read your inputs, and then you compute the value and assign it to result:
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
result = (inp1 - inp2) / (inp1 * inp2); /* computation and assignment */
C is an imperative language, meaning you have to explicitly list out the instructions for it to follow, including those for any mathematical computations.
The analogy of math formulas in programming languages is functions (without side effects). That way, you can define formulas before the code itself. For instance:
#include <stdio.h>
#include <stdlib.h>
float formula(float inp1, float inp2)
{
return (inp1 - inp2) / (inp1 * inp2);
}
int main(void)
{
float inp1, inp2;
printf("enter two values\n");
scanf("%f %f", &inp1, &inp2);
printf("(%.3f - %.3f) / (%.3f * %.3f)\n", inp1, inp2, inp1, inp2);
printf("%f\n", formula(inp1, inp2));
return 0;
}