[C++] coding a quadratic root finder with if statements

  • C/++/#
  • Thread starter maiad
  • Start date
  • #1
102
0

Main Question or Discussion Point

[C++] coding a quadratic root finder with "if statements"

//This program solves the qudratic equation
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
//Declaring Varibles
double coefA;
double coefB;
double coefC;
double x1;
double x2;
double x3;

//Initialize varibles
x1=0.0;
x2=0.0;
x3=0.0;
coefA=0.0;
coefB=0.0;
coefC=0.0;

//Input of values

cout<<"Enter the value of the coefficent of the second order (x squared) term:";
cin>>coefB;

cout<<"Enter the value of the coefficent of the first order term:";
cin>>coefA;

cout<<"Enter the value of the constant term:";
cin>>coefC;

cout<<"\n\n";

cout<<"The coefficents of the quadratic equation are:\n";

cout<<" Coefficent of the second order term is "<<coefB<<",\n";

cout<<" Coefficent of the first order term is "<<coefA<<",\n";

cout<<" Coefficent of the constant term is "<<coefC<<"\n";

cout<<"\n\n\n";

//Equation body
x1=(-coefB+sqrt(coefB*coefB-4*coefA*coefC))/(2*coefA);

x2=(-coefB-sqrt(coefB*coefB-4*coefA*coefC))/(2*coefA);

x3=-coefC/coefB;

//"If" staements that returns the appropriate number of roots

if(coefA==0&&coefB==0)
{
if(coefC==0&&coefB==0)
{
cout<<"There are infinite number of possible solutions";
}
else
{
cout<<"There are no real roots.";
}
}


else if (fabs(coefA-0)<0.005)
{
cout<<"There is one real root = "<<fixed<<setprecision(2)<<x3;
}

else if((coefB*coefB-4*coefA*coefC)>0.0)
{
cout<<"There are two real roots, root 1 = "<<fixed<<setprecision(2)<<x1<<" root2 = "<<x2;
}

else if(fabs(x1-x2)<0.005)
{
cout<<"There is one double real root, root1 = "<<fixed<<setprecision(2)<<x1;
}

else
{
cout<<"There are no real roots.";
}


return 0;
}



Above is my code so far, i'm not sure why when i enter any value at all, the out put will always be the same giving me "There are no real roots".

I'm assuming there is something wrong with the first "if" statements since before i put that in, it gave me the correct roots. can someone give me some guidance?
 
Last edited:

Answers and Replies

  • #2
11,693
5,266


1) its bad to compare a==0 if a is a float or double as the compiler interprets the 0 as an integer value

2) its bad to compare a=0.0 if a is a float or double due to machine precision

3) better to use: abs(a-0.0)<0.00001 or some equivalent math function that does this foryou

4) its good to put in cout statements each time a variable is assigned a value so you can see where your pgm went wrong as in: cout <<"x="<<x;

so rewrite these lines with the code improvements mentioned above:

if((coefA==0&&coefB==0)<0.0) // what are you trying to do here ???
{
if(coefC==0&&coefB==0)
 
  • #3
102
0


i think that was just a unfinished code that i did before i saved. i reworte it as:
if((fabs(coefA-0.0)<0.00001)&&(abs(coefB-0.0)<0.00001))
{
if((fabs(coefA-0.0)<0.00001)&&(abs(coefB-0.0)<0.00001))
{
cout<<"There are infinite number of possible solutions";
}
else
{
cout<<"There are no real roots.";
}
}
and to seems so far.

But now, when i put in the inputs :coefB=3, coefA=5,coefC=1, it gives me root1= -1#j, root2=-1,#j so i'm asumming something is wrong with :


else if(fabs((coefB*coefB-4*coefA*coefC)-0.0)>0.00001)
{
cout<<"There are two real roots, root 1 = "<<fixed<<setprecision(2)<<x1<<" root2 = "<<x2;
}

but dosent the the value it computes not satsify:
fabs((coefB*coefB-4*coefA*coefC)-0.0)>0.00001?
 
  • #4
chiro
Science Advisor
4,790
131


Just as a side note, you will probably want to guarantee that the coeffecients of the quadratic are not zero or close enough to zero to cause problems, and you can do this by having a simple loop in the input stage that checks this condition and asks you to enter it in again if it doesn't meet the criteria.
 

Related Threads on [C++] coding a quadratic root finder with if statements

  • Last Post
Replies
0
Views
2K
Replies
11
Views
1K
Replies
19
Views
1K
Replies
2
Views
22K
Replies
17
Views
2K
  • Last Post
Replies
10
Views
303
Replies
18
Views
8K
Replies
11
Views
4K
Replies
11
Views
2K
Replies
20
Views
3K
Top