Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is lab1.cpp #include #include quadraticExpression.h using namespace std; void evaluateExpression(const quadraticExpression &); int main () { quadraticExpression q[6] = { quadraticExpression(2.1, 3, -7), quadraticExpression(1.4,

Here is lab1.cpp

#include #include "quadraticExpression.h"

using namespace std;

void evaluateExpression(const quadraticExpression &);

int main () { quadraticExpression q[6] = { quadraticExpression(2.1, 3, -7), quadraticExpression(1.4, 3.9, +7), quadraticExpression(-.75, 0, 0), quadraticExpression(0, .3, -7), quadraticExpression(0, 0, 4), quadraticExpression()};

for (int i=0; i<6; i++) evaluateExpression(q[i]);

return EXIT_SUCCESS; }

void evaluateExpression(const quadraticExpression &q) { int errorsHandled = 0;

cout << "f(-5) = " << q.evaluate(-5) << endl; cout << "f(0) = " << q.evaluate(0) << endl; cout << "f(5) = " << q.evaluate(5) << endl;

if (q.getNumberOfRoots() == INFINITE_ROOTS) cout << "The Expression has Infinite Roots" << endl; else if (q.getNumberOfRoots() == ONE_ROOT) cout << "The Expression has One Root at x = " << q.getFirstRoot() << endl; else if (q.getNumberOfRoots() == TWO_ROOTS) { cout << "The Expression has First Root at x = " << q.getFirstRoot() << endl; cout << "The Expression has Second Root at x = " << q.getSecondRoot() << endl; } else cout << "The Expression has No Roots" << endl;

try { q.getFirstRoot(); } catch (domain_error e) { errorsHandled ++; }

try { q.getSecondRoot(); } catch (domain_error e) { errorsHandled ++; }

cout << "Errors Handled: " << errorsHandled << endl;

cout << endl; cout << endl; }

Here is Lab #01.cpp

// Name: Sunena Benjamin // Problem: 8 & 9 // Page: 91-92 // Quadratic Expression & Real Root

#include #include #include using namespace std; class quadraticExpression { private: double a, b, c;//coefficients of the quadratic equation public: enum Roots { NO_ROOTS, ONE_ROOT, TWO_ROOTS, INFINITE_ROOTS }; quadraticExpression() : a(0), b(0), c(0) {}//default constructor void changeCoeeficients(double a1, double b1, double c1) { a = a1; b = b1; c = c1; } //inline functions to return coefficients inline double getACoeeficient() const { return a; } inline double getBCoeeficient() const { return b; } inline double getCCoeeficient() const { return c; } //evaluate function for given value of double double evaluateFunction(double x) const { return a * x*x + b * x + c; } int getNoOfRoots() const { //infinite roots if (a == 0 && b == 0 && c == 0) return INFINITE_ROOTS; //If a and b are zero, but c is nonzero, then there are no real roots if (a == 0 && b == 0 && c != 0) return NO_ROOTS; //If a is zero, and b is nonzero, then the only real root is -c/b. if (a == 0 && b != 0) return ONE_ROOT; //no real roots if (a != 0 && b*b < 4 * a*c) return NO_ROOTS; //2 equal roots if (a != 0 and b*b == 4 * a*c) return ONE_ROOT; //2 distinct real roots if (a != 0 && b*b > 4 * a*c) return TWO_ROOTS; return NO_ROOTS;//if none of the above condition matches, which is unlikely to happen. //Last return just to suppress the warnings. } double getFirstRoot()//returns the smaller of the 2 roots { if (getNoOfRoots() == 0)//if no real roots then throw domain_error throw domain_error(" Domain Error: No real roots"); if (getNoOfRoots() == 3)//if infinite roots then return 0. return 0; if (a == 0 && b != 0)//if only one root the return its value return -c / b; if (a != 0 && b*b == (4 * a*c))//2 equal roots each equal to -b/2a return -b / (2 * a); if (a != 0 && b*b > (4 * a*c))//2 distinct real roots, returning the smaller of the two. { int x1 = (-b + (sqrt(pow(b, 2) - (4 * a*c)))) / (2 * a); int x2 = (-b - (sqrt(pow(b, 2) - (4 * a*c)))) / (2 * a); return min(x1, x2); } } double getSecondRoot() { if (getNoOfRoots() == 0)//if no real roots then domain error throw domain_error(" Domain Error: No real roots"); if (getNoOfRoots() == 3)//infinite roots return 0; if (a == 0 && b != 0)//if only one root then throw domain_error for second root throw domain_error(" Domain Error: No second root"); if (a != 0 && b*b == (4 * a*c))//2 equal roots return -b / (2 * a); if (a != 0 && b*b > (4 * a*c))//2 distinct roots,returning larger of the two roots. { int x1 = (-b + (sqrt(pow(b, 2) - (4 * a*c)))) / (2 * a); int x2 = (-b - (sqrt(pow(b, 2) - (4 * a*c)))) / (2 * a); return max(x1, x2); } } void display() { printf("a: %f, b: %f, c: %f ", a, b, c); } }; int main() { try { quadraticExpression q; q.display();//infinite roots cout << "No of roots: " << q.getNoOfRoots() << ", Smaller Root: " << q.getFirstRoot() << ", Larger Root: " << q.getSecondRoot() << endl; q.changeCoeeficients(1, 2, 1); q.display();//equal roots cout << "No of roots: " << q.getNoOfRoots() << ", Smaller Root: " << q.getFirstRoot() << ", Larger Root: " << q.getSecondRoot() << endl; q.changeCoeeficients(1, -5, 6); q.display();//2 distinct roots cout << "No of roots: " << q.getNoOfRoots() << ", Smaller Root: " << q.getFirstRoot() << ", Larger Root: " << q.getSecondRoot() << endl; q.changeCoeeficients(2, 2, 2); q.display();//no real roots. throws error cout << "No of roots: " << q.getNoOfRoots() << ", Smaller Root: " << q.getFirstRoot() << ", Larger Root: " << q.getSecondRoot() << endl; system("pause"); } catch (exception &err) { cerr << err.what();//catched domain error and displays on console } }

/* a: 0.000000, b: 0.000000, c: 0.000000 No of roots: 3, Smaller Root: 0, Larger Root: 0 a: 1.000000, b: 2.000000, c: 1.000000 No of roots: 1, Smaller Root: -1, Larger Root: -1 a: 1.000000, b: -5.000000, c: 6.000000 No of roots: 2, Smaller Root: 2, Larger Root: 3 a: 2.000000, b: 2.000000, c: 2.000000 Domain Error: No real roots */

Please test the Lab #01.cpp against the lab1.cpp to see if it works and to test out the class. Thank you. Please provide screenshots of the tested code and tested output as well as regular text.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions