Home > Software engineering >  Wrong root from quadratic equation calculator
Wrong root from quadratic equation calculator

Time:07-04

I was wondering if someone could help me in this problem. So i tested the code but it didn't show the right answer below for equation result of x2 5x 6

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
        
double roots() {
    double a, b, c, x1, x2;
    cout << "Enter quadratic equation in order (a, b, c): \n";
    cin >> a >> b >> c;
    double discriminant = b * b - 4 * a * c;
    x1 = -b   sqrt(discriminant) / 2 * a;
    x2 = -b - sqrt(discriminant) / 2 * a;
    
    if (discriminant >= 0 && a > 1) {
        cout << "Your quadratic equation is " << a << "x^2   " << b << " x   " << c << '\n';
        cout << "x1 = " << x1 << '\n';
        cout << "x2 = " << x2 << '\n';
    }
    else if (a == 1) {
        cout << "Your quadratic equation is " << "x^2   " << b << " x   " << c << '\n';
        cout << "x1 = " << x1 << '\n';
        cout << "x2 = " << x2 << '\n';
    }
    else {
        cout << "Negative value returned from (b2 - 4ac), please try again!";
        exit(1);
    }
}
    
int main() {
    roots();
}

CodePudding user response:

You have the formula incorrect. Try this

x1 = (-b   sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);

Notice the extra parenthesis in order to put 2*a in the denominator and have it divide both b and the sqrt().

You also need to check if discriminant >= 0 before doing so, because if it is negative there is no root and the above lines are going to fail.

CodePudding user response:

Firstly, using namespace std was not used, so if you don't want to use it write std::court and std::cin. Secondly, the formula is b^2 -4ac so you need to put round brackets around b*b so that the answer is subtracted from -4ac. Then, you don't need to write else if for a==1 you can add it in the above condition as a>=1 and else put down a condition where discriminant is >=0 but a==0 which violates quadratic eq condition and you can write a cannot be equal to zero. Also, the main formula for x1 and x2 is wrong since the bracket should be applied around -b sqroot(discriminant) so that the answer is then divided by multiplication of (2*a). Otherwise, what happens is that first sqrt is divided by 2 then multiplied by a and then added to -b.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
    
double roots() {
double a, b, c, x1, x2;
std::cout << "Enter quadratic equation in order (a, b, c): \n";
std::cin >> a >> b >> c;
double discriminant = (b * b)- (4 * a * c);
x1 = (-b   sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant))/ (2 * a);

if (discriminant >= 0 && a >= 1) {
    std::cout << "Your quadratic equation is " << a << "x^2   " << b << " x 
  " << c << '\n';
    std::cout << "x1 = " << x1 << '\n';
     std::cout << "x2 = " << x2 << '\n';
}
else if (a==0){
     std::cout << "a cannot be zero!";
    exit(1);
}
else{
     std::cout << "Negative value returned from (b2 - 4ac), please try again!";
    exit(1);
}
}

int main() {
roots();
}
  • Related