Compile the code and run the program. Try the following inputs for a, b, and c:
ID: 3891882 • Letter: C
Question
Compile the code and run the program. Try the following inputs for a, b, and c:
3 4 -4
3 4 4
Modify the calculateRoots function such that an std::runtime_error is thrown when the discriminant is negative, with the string argument “Cannot calculate roots when the discriminant is negative..."
That is, throw runtime_error("Cannot calculate roots when the discriminant is negative...");
Compile and run the program with the inputs in 2b; this set of inputs should cause a runtime_error exception to be thrown, and the program should abruptly terminate execution.
Add a try-block inside the main function that surrounds the while loop with the following exception handler: catch (std::runtime_error &e) { cout << "ERROR: " << e.what() << endl; return 1; }
Compile and run the program with the inputs in 2b; this set of inputs should cause an std::runtime_error exception to be thrown. In this instance, the exception handler in main should catch the thrown runtime_error. Accordingly, “Error : cannot calculate roots when the discriminant is negative…” should be output to the screen and the program should terminate.
Remove the try-catch block you added in step 5, then add a try-catch statement in the main function that catches the std::runtime_error thrown by the calculateRoots function when the discriminant is negative and output “No real roots.”
This particular try-catch statement should not be for the entire main function. It should only encompass the part where the program still functions as otherwise designed.
That is, after outputting “No real roots.”, it will ask if the user wants to continue or not as with any inputs that do not cause the exception.
Compile and run the program with the inputs provided in step-2. The program should output "No real roots." with the second set of inputs, and then prompt the user to continue or exit.
Modify the exception handler written in step-7 to instead catch an std::out_of_range instead of an std::runtime_error.
Compile and run the program with the inputs provided in step-2. Think about why the error message “ERROR: Cannot calculate roots when the discriminant is negative…” is output and program terminated? Could you explain what’s going on here on an exam? If not, discuss this further with other students/instructors.
Return the exception handler written in step-7 and modified in step-9 to once again catch an std::runtime_error exception.
Compile and run the program with the inputs provided in step-2. The program, with the second set of inputs, should ask the user if he wants to continue regardless of the error and if so, output "No real roots.”, and then prompt the user to continue or exit.
Here is my code:
Explanation / Answer
Hello, I have answered this question before, so I’m referring it here. Below is the modified code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
Also, if we remove the statement to catch runtime exception and replace it with a std::out_of_range, the program terminates after displaying “ERROR: Cannot calculate roots when the discriminant is negative…”. This is because the calculateRoots() method has thrown a runtime_error exception and there is no catch block to catch it. Hence the program is terminated abnormally.
//Code
#include <iostream>
#include <math.h>
#include <limits>
#include <stdexcept>
using namespace std;
void getCoefficients(double& a, double& b, double& c)
{
do {
if (!cin.good()) {
cout << "Coefficients must be numbers." << endl << endl;
// clean stream
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ');
}
cout << "Enter a, b and c" << endl;
cin >> a;
cin >> b;
cin >> c;
} while (!cin.good());
}
void calculateRoots(double a, double b, double c,
double& x1, double& x2)
{
// calculate discriminant
double disc = b * b - 4 * a * c;
if(disc<0){
//throwing exception if the discriminant is negative
throw runtime_error("Error : cannot calculate roots when the discriminant is negative...");
}
// positive discriminant
x1 = (-b + sqrt(disc)) / (2 * a);
x2 = (-b - sqrt(disc)) / (2 * a);
}
void outputRoots(double x1, double x2)
{
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
int main()
{
// coefficients
double a = 0;
double b = 0;
double c = 0;
// roots
double x1 = 0;
double x2 = 0;
char cont = 'y';
while (cont == 'y') {
getCoefficients(a, b, c);
// Check if a is equal to 0
if (a == 0) {
// if true output not a quad eq
cout << endl << "Not a quadratic equation." << endl;
} else {
try{
//trying to calculate roots
calculateRoots(a, b, c, x1, x2);
//if no exception occured, displaying roots
outputRoots(x1, x2);
}catch(const std::exception& e) {
//exception occured, displaying message
//cout<<e.what(); //(uncomment this line to print original exception message)
cout<<"No real roots"<<endl;
}
}
cout << endl << "Input 'y' or 'Y' to do another. Input anything else to exit." << endl;
cin >> cont;
cont = tolower(cont);
}
}
/*OUTPUT*/
Enter a, b and c
3 4 -4
x1 = 0.666667
x2 = -2
Input 'y' or 'Y' to do another. Input anything else to exit.
y
Enter a, b and c
3 4 4
No real roots
Input 'y' or 'Y' to do another. Input anything else to exit.
y
Enter a, b and c
-4 1 3
x1 = -0.75
x2 = 1
Input 'y' or 'Y' to do another. Input anything else to exit.
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.