By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ 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
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.     double a != 0;
  10.     double b;
  11.     double c;
  12.     double x;
  13.     double root1;
  14.     double root2;
  15.     double descrim;
  16.  
  17.     cout << "Please input the value of a: " << endl;
  18.     cin >> a;
  19.     cout << "Please input the value of b: " << endl;
  20.     cin >> b;
  21.     cout << "Please input the value of c: " << endl;
  22.     cin >> c;
  23.  
  24.     descrim = pow(b,2) - (4 * a * c);
  25.     root1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);
  26.     root2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);
  27.  
  28.     if descrim = 0
  29.         cout << "Equation has a single (repeated) root" << endl;
  30.     if descrim > 0
  31.         cout << "Equation has two real roots" << endl;
  32.     if descrim < 0
  33.         cout << "Equation has two complex roots" << endl;
  34.     if descrim >= 0
  35.         cout << "The roots of the quadratic equation are: " << endl;
  36.         cin >> root1;
  37.         cin >> root2;
  38.  
  39.     system("pause");
  40.     return 0;
Feb 12 '07 #1
Share this Question
Share on Google+
6 Replies


RRick
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>). 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

Ganon11
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

P: 22
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>). Complex also supports negative sqrts. Instead of using doubles, use complex for your root calculations. For sqrt, use pow with a .5 power value.
yeah, i changed the coding a little bit but i still get a syntax error for descrim. Also, i don't really understand how to set pow with a .5 value. Here are the changes i have made:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <cmath>
  5. #include <complex>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     double a;
  13.     double b;
  14.     double c;
  15.     complex<double> root1;
  16.     complex<double> root2;
  17.     double descrim;
  18.  
  19.     cout << "Please input the value of a: " << endl;
  20.     cin >> a;
  21.     if (a != 0) 
  22.         cout <<"Please input a value other than 0: " << endl;
  23.         cin >> a;
  24.     cout << "Please input the value of b: " << endl;
  25.     cin >> b;
  26.     cout << "Please input the value of c: " << endl;
  27.     cin >> c;
  28.  
  29.     descrim = pow(b,2) - (4 * a * c);
  30.     root1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);
  31.     root2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);
  32.  
  33.     if descrim = 0
  34.         cout << "Equation has a single (repeated) root" << endl;
  35.     if descrim > 0
  36.         cout << "Equation has two real roots" << endl;
  37.     if descrim < 0
  38.         cout << "Equation has two complex roots" << endl;
  39.     if descrim >= 0
  40.         cout << "The roots of the quadratic equation are: " << endl;
  41.         cin >> root1;
  42.         cin >> root2;
  43.  
  44.     system("pause");
  45.     return 0;
  46. }
  47.  
Feb 12 '07 #5

RRick
Expert 100+
P: 463
I assume your errors are at the conditionals for discrim. It looks like you need parentheses around the conditional expressions.
===> if ( discrim > 0 ) ....

Use the complex objects, but be warned, there is a bit of a learning curve to understand them. That said: you can use an int or a double value for the second parameter to the complex version of pow.

Expand|Select|Wrap|Line Numbers
  1. Complex a(9.0), b;
  2. b = pow(a, 2);      //  b = a * a
  3. b = pow(a, .5);      //  b = sqrt( a)
  4.  
Feb 12 '07 #6

P: 22
it works, thanks for the help
Feb 13 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.