Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code this using c++ for a quadratic equation solver: The requirements for our so

ID: 3882320 • Letter: C

Question

Code this using c++ for a quadratic equation solver:

The requirements for our solver were updated. We now have to solve the linear solution that can result when a=0, and we have to output imaginary roots as well!

Requirements

Do not use the complex number class.

The program should give all roots including imaginary ones.

If the coefficients constitute a linear equation you should calculate the single root.

Input:

Three coefficients (a, b, and c respectively) on a single line separated by spaces.

Output:

The equation being solved. See Sample Run below for format.

Roots.

Each root on a separate line.

If there is more than one root, then the one obtained by subtraction is first.

Roots should be reported as

x = <number>

x = <number> - <number>i

x = <number> + <number>i

Note that there is a single space ( ) on either side of an '=', a '+' or a '-'.

If no valid solutions can be calculated, then output:

Unable to determine root(s).

Note that endl puts a line return () at the end.

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
   float x, y, z, a1, a2, d, r, im;
    cin >> x >> y >> z;
    d = y*y - 4*x*z;
    if(x==0)
    {
        cout <<"x = "<<-z/y;
    }
    if (d > 0 && x!=0)
       {
        a1 = (-y + sqrt(d)) / (2*x);
        a2 = (-y - sqrt(d)) / (2*x);
        cout << a1 <<" - 0i"<<endl;
        cout << a2 <<" + 0i" << endl;
       }

    else if (d == 0) {
        a1 = (-y) / (2*x);
        cout << a1 << endl;
    }

    else if (d < 0)
    {
        r = -y/(2*x);
        im =sqrt(-d)/(2*x);
        cout << "x = " << r << " - " << im << "i" << endl;
        cout << "x = " << r << " + " << im << "i" << endl;
    }

    return 0;
}