second degree ecuation solver program

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
using namespace 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";
}
}
 

what do you exactly mean with 'complex solutions'?
solutions that are complex numbers, you know, the next type of numbers after real numbers, are the complex numbers
Flashdrive: 16 = x^2 would have:

4 and -4 as the answers to x, but you can't get a negative answer from a sqrt, so you would use 4 and 4i.

Last edited on
So, I'll try to do it with a i constant and an if(delta<0) condition.
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)
How about using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <complex> //New header (it's in standard)
#include <iostream> //We don't even need <cmath> anymore!
using namespace 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.

Relevant references:
http://cplusplus.com/reference/std/complex/
http://cplusplus.com/reference/std/complex/complex/complex/
http://cplusplus.com/reference/std/complex/sqrt/
http://cplusplus.com/reference/std/complex/real/
http://cplusplus.com/reference/std/complex/imag/

EDIT: I made some corrections as I was learning to use this class and these functions.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
using namespace 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;}
   else if(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";}
}

}
 

Ok, I'll "keep it real" and leave it this way. :) Thank you for your help.
Topic archived. No new replies allowed.