/// GH solving PPPe-18

#include "..\std_lib_facilities.h"
#include <cstdlib> ///for random numbers
#include <ctime> ///for time()
/****************
Write a program to solve quadratic equations.
A quadratic equation is of the form
    ax^2 + bx + c = 0
If you don’t know the quadratic formula for solving such an
expression, do some research.  Remember, researching how to
solve a problem is often necessary before a programmer can
teach the computer how to solve it. Use doubles for the user
inputs for a, b, and c. Since there are two solutions to a
quadratic equation, output both x1 and x2.

Also: build your own square root.
********************/

double squareRoot(double);


int main() {
    double a,b,c,disc,h,k;
    cout << "\nEnter a, b and c to solve ax^2+bx+c=0: ";
    while(cin >> a >> b >> c) {
        disc = b*b - 4*a*c;
        if(squareRoot(disc)!=-1) {
            h = -b/(2*a);
            k = squareRoot(disc)/(2*a);
            cout << "\nThe solutions are  "
                 << setprecision(12) << h+k << " and " << h-k;
        }
        cout << "\nEnter a, b and c to solve ax^2+bx+c=0: ";
    }
}

double squareRoot(double A) {
    if(A<0) {
        cout << "\nCan't do imaginary roots.";
        return -1;
    }
    double x_next = A/2, x = A;
    while(abs(x-x_next)>1e-8) {
        x = x_next;
        x_next = (x + A/x)/2;
    }
    return x_next;
}
