Hello again. The algorithm I have to write must do exactly what the title describes, it must solve 2nd degree equations until the user wants to stop. For some values x1 and x2 equal to "nan". I assume that's when x1 and x2 are complex. What I must add to make the program also show the complex solutions? Is it a more complicated think with which I shouldn't bother now at the beginning, or it's not a big deal?
#include <iostream>
#include <cmath>
usingnamespace std;
main ()
{
float delta, x1, x2; int a, b, c; //x1, x2 are the solutions, a b c the coeficients
a=1;
while(a!=0) //condition of existence of a 2nd degree ecuation
{
cout<<"\n a=";cin>>a; //user writes the desired coeficients
cout<<"\n b=";cin>>b;
cout<<"\n c=";cin>>c;
if(a!=0) //condition of existence of a 2nd degree ecuation again
{ //solving algorithm of the ecuation
delta=b*b-4*a*c;
x1=(-b+sqrt(delta))/2*a;
x2=(-b-sqrt(delta))/2*a;
cout<<"\n x1="<<x1;
cout<<"\n x2="<<x2;}
else
cout<<"\n that is not a 2nd degree ecuation... process ending";
}
}
Delta is the wrong term, the part inside of the square root of the quadratic formula is called the Discriminant.
if the answer is complex then simply print out: sqrt(-x)
even if the answer is real you should still print out the answers' exact value which will most of the time be
under the form of: sqrt(x)
#include <complex> //New header (it's in standard)
#include <iostream> //We don't even need <cmath> anymore!
usingnamespace std;
int main()
{
complex<float> delta, x1, x2; int a, b, c; //Change the type from float to complex<float>
//
//... Everything else in your code should be the same
//
cout << "\n x1="<< real(x1); // Output the real part
if (imag(x1) != 0) cout << "+ " << imag(x1) << "i"; //Output i if there.
cout << "\n x2="<< real(x2);
if (imag(x2) != 0) cout << "+ " << imag(x2) << "i";
// ...
It only adds 3 2 lines of code to what you had already - The include and the two couts. The rest of the code is essentially the same.
I made the modifications that you recommended and I got two errors:
line 19 error: no match for 'operator+' in '- b + std::sqrt [with _Tp = float](((const std::complex<float>&)((const std::complex<float>*)(& delta))))'|
line 20 error: no match for 'operator-' in '- b - std::sqrt [with _Tp = float](((const std::complex<float>&)((const std::complex<float>*)(& delta))))'|
I really have no idea what's with the + and - signs from lines 19 and 20 between -b and sqrt(delta). I googled it and found no results from which to make connections with my problem. Any suggestions?
i would advise to use this one to avoid complex numbers
you shouldnt bother about the complex ones because youre searching for the xvalues of points where y=0. if delta is lower then 0 you wont have any. so displaying complex numbers is pointless.
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
float delta, x1, x2, a=1, b, c, x; //x1, x2 are the solutions, a b c the coeficients
while(a!=0) //condition of existence of a 2nd degree ecuation
{
cout<<"\n a=";cin>>a; //user writes the desired coeficients
cout<<"\n b=";cin>>b;
cout<<"\n c=";cin>>c;
if(a!=0) //condition of existence of a 2nd degree ecuation again
{ //solving algorithm of the ecuation
delta=b*b-4*a*c;
if (delta<0) { cout << "no answers." << endl;}
elseif(x==0) {x=-b/2*a;cout << "x1=x2= " << x << endl;}
else {
x1=(-b+sqrt(delta))/2*a;
x2=(-b-sqrt(delta))/2*a;
cout<<"\n x1="<<x1;
cout<<"\n x2="<<x2;}}
else
{cout<<"\n that is not a 2nd degree ecuation... process ending";}
}
}