/***************************************************
Implementing the Quadratic Formula
Write a program that reads in the coeeficients of
a quadratic polynomial, and prints out its roots.
The quadratic formula says that the roots of a
polynomial a x^2 + b x + c are:
____________ ____________
-b - \/ b^2 - 4 a c -b + \/ b^2 - 4 a c
x = ------------------- , -------------------
2 a 2 a
Basic C++ does not have a "square root" function
so you have to use one of the "Standard Libraries"
to get it. We've been using one such library,
"iostream", for i/o. For mathematical functions
we use "cmath". So "include" cmath, and you can
use the "sqrt" function, which takes an object of
type double and returns an object of type double,
namely the squareroot of what you gave it.
***************************************************/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// read in the coefficients of a x^2 + b x + c
double a,b,c;
cout << "Enter coefficient a of x^2 + b x + c: ";
cin >> a;
cout << "Enter coefficient b of x^2 + b x + c: ";
cin >> b;
cout << "Enter coefficient c of x^2 + b x + c: ";
cin >> c;
// compute the common subexpression with squareroot
double d;
d = sqrt(b*b - 4.0*a*c);
// compute x1 and x2, the two roots
double x1,x2;
x1 = (-b - d)/(2.0*a);
x2 = (-b + d)/(2.0*a);
// write out the roots
cout << "The roots of the polynomial are "
<< x1 << " and " << x2 << "." << endl;
return 0;
}