-1

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;
        }
    }
}
user1584421
  • 3,499
  • 11
  • 46
  • 86
  • 4
    _' I cannot understand whats wrong in my logic'_ Did you try debugging already? – πάντα ῥεῖ Apr 13 '14 at 13:41
  • 3
    Hi. Asking people to spot errors in your code is not especially productive. You should use the debugger (or add print statements) to isolate the problem, by tracing the progress of your program, and comparing it to what you expect to happen. As soon as the two diverge, then you've found your problem. (And then if necessary, you should construct a [minimal test-case](http://stackoverflow.com/help/mcve).) – Oliver Charlesworth Apr 13 '14 at 13:41
  • 1
    Check through the code carefully - you have several very simple but obvious mistakes. – Paul R Apr 13 '14 at 13:42
  • 2
    (2*2) in the denominator ought to be (2*a) for a start; it should also divide the entire expression, including b: http://mathworld.wolfram.com/QuadraticEquation.html – duffymo Apr 13 '14 at 13:44
  • Are you sure you're not sqrt-ing(negative_value) ? – Marco A. Apr 13 '14 at 13:46
  • 2
    `4*b*c` should be `4 * a * c`. – Jarod42 Apr 13 '14 at 14:10

1 Answers1

2

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302