Write a C++ program to find the root(s) of a quadratic equation or a linear equa
ID: 3621436 • Letter: W
Question
Write a C++ program to find the root(s) of a quadratic equation or a linear equation. Use the form: ax2 + bx + c = 0. a = 0 will indicate a linear equation with one root. a, b, and c should be float(double) values. All values, including the roots, should be printed with two decimal places.
Use a while loop to enter sets of coefficients from a file. Use -9999 as your sentinel to stop reading from the file. The name of the file should be entered from the keyboard in the main function. Output should go to the screen. The following functions should be called from your main program:
Use the form ax^2 + (b)x + (c) = 0 to display the equation entered. When a = b = 0 the display the error message: "No linear or quadratic equation is formed. When roots exist, but are not real, print the message "Roots are not real."
This is the input File need to be create in .txt format
input file:
1 2 3
5 6 1
0 3 6
-9999
This is the finish program, it should look like this
COMPUTE THE REAL ROOT(S) OF A QUADRATIC OR A LINEAR EQUATION
Enter the name of the input file e:in5.txt
Equation 1.00x^2 + (2.00)x + (3.00) = 0
Roots are not real.
Equation 5.00x^2 + (6.00)x + (1.00) = 0
Roots are -0.20 and -1.00.
Equation 0.00x^2 + (3.00)x + (6.00) = 0
Root is -2.00.
Explanation / Answer
please rate - thanks
#include<iostream>
#include<cmath>
#include <fstream>
using namespace std;
int DetermineRootTypes(double,double,double);
int main()
{double a,b,c;
int type;
char filename[80];
double discriminant,denom;
ifstream in;
cout.setf(ios::showpos);
cout.setf(ios::fixed);
cout.precision(2);
cout<<"COMPUTE THE REAL ROOT(S) OF A QUADRATIC OR A LINEAR EQUATION ";
cout<<"Enter the name of the input file: ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
in>>a;
while(a!=-9999)
{in>>b>>c;
cout<<" Equation"<<a<<"x^2+("<<b<<")x+("<<c<<") =0 ";
if(a==0)
if(b==0)
cout<<"No linear or quadratic equation is formed. ";
else
cout<<"Root is "<<-c/b<<". ";
else
{
type=DetermineRootTypes(a,b,c);
denom=2.*a;
discriminant=pow(b,2.)-4.*a*c;
if(type==1)
cout<<"Roots are "<<(-b+sqrt(discriminant))/denom<<" and "<<
(-b-sqrt(discriminant))/denom<<". ";
else if(type==2)
cout<<"Root is "<<(-b+sqrt(discriminant))/denom<<". ";
else if(type==3)
cout<<"Roots are not real. ";
}
in>>a;
}
system("pause");
return 0;
}
int DetermineRootTypes(double a,double b,double c)
{double discriminant;
discriminant=pow(b,2.)-4*a*c;
if(discriminant>0)
return 1;
else if(discriminant<0)
return 3;
else
return 2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.