-2

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;
}
user507879
  • 13
  • 2
  • please indent your code with 4 spaces to make it look like code. – John Smith Nov 15 '10 at 05:45
  • please read the editing and posting guidelines. this code is nearly impossible to read! – Matt Phillips Nov 15 '10 at 05:46
  • 2
    Proper code formatting isn't one of those sissy little uptight things only computer science professors care about. It's an extremely important part of writing code that other people can read. Much as this may surprise you, the goal of programming is not to write code the computer can understand, the goal is to write code that makes sense to other people (including yourself) that the computer happens to also be able to execute to get the results you want. – Omnifarious Nov 15 '10 at 05:55
  • 1
    I redid the indention (similar to the way that an auto indenter would, i.e. not caring about intent but rather the syntactic level) in such a way that the problem should be immediately obvious. (I didn't change any other formatting, and the indent was difficult enough because of the error some things are syntactically meaningless). – Reese Moore Nov 15 '10 at 05:58
  • @user507879: If you have a [question about code](http://www.catb.org/~esr/faqs/smart-questions.html#code), don't dump it all and say "it doesn't work". Write a [minimal test case](http://sscce.org/), tell us what you expect the code to do and what it actually does, including any error messages. Read Jon Skeet's blog post on [writing the perfect question](http://tinyurl.com/so-hints). – outis Nov 15 '10 at 05:58
  • And it really is important that if nothing else you are at least constant in your code. There is no reason to use `endl` in some places and `\n` in others. And had you used `{}` everywhere the problem would have been averted. Consistency and Readability are key. – Reese Moore Nov 15 '10 at 05:59
  • @user507879: Note that you can easily indent/unindent lines by selecting them in the SO editor and clicking the "101\n010" button. Click the orange question mark in the editor toolbar for more information and tips on formatting. Don't neglect to thank the people below if their answers are helpful by up-voting them. If one of their answers is definitive, [accept it](http://stackoverflow.com/faq#howtoask). – outis Nov 15 '10 at 06:00

3 Answers3

4

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

vinothkr
  • 1,190
  • 10
  • 23
  • 3
    And the moral of the story ... **properly format your code** (and it also helps to keep a consistent style -- I prefer to always use braces with conditionals, for instance) –  Nov 15 '10 at 05:51
0

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
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
0
#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;

}

Jason Rogers
  • 18,658
  • 25
  • 74
  • 110
  • Thank you. But after a few modifications, when I run the code I get a wrong answer. For example when I input a=1 and b=2, c=2, I should get the answers; -1+i and -1-i, but when I run my code, it gives me 0.73205+i and -2.73205 – user507879 Nov 16 '10 at 03:51
  • disc2 = pow(b,2.0)-4*a*c; disc2 = sqrt(disc2); unless you have written your own sqrt function you'll have a pb here I think, you can't do a sqrt of a negative number you have to make it absolute or *-1. – Jason Rogers Nov 16 '10 at 05:23