Question

I've made a few changes here but I'm still not getting what I expect to get. For example, when I substitute a for 1, b for 2 and c for 2, I should get -1+i and -1-i but when I run code it gives me -0.73205+i and - 2.73205+i. How do I fix this?

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

int main()
{
    double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2;
    char i;
    cout<<"Enter a, b and c ";
    cin >> a >> b >> c ;


    if(disc == 0.0 && b == 0.0)
        cout<<"The equation is degenerate and has no real roots. \n";
    else if(a == 0.0)
        cout<<"The equation has one real root x = "<< -c/b <<endl;

    else
    {
        disc =  pow(b,2.0)-4*a*c;
        if (disc > 0.0)
        {
            disc = sqrt(disc);
            root1 = (-b+disc)/(2*a);
            root2 = (-b-disc)/(2*a);
            cout<<"The two real roots are "<<root1<<" and "<<root2<<endl;
        }

        else if(disc < 0.0)
        disc =  pow(b,2.0)+4*a*c;
        disc = sqrt(disc);
        imrt1 = (-b+disc)/(2*a);
        imrt2 = (-b-disc)/(2*a);
        cout<<"The two imaginary roots are "<<imrt1<<"+i"<<" and <<imrt2<<"+i"<<"\n";

        else
            cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
    }//End of compound statement for the outer else

    system("PAUSE");
    return 0;
}
Was it helpful?

Solution

At the first test for if (disc == 0.0 && b == 0.0), the variable disc is uninitialized and hence could have any value whatsoever. You probably intended to write if (a == 0.0 && b == 0.0)...


Your mathematics in the imaginary case is more than a little suspect. If the discriminant is negative, then you need the real part of '-b / 2a' and the imaginary parts are '±√(b² - 4ac)/2a'. So, maybe you need:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double a, b, c;

    cout << "Enter a, b and c: ";
    cin  >> a >> b >> c;

    if (a == 0.0 && b == 0.0)
        cout << "The equation is degenerate and has no real roots.\n";
    else if (a == 0.0)
        cout << "The equation has one real root x = " <<  -c/b  << endl;
    else
    {
        double disc = pow(b,2.0)-4*a*c;
        if (disc > 0.0)
        {
            disc = sqrt(disc);
            double root1 = (-b+disc)/(2*a);
            double root2 = (-b-disc)/(2*a);
            cout << "The two real roots are " << root1 << " and " << root2 << endl;
        }
        else if (disc < 0.0)
        {
            double imag = sqrt(-disc)/(2*a);
            double real = (-b)/(2*a);
            cout << "The two complex roots are "
                 << "(" << real << "+" << imag << "i)" << " and "
                 << "(" << real << "-" << imag << "i)" << endl;
        }
        else
            cout << "Both roots are equal to " << -b/(2*a) << endl;
    }
    return 0;
}

Sample outputs:

Enter a, b and c: 2 6 3
The two real roots are -0.633975 and -2.36603

Enter a, b and c: 2 4 3
The two complex roots are (-1+0.707107i) and (-1-0.707107i)

Enter a, b and c: 3 6 3
Both roots are equal to -1

OTHER TIPS

You missed braces from else if(disc <0.0) hence the next else is orphaned

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

int main(){
    double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2, disc2;
    char i;
    cout<<"Enter a, b and c ";
    cin >> a >> b >> c ;


    if(disc == 0.0 && b == 0.0)
        cout<<"The equation is degenerate and has no real roots. \n";
    else if(a == 0.0)
        cout<<"The equation has one real root x = "<< -c/b <<endl;

    else
    {
        disc =  pow(b,2.0)-4*a*c;
        if (disc > 0.0)
        {
            disc = sqrt(disc);
            root1 = (-b+disc)/(2*a);
            root2 = (-b-disc)/(2*a);
            cout<<"The two real roots are "<<root1<<" and "<<root2<<endl;
        }

        else if(disc < 0.0)
            disc2 =  pow(b,2.0)-4*a*c;
        disc2 = sqrt(disc2);
        imrt1 = (-b+disc2)/(2*a);
        imrt2 = (-b-disc2)/(2*a);
        cout<<"The two imaginary roots are "<<"i"<<imrt1<<" and "<<"i"<<imrt2<<"\n";

        else
            cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
    }//End of compound statement for the outer else

    system("PAUSE");
    return 0;
}

here is your code correctly indented (30 sec under notepad++)

and it becomes obvious (as posted by vinothkr) that your missing the braces

else if(disc < 0.0)

Also Disc in the first test isn't init ...

And I would also recommend to always use {} with if else. Even if it saves 5 secs to not write it, you lose 1/2h debugging any changes.

in comparison

int main(){
double a, b, c, disc, x1, x2, root1, root2, imrt1, imrt2, disc2;
char i;
cout<<"Enter a, b and c ";
cin >> a >> b >> c ;

    //Disc never init...
if(disc == 0.0 && b == 0.0){
    cout<<"The equation is degenerate and has no real roots. \n";
}else if(a == 0.0){
    cout<<"The equation has one real root x = "<< -c/b <<endl;

}else{
    disc =  pow(b,2.0)-4*a*c;
    if (disc > 0.0){
        disc = sqrt(disc);
        root1 = (-b+disc)/(2*a);
        root2 = (-b-disc)/(2*a);
        cout<<"The two real roots are "<<root1<<" and "<<root2<<endl;
    }else if(disc < 0.0){
        disc2 =  pow(b,2.0)-4*a*c;
        disc2 = sqrt(disc2);
        imrt1 = (-b+disc2)/(2*a);
        imrt2 = (-b-disc2)/(2*a);
        cout<<"The two imaginary roots are "<<"i"<<imrt1<<" and "<<"i"<<imrt2<<"\n";
    }else{
        cout<<"Both roots are equal to "<<-b/(2*a)<<endl;
    }
}//End of compound statement for the outer else

system("PAUSE");
return 0;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top