Question

I wrote this code to find the roots of quadratic equations. The code is this:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double a,b,c;
    double x,x2;
    cout<<"Give a: ";
    cin>>a;
    cout<<"Give b: ";
    cin>>b;
    cout <<"Give c: ";
    cin>>c;
    if (a==0)
    {
        if (b==0)
        {
            if (c==0)
            {
            cout<<"Solution indeterminable";
            return 0;
            }
            else 
            {
            cout<<"No solution";
            return 0;
            }   
        }
        else
        {
        x=-c/b;
        cout<<"The only root is x: "<<x;
        return 0;
        }

    }
    else
    {
    double b_sqr=b*b;
    if (b_sqr>4*b*c)
        {
        cout<<"Complex roots: ";
        return 0;
        }
    else if (b_sqr==4*b*c)
        {
        x=-b/(2*a);
        cout<<"The only solution is x: "<<x;
        return 0;
        }
    else
        {
            x=-b+(sqrt((b*b)-(4*a*c)))/(2*2);
            x2=-b-(sqrt((b*b)-(4*a*c)))/(2*2);
            cout<<"The first root is x1: "<<x;
            cout<<"The first root is x2: "<<x2;
            return 0;
        }
    }
}

No matter what i type, it either finds two roots of -1, or one root of -1. I cannot understand whats wrong in my logic. Everytbhing seems fine

EDIT:

This is a case where you have no compiler mistakes, yet the code doesn't seem to work. This happens when the code is 100% correct, yet the errors are not on the syntax or grammar of the language in your code, but rather on the logic behind it.

Before you start coding, you should make sure that the references you are using detailing the algorithmical solutions of the problem you are trying to solve are correct.

When you get no compiler errors, yet the program doesn't run as expected, then you should check the 'details' of your program. Are your formulas correct? Are you sure you are using the right equations? (And like i said before, make sure that the ones you reference are indeed correct).

Anyways, this question answered indirectly an important topic on software development. But for those who came here interested in a C++ program that solves the quadratic equation, here is the working code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double a,b,c;
    double x,x2;
    cout<<"Give a: ";
    cin>>a;
    cout<<"Give b: ";
    cin>>b;
    cout <<"Give c: ";
    cin>>c;
    if (a==0)
    {
        if (b==0)
        {
            if (c==0)
            {
            cout<<"Solution indeterminable";
            return 0;
            }
            else
            {
            cout<<"No solution";
            return 0;
            }
        }
        else
        {
        x=-c/b;
        cout<<"The only root is x: "<<x;
        return 0;
        }

    }
    else
    {
    double b_sqr=b*b;
    if (b_sqr<4*a*c)
        {
        cout<<"Complex roots ";
        return 0;
        }
    else if (b_sqr==4*a*c)
        {
        x=-b/(2*a);
        cout<<"The only solution is x: "<<x;
        return 0;
        }
    else
        {
            x=-b+(sqrt((b*b)-(4*a*c)))/(2*a);
            x2=-b-(sqrt((b*b)-(4*a*c)))/(2*a);
            cout<<"The first root is x1: "<<x;
            cout<<"The second root is x2: "<<x2;
            return 0;
        }
    }
}
Was it helpful?

Solution

Following may help : http://ideone.com/o8nLlV

bool solve_quadratic(double a, double b, double c, double& x1, double& x2)
{
    assert(a != 0);

    const double delta = b * b - 4 * a * c;
    if (delta < 0) {
        return false;
    }
    if (delta == 0) {
        x1 = -b / (2 * a);
        x2 = x1;
    } else {
        const double sqrt_delta = sqrt(delta);
        x1 = (-b + sqrt_delta) / (2 * a);
        x2 = (-b - sqrt_delta) / (2 * a);
    }
    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top