Advanced Quadratic Solver CSc 1253: Programming Project # 2 Solving Quadratic Eq
ID: 3628632 • Letter: A
Question
Advanced Quadratic Solver CSc 1253: Programming Project # 2Solving Quadratic Equations
Out: 9/9
Due: 9/17 by 11:50 PM
Learning Objectives
• Using Conditional Statements
• Using Logical Connectors
• More on Writing Interactive Programs
Definition 1. A Quadratic Equation is a second-order polynomial equa-
tion in a single variable x
ax2 + bx + c = 0 (1)
with a 6= 0. a is referred to as the coefficient of the quadratic term, b, the
coefficient of the linear term, and c, the constant term. Because a quadratic
equation is a second-order polynomial equation, the fundamental theorem of
algebra guarantees that it has two solutions. These solutions may be both
real, or both complex.
Definition 2. The quantity b2 - 4ac is called the discriminant of a
quadratic equation.
Since we have covered decision statements, we will consider quadratic equa-
tions whose roots are real or complex numbers.
Duncan 1 Fall 2011
Advanced Quadratic Solver CSc 1253: Programming Project # 2
For any quadratic equation, its roots may fall into one of these categories:
1. The discriminant is 0:
x1 = -b
2a
(2)
x2 = -b
2a
(3)
2. The discriminant, D, is positive:
x1 = -b + vD
2a
(4)
x2 = -b -
vD
2a
(5)
3. The discriminant, D, is negative:
x1 = u + vi (6)
x2 = u - vi (7)
where, u = -b
2a and v =
v|D|
|2a| .
Observe that when the discriminant is a negative number, the roots of the
quadratic equation are complex numbers. When the discriminant is 0, the
roots of the quadratic equation are identical and real. When the discriminant
is a positive number, the roots of the quadratic equation are distinct real
numbers.
Write a C++ program that prompts the user for the coefficient of the
quadratic term, the coefficient of the linear term, and the constant term of
a quadratic equation. Your program should then compute and display the
roots of the quadratic equation. All numeric values in the output should
be displayed with five decimal places and the input and output MUST be
formatted as below. Name your source file quadraticsolver2.cpp.
Duncan 2 Fall 2011
Advanced Quadratic Solver CSc 1253: Programming Project # 2
Your program interactions should appear as shown in the sample run. Be
sure to format the output of your program exactly as shown below.
Sample Run 1:
Enter the coefficient of the quadratic term>2.5
Enter the coefficient of the linear term>5
Enter the constant term>2.5
The solution to the equation 2.50000x^2 + 5.00000x + 2.50000 = 0.00000
is {-1.00000, -1.00000}.
Sample Run 2:
Enter the coefficient of the quadratic term>2.2
Enter the coefficient of the linear term>3.4
Enter the constant term>1.2
The solution to the equation 2.20000x^2 + 3.40000x + 1.20000 = 0.00000
is {-0.54545, -1.00000}.
Sample Run 3:
Enter the coefficient of the quadratic term>-4
Enter the coefficient of the linear term>-2
Enter the constant term>7
The solution to the equation -4.00000x^2 - 2.00000x + 7.00000 = 0.00000
is {-1.59629, 1.09629}.
Sample Run 4:
Enter the coefficient of the quadratic term>6
Enter the coefficient of the linear term>-3
Enter the constant term>7
The solution to the equation 6.00000x^2 - 3.00000x + 7.00000 = 0.00000
is {0.25000 + 1.05079i, 0.25000 - 1.05079i}.
Here is what I have written so far:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double quadCoef,linCoef,constant;
cout << "Enter the coefficent of the quadratic term>";
cin >> quadCoef;
cout << "Enter the coefficient of the linear term>";
cin >> linCoef;
cout << "Enter the constant term>";
cin >> constant;
double root_1, root_2, discriminant = pow(linCoef,2)-4*quadCoef*constant;
if (quadCoef = 0)
{
cout << " This is not a quadratic equation" << endl;
}
if (discriminant >=0)
{
if (discriminant = 0)
{
root_1 = (-linCoef/(2*quadCoef));
root_2 = (-linCoef/(2*quadCoef));
cout<<fixed<<setprecision(5)<<"The solution of the equation "<<quadCoef<<"x^2";
cout<<fixed<<setprecision(5)<<linCoef<<"x"<<constant<<"=0.00000"<<endl;
cout<<fixed<<setprecision(5)<<"is {"<<root_1<<", "<<root_2<<"}."<<endl;
}
else
{
double root_1 = (-linCoef+sqrt(pow(linCoef,2)-4*quadCoef*constant))/(2*quadCoef);
double root_2 = (-linCoef-sqrt(pow(linCoef,2)-4*quadCoef*constant))/(2*quadCoef);
cout <<fixed<<setprecision(5)<< "The solution of the equation " << quadCoef << "x^2 + ";
cout <<fixed<<setprecision(5)<<linCoef<< "x +" << constant << "=0.00000" << endl;
}
}
else
{
double u = (-linCoef/(2*quadCoef));
double v = (sqrt(fabs(discriminant))/fabs(2*quadCoef));
double root_1 = u+v;
double root_2 = u-v;
cout<<fixed<<setprecision(5)<<"The solution of the equation "<<quadCoef<<"x^2";
cout<<fixed<<setprecision(5)<<linCoef<<"x"<<constant<<"=0.00000"<<endl;
cout<<fixed<<setprecision(5)<<"is {"<<root_1<<", "<<root_2<<"}."<<endl;
}
return 0;
}
I seem to have no syntax errors form when I compiled, however the results I'm getting are incorrect. Please help me out.
Explanation / Answer
#include #include #include using namespace std; int main() { double quadCoef,linCoef,constant; double root_1, root_2, discriminant,u,v; cout > quadCoef; cout linCoef; cout constant; discriminant = pow(linCoef,2)-(4 * quadCoef * constant); coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.