0

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();
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 4
    `/ 2 * a` divides by 2 and multiplies by `a` – Raymond Chen Jul 04 '22 at 05:02
  • if i may ask how should i write it – Kimi Andrew Jul 04 '22 at 05:06
  • 1
    With parentheses – Silvio Mayolo Jul 04 '22 at 05:26
  • 1
    As a hint `x/y*z` is equivalent to `(x/y)*z` which differs from `x/(y*z)`. There's more than one set of parentheses needed in your code. Unrelated : if `b*b - 4*a*c` is negative, a quadratic equation still has roots - they only difference is that they have an imaginary component. – Peter Jul 04 '22 at 05:41
  • Please ensure you post code you're actually using, and do not make modifications after posting without verifying. As you have not used `using namespace std;` or `using std::cout; using std::cin;`, your code should not compile at all. – Chris Jul 04 '22 at 06:11
  • In fact don't use "using namespace std" and type std::cout. (https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also consider not calculating things twice, and separating the output from the calculation. Let roots return a struct with the calculated values. – Pepijn Kramer Jul 04 '22 at 06:47
  • `"Negative value returned from (b2 - 4ac), please try again!";` -- But then, you call `exit(1);`. You don't give the user a chance to try again, since you terminated the program. – PaulMcKenzie Jul 04 '22 at 07:05
  • Do `sqrt(discriminant)` only if `discriminant > 0` and preferably do it just once. `discriminant == 0` only has one solution. and you can print `a` as `(a == 1) ? "" : std::to_string(a)`. Same for `b == 1` and slightly more complex `a/b/c == 0`. Or you can use `std::cout << "your bla bla "; if (a != 1) std::cout << a << " " ; std::cout << "x^2"; ...` – Goswin von Brederlow Jul 04 '22 at 13:30
  • @Chris i think i forgot the namespace std – Kimi Andrew Jul 04 '22 at 14:02

2 Answers2

1

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.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • Finally, a sensible answer. I'd recommend checking all the special cases other than discriminant less than zero: a = 0, b = 0, and c = 0. All three are meaningful. – duffymo Jul 04 '22 at 12:49
  • Oh my god thank you so much John for the answer. This is the answer that helped me with the code – Kimi Andrew Jul 04 '22 at 13:59
  • 1
    If this answer helped you, you can recognize that by accepting it. – Chris Jul 04 '22 at 16:00
0

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();
}
CompuChip
  • 9,143
  • 4
  • 24
  • 48
  • 2
    Please explain what the point of this blob of code is. – j6t Jul 04 '22 at 06:53
  • 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). – Sowaiba9801 Jul 04 '22 at 07:15