Task 1 Write a function, quadEquation() which takes five arguments, each of type
ID: 3798071 • Letter: T
Question
Task 1
Write a function, quadEquation() which takes five arguments, each of type double. The first three parameters: a, b, and c, represent the coefficients in the equation ax2 + bx + c = 0. The last two parameters, r1 and r2, are passed by reference. They are the roots (solutions) to the quadratic equation. Recall that the quadratic equation is as follows, where r represents roots (solutions): You must create appropriate exception classes for the following: • Divide by zero • Square root of negative number • Only one (repeated) root
Task 2
Write specifications for the above function in comments. Write these comments above the function definition and make sure to include: • Pre-conditions • Post-conditions • The exceptions that could be thrown, and what they mean
Task 3
Write a main function that calls the quadEquation() function with a try statement. You should also write a corresponding catch statement for the aforementioned exceptions and print out an appropriate response.
#include
#include
using namespace std;
void quadEquation(double a, double b, double c, double& r1, double& r2);
int main() {
double a1, b1, c1; double r1; double r2;
cout << "Enter the value of a as in the function ax^2 + bx + c = 0:";
cin >> a1;
cout << "Enter the value of b as in the function ax^2 + bx + c = 0:";
cin >> b1;
cout << "Enter the value of c as in the function ax^2 + bx + c = 0:";
cin >> c1;
quadEquation(a1, b1, c1, r1, r2);
try {
if (a1 == 0) {
throw 0;
}
if (((b1*b1) - 4 * (a1)*(c1)) < 0) {
throw 0.0;
}
if (((b1*b1) - 4 * (a1)*(c1)) == 0) {
throw 'A';
}
quadEquation(a1, b1, c1, r1, r2);
cout << "The first root answer is: " << r1 << endl;
cout << "The second root answer is: " << r2 << endl;
}
catch (int x) {
cout << "Error-can't devide by zero" << endl;
}
catch (double m) {
cout << "Error--can't have a negative root" << endl;
}
catch (char z) {
cout << "Error--can't have a repeated root" << endl;
}
return 0;
}
void quadEquation(double a, double b, double c, double& r1, double& r2)
{
r1 = ((-b - sqrt((b * b) - 4 * (a)*(c))) / (2 * a));
r2 = ((-b + sqrt((b * b) - 4 * (a)*(c))) / (2 * a));
}
My code is working perfectly fine, I just do not know how to code it using "classes" and inlcuding library as our instructor wants.
Example of syntax..... class <class name> : public exception
I also need help identifying:
Pre-conditions
Post-conditions
The exceptions that could be thrown, and what they mean.
Any help would be appreciated.
Thanks in advance.
Explanation / Answer
code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
class Quadequation{
double a,b,c;//coefficient
Quadequation()
{
a=1;b=1;c=1;
}
Quadequation(double a1,double b1,double c1)
{
a=a1;b=b1;c=c1;
}
//
void find_roots(double a, double b, double c, double& r1, double& r2)
{
r1 = ((-b - sqrt((b * b) - 4 * (a)*(c))) / (2 * a));
r2 = ((-b + sqrt((b * b) - 4 * (a)*(c))) / (2 * a));
}
}
int main () {
double a1, b1, c1; double r1; double r2;
cout << "Enter the value of a as in the function ax^2 + bx + c = 0:";
cin >> a1;
cout << "Enter the value of b as in the function ax^2 + bx + c = 0:";
cin >> b1;
cout << "Enter the value of c as in the function ax^2 + bx + c = 0:";
cin >> c1;
Quadequation q = new Quadequation(a1,b1,c1);
try {
if (a1 == 0) {
throw 1; //not a quadratic equation .it is linear equation
}
if (((b1*b1) - 4 * (a1)*(c1)) < 0) {
throw 2; //roots are imaginary
}
if (((b1*b1) - 4 * (a1)*(c1)) == 0) {
throw 3; //roots are equal
}
q.find_roots(a1, b1, c1, r1, r2);
cout << "The first root answer is: " << r1 << endl;
cout << "The second root answer is: " << r2 << endl;
}
catch (int x) {
if(x==1)
cout << "Error--It is not a quadratic equation" << endl;
else if (x==2)cout << "Error--can't have a negative root" << endl;
else cout << "Error--can't have a repeated root" << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.