// Program Description:
// This program solves quadratic equations to find their roots. This
// program takes values of a, b, and c as input and outputs the root(s).
// The user can repeat the calculation for as many equations as they like.
My else if and else statements both work the way I want them too, but I need my if tatement to do negative numbers. When I enter the values for a, b, c as
1, 4, a nd 7 respectfully I need the answer to be -2.0000 - 1.7321 i and -2.0000 + 1.7321 i. I am just not sure how to do this, so if anyone has any suggestions.
// This program solves quadratic equations to find their roots. This
// program takes values of a, b, and c as input and outputs the root(s).
// The user can repeat the calculation for as many equations as they like.
Code:
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;
int main()
{
char cAgain;
int iDataset = 1;
double dRoot1, dRoot2, dValuea, dValueb, dValuec, dDescriminant;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);
do
{
cout << endl << "****************" << endl
<< "* Data Set " << iDataset << " *" << endl
<< "****************" << endl << endl;
cout << endl << "Please enter coefficients for quadratic equation"
<< " ax^2 + bx + c = 0" << endl;
cout << endl << "Value of 'a' : ";
cin >> dValuea;
cout << "Value of 'b' : ";
cin >> dValueb;
cout << "Value of 'c' : ";
cin >> dValuec;
dRoot1 = (- dValueb + sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
dRoot2 = (- dValueb - sqrt(pow(dValueb, 2) - 4 * dValuea * dValuec)) / 2 * dValuea;
dDescriminant = pow(dValueb, 2) - 4 * dValuea * dValuec;
if(dDescriminant < 0)
{
cout << endl << "You have entered the equation " << dValuea
<< "x^2 + " << dValueb << "x +" << dValuec
<< " = o" << endl;
cout << "The root to this equation is: " << endl << endl
<< dRoot1 << " i and" << endl << dRoot2 << endl << endl;
}
else if(dDescriminant == 0)
{
cout << endl << "You have entered the equation " << dValuea
<< "x^2 + " << dValueb << "x +" << dValuec
<< " = 0" << endl;
cout << "The root to this equation is: " << endl << endl
<< dRoot1 << endl;
}
else
{
cout << endl << "You have entered the equations " << dValuea
<< "x^2 + " << dValueb << "x +" << dValuec
<< " = o" <<endl;
cout << "The root to this equations is: " << endl << endl
<< dRoot1 << " and" << endl << endl << dRoot2 << endl;
}
cout << endl << "Want to do this again? Y/y/N/n" << endl << endl;
cin >> cAgain;
iDataset++;
}while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
1, 4, a nd 7 respectfully I need the answer to be -2.0000 - 1.7321 i and -2.0000 + 1.7321 i. I am just not sure how to do this, so if anyone has any suggestions.
Comment