+ 1

anyone who can help me fix this c++ code,kindly? It was to solve the real roots of a quadratic equation.

#include <iostream> #include<cmath> using namespace std; int main() { //quadratic eqn is in form of a*a + 2*a +c // our a*a will be unknown_a //our 2*a will be unknown_b //our c will be num_c double unknown_a, unknown_b, num_c, real_roots,real_root2; cout << "enter the first unknown" << endl; cin >> unknown_a; cout << "enter the second unknown" << endl; cin >> unknown_b; cout << "enter the third value(C)" << endl; cin >> num_c; real_roots=- unknown_b+ ( sqrt((unknown_b*unknown_b)-(4*unknown_a*num_c))/2*unknown_a); cout << "root one is:"; cout << real_roots<<endl; real_root2=- unknown_b-( sqrt((unknown_b*unknown_b)-(4*unknown_a*num_c))/2*unknown_a); cout << "root two is:"; cout << real_root2; return 0; }

13th Aug 2021, 4:35 PM
Alexender Mwaniki
Alexender Mwaniki - avatar
3 ответов
+ 3
The parentheses in your formula are wrong. Recall that the quadratic polynomial is: ax²+bx+c And its real roots are: x=(-b±√(b²-4ac))/2a Also, the 'unknowns' are not really unknowns when the user inputs them 😛
13th Aug 2021, 7:45 PM
Giorgos
+ 2
Your intention is to divide by 2a, but order of operations causes it to divide by 2 and then multiply by a. As Giorgos D indicated, it can be fixed by using parentheses that force the order of operations. Replace /2*unknown_a with /(2*unknown_a).
13th Aug 2021, 11:48 PM
Brian
Brian - avatar
+ 1
This will help you if you like it to analyze: https://code.sololearn.com/c079doXhZlMI/?ref=app
13th Aug 2021, 8:48 PM
JaScript
JaScript - avatar