CMPS 148: Homework 3 C++ Please Follow Instructions and use the partial code. Th
ID: 3567564 • Letter: C
Question
CMPS 148: Homework 3 C++
Please Follow Instructions and use the partial code.
The output should be the same as the sample.
Write a class that models a polynomial equation of the form:
ax2 + bx + c = 0
The class should have 3 properties, A, B, and C. You are to provide several functions as well,
including an evaluate function that accepts an x and computes the results, and a findRoots function
that executes the quadratic formula to determine the equations roots (x values which cause the
polynomial to evaluate to 0).
Details for the findRoots function:
The quadratic formula is used to find the value(s) of x that lead to the polynomial equaling zero. It is
defined as follows: Where the two values found can be called root1 and root2. If the value of b2
-4ac is negative there are no real roots (the function never equals 0). Your findRoots function should return true or false, depending on whether or not there are real roots (if b2 -4ac is positive). The function accepts two double values using pass by reference. If there are roots, then those two parameters are set within the function. Note, if b
2 -4ac is zero, both roots are the same and that is OK (you just set them both).
On the next page is a main function that uses the Polynomial class. For this homework assignment,
you must use this EXACT program without modification! It is posted as a separate file on
Moodle. You may of course add print statements to help you debug while you work, but I will expect
you to turn in the file without any of your modifications.
NOTE: YOU WILL NEED TO CREATE ADDITIONAL FUNCTIONS BASED ON HOW THE
POLYNOMIAL CLASS IS USED IN THE MAIN.CPP PROVIDED YOU MUST IMPLEMENT ALL
FUNCTIONS REQUIRED TO MAKE IT COMPILE!
SEE PAGE 3 FOR SAMPLE OUTPUT
#include <iostream>
#include "Polynomial.h"
using namespace std;
void printRoots(Polynomial p) {
double r1=0, r2=0;
if ( p.findRoots(r1, r2) ) {
cout << " with roots at " << r1 << " and " << r2 << endl;
}
else {
cout << " No Roots" << endl;
}
}
int main() {
Polynomial p1(14, 19, 2); // Provide a constructor to set A, B, C
Polynomial p2; // Default constructor should initialize to all zeros
double tmp;
cout << "Enter A: ";
cin >> tmp;
p2.setA(tmp); // A, B, and C need to be private variables - supply getters and setters.
cout << "Enter B: ";
cin >> tmp;
p2.setB(tmp);
cout << "Enter C: ";
cin >> tmp;
p2.setC(tmp);
cout << "Two polynomials will be used:" << endl;
cout << "P1: Predefined: ";
p1.print(); // Should print as 14x^2 + 19x + 2
cout << "P2: User-Defined: ";
p2.print(); // Should print in the same format, but with the user-entered values.
cout << "Enter an x value to evaluate both polynomials: ";
cin >> tmp;
cout << "P1: " << p1.evaluate(tmp) << endl;
printRoots(p1);
cout << "P2: " << p2.evaluate(tmp) << endl;
printRoots(p2);
}
Sample output user input in red.
Enter A: 1
Enter B: 1
Enter C: 1
Two polynomials will be used:
P1: Predefined: 14x^2 + 19x + 2
P2: User-Defined: 1x^2 + 1x + 1
Enter an x value to evaluate both polynomials: 8
P1: 1050
with roots at -0.11501 and -1.24213
P2: 73
No Roots
Explanation / Answer
Polynomial.h
-----------------------------
class Polynomial
{
private:
double A;
double B;
double C;
public:
Polynomial()
{
A=0;
B=0;
C=0;
}
Polynomial(double a, double b, double c)
{
A=a;
B=b;
C=c;
}
void setA(double a)
{
A=a;
}
void setB(double b)
{
B=b;
}
void setC(double c)
{
C=c;
}
double evaluate(double x)
{
return ((A*x*x) + (B*x) + C );
}
bool findRoots(double &r1, double &r2)
{
double cond = ((B*B) - (4*A*C));
if(cond < 0)
return false;
else
{
cond = sqrt(cond);
r1 = ((-B) + cond)/(2*A);
r2 = ((-B) - cond)/(2*A);
return true;
}
}
void print()
{
cout<<A<<"x^2 + "<<B<<"x + "<<C<<" ";
}
};
--------------------------------------------------------------------------------------------------------------------
main.cpp
-----------------------------
#include <iostream>
#include "Polynomial.h"
using namespace std;
void printRoots(Polynomial p) {
double r1=0, r2=0;
if ( p.findRoots(r1, r2) ) {
cout << " with roots at " << r1 << " and " << r2 << endl;
}
else {
cout << " No Roots" << endl;
}
}
int main() {
Polynomial p1(14, 19, 2); // Provide a constructor to set A, B, C
Polynomial p2; // Default constructor should initialize to all zeros
double tmp;
cout << "Enter A: ";
cin >> tmp;
p2.setA(tmp); // A, B, and C need to be private variables - supply getters and setters.
cout << "Enter B: ";
cin >> tmp;
p2.setB(tmp);
cout << "Enter C: ";
cin >> tmp;
p2.setC(tmp);
cout << "Two polynomials will be used:" << endl;
cout << "P1: Predefined: ";
p1.print(); // Should print as 14x^2 + 19x + 2
cout << "P2: User-Defined: ";
p2.print(); // Should print in the same format, but with the user-entered values.
cout << "Enter an x value to evaluate both polynomials: ";
cin >> tmp;
cout << "P1: " << p1.evaluate(tmp) << endl;
printRoots(p1);
cout << "P2: " << p2.evaluate(tmp) << endl;
printRoots(p2);
}