quadratic equation program | Newbie | | Join Date: Feb 2007 Location: Michigan
Posts: 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: -
#include <iostream>
-
#include <cmath>
-
-
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;
|  | Expert | | Join Date: Feb 2007
Posts: 430
| | | re: quadratic equation program
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.
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: quadratic equation program
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).
| | Newbie | | Join Date: Feb 2007 Location: Michigan
Posts: 22
| | | re: quadratic equation program
thanks for the help guys, its greatly appreciated. If i have any more problems with the program i'll be sure to post.
| | Newbie | | Join Date: Feb 2007 Location: Michigan
Posts: 22
| | | re: quadratic equation program Quote:
Originally Posted by RRick 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: -
-
#include "stdafx.h"
-
#include <iostream>
-
#include <cmath>
-
#include <complex>
-
-
using namespace std;
-
-
-
int _tmain(int argc, _TCHAR* argv[])
-
{
-
double a;
-
double b;
-
double c;
-
complex<double> root1;
-
complex<double> root2;
-
double descrim;
-
-
cout << "Please input the value of a: " << endl;
-
cin >> a;
-
if (a != 0)
-
cout <<"Please input a value other than 0: " << 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;
-
}
-
|  | Expert | | Join Date: Feb 2007
Posts: 430
| | | re: quadratic equation program
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. -
Complex a(9.0), b;
-
b = pow(a, 2); // b = a * a
-
b = pow(a, .5); // b = sqrt( a)
-
| | Newbie | | Join Date: Feb 2007 Location: Michigan
Posts: 22
| | | re: quadratic equation program
it works, thanks for the help
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,418 network members.
|