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

AI Thread Summary
The discussion focuses on coding a quadratic root finder in C++ using if statements. The main issue raised is that the program consistently outputs "There are no real roots" regardless of the input values. Participants highlight the importance of avoiding direct comparisons of floating-point numbers with zero due to precision issues, suggesting the use of functions like fabs to handle comparisons instead. A specific code snippet is revised to improve accuracy in root calculations, and it is noted that additional input validation for coefficients is recommended to prevent erroneous results. The conversation emphasizes debugging techniques and ensuring proper handling of edge cases in quadratic equations.
maiad
Messages
101
Reaction score
0
[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:
Technology news on Phys.org


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)
 


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?
 


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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
3K
Replies
35
Views
4K
Replies
36
Views
3K
Replies
39
Views
4K
Replies
17
Views
2K
Replies
1
Views
3K
Replies
23
Views
2K
Back
Top