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

In summary: I just wanted to mention this because it can cause a lot of problems in your code if you don't account for it.
  • #1
maiad
102
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
  • #2


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


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


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.
 
  • #5


I would first recommend checking your code for any syntax errors or typos. It is possible that a small mistake in the code is causing it to always return the same result.

Additionally, I would suggest using a debugger or print statements to track the values of your variables throughout the program. This can help you identify where the issue may be occurring and make it easier to troubleshoot.

I would also recommend reviewing the logic of your "if" statements to make sure they are accurately capturing all possible scenarios and returning the correct results. It may be helpful to use a flowchart or pseudocode to visually map out the steps your program should take.

Finally, I would suggest testing your program with different input values to ensure it is working correctly in all cases. If you are still having trouble, it may be helpful to seek assistance from a colleague or online community to get a fresh perspective on the issue.
 

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

Q: What is a quadratic root finder?

A: A quadratic root finder is a program or function that calculates the roots of a quadratic equation in the form of ax^2 + bx + c = 0, where a, b, and c are constants.

Q: How do if statements help in coding a quadratic root finder?

A: If statements are used in coding a quadratic root finder to check for certain conditions and execute specific code depending on the result. For example, if the discriminant is negative, the quadratic equation has no real roots and the program can display an appropriate message.

Q: What is the role of the discriminant in a quadratic root finder?

A: The discriminant, b^2-4ac, helps determine the nature of the roots of a quadratic equation. If the discriminant is positive, the equation has two distinct real roots. If it is zero, the equation has one real root. And if it is negative, the equation has no real roots.

Q: Can a quadratic root finder handle complex or imaginary roots?

A: Yes, a quadratic root finder can handle complex or imaginary roots by using complex numbers in its calculations. This allows for the roots of all quadratic equations to be accurately determined.

Q: Are there any limitations to using if statements in coding a quadratic root finder?

A: While if statements are useful for handling specific conditions, they may become cumbersome and difficult to manage when dealing with multiple conditions. In these cases, using other programming techniques such as loops or switch statements may be more efficient.

Similar threads

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