431,780 Members | 1,520 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

# quadratic equation program

 P: 22 Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a cannot equal 0 are given by the following formula -b + or - square root of (b^2 - 4ac) / 2a. If b^2 - 4ac = 0 then equation has a single root. if b^2 - 4ac > 0 then equation has two real roots. if b^2 - 4ac < 0 then equation has two complex roots. Write a program that prompts the user to input the value of a,b,c, and outputs the type of roots of the equation. Furthermore, if b^2 - 4ac >= 0 the program should output the roots of the quadratic equation. This is the coding i have so far. I am getting a few errors: Expand|Select|Wrap|Line Numbers #include  #include    using namespace std;     int _tmain(int argc, _TCHAR* argv[]) {     double a != 0;     double b;     double c;     double x;     double root1;     double root2;     double descrim;       cout << "Please input the value of a: " << endl;     cin >> a;     cout << "Please input the value of b: " << endl;     cin >> b;     cout << "Please input the value of c: " << endl;     cin >> c;       descrim = pow(b,2) - (4 * a * c);     root1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);     root2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);       if descrim = 0         cout << "Equation has a single (repeated) root" << endl;     if descrim > 0         cout << "Equation has two real roots" << endl;     if descrim < 0         cout << "Equation has two complex roots" << endl;     if descrim >= 0         cout << "The roots of the quadratic equation are: " << endl;         cin >> root1;         cin >> root2;       system("pause");     return 0; Feb 12 '07 #1
6 Replies

 Expert 100+ P: 463 If this doesn't compile, try displaying the errors. I doubt the compiler will like double a!= 0; Add some logic and a error message for this after the user inputs the 'a' value. Your ideas are sound, but the root values are missing a sqrt call. It looks like the logic for read and imaginary solutions (the couts at the bottom) are reversed. If you want use doubles for the roots, you'll have to add some logic for imaginary roots. Sqrt does not like negative values. Remember, a complex root has two parts: real and imaginary. There is a better solution. C++ now has a complex object (). Complex also supports negative sqrts. Instead of using doubles, use complex for your root calculations. For sqrt, use pow with a .5 power value. Feb 12 '07 #2

 Expert 2.5K+ P: 3,652 I'd like to add a few points: You declare double x, but never use it - you can safely rid yourself of it. Next, when finding root1 and root2, you explicitly type pow(b,2) - (4 * a * c), but you already have this value scored in descrim - why not use descrim in your calculations? Finally, to avoid finding the square root of negative numbers, once you have the descrim you can use an if...statement to make sure you only find the roots if descrim >= 0. If not, you can display "2 complex roots" and end the program. If it is >= 0, you can determine if it is exactly 0 (and if so, print 1 root) or if it is positive (and if so, print both roots). Feb 12 '07 #3

 P: 22 thanks for the help guys, its greatly appreciated. If i have any more problems with the program i'll be sure to post. Feb 12 '07 #4 