1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
/*
Objective: To find and check the real roots of a quadratic Equation.
*/
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double input(double& a, double& b, double& c);
void checkRoots(double a, double b, double c, bool roots);
void findRootsAndPrint(double a, double b, double c);
int main ()
{
double a,b,c,d;
int roots;
input(a,b,c);
checkRoots(a,b,c,roots);
return 0;
}
double input(double& a, double& b, double& c)
{
cout << "Enter the numbers to be respresented in the equation below.\n";
cout << "a=";
cin >> a;
cout << "\nb=";
cin >> b;
cout << "\nc=";
cin >> c;
if (a==0)
{
cout << "I'm Sorry 'a' cannot be = to 0.\n";
return 0;
}
else
return 0;
}
void checkRoots(double a, double b, double c, bool roots)
{
roots=(b*b)-(4*a*c);
switch (roots)
{
case 0:
{
cout << "There is one real root for the equation."<< endl << endl;
findRootsAndPrint(a,b,c);
break;
}
case 1:
{
cout << "There are two real roots for the equation."<< endl << endl;
findRootsAndPrint(a,b,c);
break;
}
default:
{
cout << "The Quadratic Equation with the variables of a="
<< a << ", b=" << b << ", c=" << c << " have no real roots, only imaginary."<< endl;
system("pause");
break;
}
}
}
void findRootsAndPrint(double a, double b, double c)
{
double first, second, d, det;
d=(b*b)-(4*a*c);
det=sqrt(d);
if (d==0)
{
first=(-b/(2*a))+(det/(2*a));
cout << "The Root of the equation " << a << "(x*x) - " << b << "x + "
<< c << " is: " << first << endl << endl;
}
else
{
first=(-b/(2*a))+(det/(2*a));
second=(-b/(2*a))-(det/(2*a));
cout << "The Roots of the equation " << a << "(x*x) - " << b << "x + "
<< c << " are...\n";
cout << "First Root: " << first << endl;
cout << "Second Root: " << second << endl << endl;
}
system("pause");
return;
}
|