helppp

How do I make a program that solves for the roots of a Quadratic Equation. It should be able to accept decimal and able to print even the imaginary root? I really need your help on the imaginary part... Please....
I haven't tested this, but I think it would work.. It works!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <complex>

// inputs a, b, c
// output: root[2]
void QuadraticEqu(const double a, const double b, const double c,
                             std::complex<double> root[2])
{
    std::complex<double> discriminant(b*b - 4*a*c , 0.0);

    root[0] = (-b + sqrt(discriminant)) / (2*a);
    root[1] = (-b - sqrt(discriminant)) / (2*a);
}

int main()
{
    std::complex<double> roots[2];

    QuadraticEqu(1, -4, 8, roots);

    std::cout << roots[0] << '\t' << roots[1] << std::endl;
}
(2,2)   (2,-2)
Last edited on
Thanks Mr Stewbond.. IT surely will be a great help...
Topic archived. No new replies allowed.