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

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 : 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!