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