+ 1
Displaying wrong result . Why?
#include <iostream> #include <cmath> using namespace std; int main() { int a,b,c; cin >>a; cin >>b; // values // cin >>c; int d = sqrt((b*b)-(4*a*c)); // discriminant // int alpha = -b+d/2*a; //formula // int beeta = -b-d/2*a; cout <<"D="<<d<<endl; cout <<"alpha="<<alpha<<endl; cout <<"beeta="<<beeta<<endl; return 0; }
4 Respuestas
+ 2
Becz you have written int alpha = -b+d/2a and same for beta
Here you need to put round bracket u dividing value of d only
It should be like this
(-b +d)/2a
...
+ 2
Not too sure, but you might need to check whether the use of `float` or `double` as type for <a>, <b> and <c> helps. I'm just guessing it was due to integer division issue.
(Edit)
Might be considerable to rather define all variables involved in the calculation as floating point type.
+ 1
It is only working for few equations.
+ 1
These lines are incorrect:
int alpha = -b+d/2*a; /7/formula //
int beeta = -b-d/2*a;
It is an error in order of operations. Understand that -b+d/2*a is interpreted as -b+(d/2)*a.
Change them to:
int alpha = (-b+d)/(2*a); /7/formula //
int beeta = (-b-d)/(2*a);
And as already pointed out, it is likely a better choice to use floats instead of ints if accuracy is important.