Question

 The extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other features (i.e., constructor and operators) are given as part of the solution for tutorial 3. In the .cpp file for the new methods you need to include cmath that contains the definition of pow - raise to power.

The extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other feThe extended specification of class Polynomial is shown below. You only need to implement the last four methods. The other fe


The goal of this problem set is to extend the solution of tutorial 3. In particular, we wish to

add methods to calculate a polynomial and to determine a polynomial’s derivative and it’s

indefinite and definite integral.

Start with the solution of tutorial 3 (see Canvas). Do not edit the provided files. Rather create

a new .cpp unit, called PolynomialPS1.cpp, to implement the new methods. Please note

that in C++ the implementation of a class does not need to occur in just one compilation unit

(i.e., a .cpp file). As long as the compiler and the linker see all definitions, the build process

should succeed, if the program does not contain any errors.

Remember, a polynomial can be expressed concisely by using summation notation:

That is, a polynomial can be written as the sum of a finite number of terms aix

i. Each term

consists of the product of a number ai, called the coefficient, and a variable x raised to integer

powers – x

i. The exponent i in x

i is called the degree of the term aix

i. The degree of a

polynomial is the largest exponent of any one term with a non-zero coefficient.

To calculate a polynomial, we need to provide a value for the variable x. Mathematically, we

can write

to denote the result of calculating a polynomial for a given value of x. Again, this allows for a

straightforward mapping to a for-loop is C++. To compute the x

i, we need to use the function

pow, called raise-to-power, which is defined in cmath. For more details and example uses of

pow, see http://www.cplusplus.com/reference/cmath/pow/.

Please refer to PolynomialMath.pdf available on Canvas to review how one can compute the

differential and integral of a polynomial. An understanding of the mathematical principles is

essential for the successful completion of this assignment. Remember, polynomials appear

frequently in computer science and software engineering. A notable example it polynomial

time complexity of algorithms, that is, algorithms that are effectively computable.


The extended specification of class Polynomial is shown below. You only need to implement

the last four methods. The other features (i.e., constructor and operators) are given as part

of the solution for tutorial 3. In the .cpp file for the new methods you need to include cmath

that contains the definition of pow – raise to power.



#pragma once

#include

#define MAX_DEGREE 20+1+1 // max degree 0 to 20 plus 1 for integral

class Polynomial

{

private:

int fDegree; // the maximum degree of the polynomial

double fCoeffs[MAX_DEGREE]; // the coefficients (0..10, 0..20, 0..21)

public:

// the default constructor (initializes all member variables)

Polynomial();

// binary operator* to multiple to polynomials

// arguments are read-only, signified by const

// the operator* returns a fresh polynomial with degree i+j

Polynomial operator*( const Polynomial& aRight ) const;

// binary operator== to compare two polynomials

// arguments are read-only, signified by const

// the operator== returns true if this polynomial is

// structurally equivalent to the aRHS polynomial

bool operator==( const Polynomial& aRHS ) const;

// input operator for polynomials

friend std::istream& operator>>( std::istream& aIStream,

Polynomial& aObject );

// output operator for polynomials

friend std::ostream& operator<<( std::ostream& aOStream,

const Polynomial& aObject );

// new methods in problem set 1

// call operator to calculate polynomial for a given x (i.e., aX)

double operator()( double aX ) const;

// compute differential

// the differential is a fresh polynomial with degree fDegree-1

// the method does not change the current object

Polynomial getDifferential() const;

// compute indefinite integral

// the indefinite integral is a fresh polynomial with degree fDegree+1

// the method does not change the current object

Polynomial getIndefiniteIntegral() const;

// calculate definite integral

// the method does not change the current object

// the method computes the indefinite integral and then calculates it

// for xlow and xhigh and returns the difference

double getDefiniteIntegral( double aXLow, double aXHigh ) const;

};


Use as main program the following code:

#include

#include "Polynomial.h"

using namespace std;

int main()

{

Polynomial A;

cout << "Specify polynomial:" << endl;

cin >> A;

cout << "A = " << A << endl;

double x;

cout << "Specify value of x:" << endl;

cin >> x;

cout << "A(x) = " << A( x ) << endl;

cout << "Indefinite integral of A = "

<< A.getIndefiniteIntegral() << endl;

cout << "Differential of A = "

<< A.getDifferential() << endl;

cout << "Differential of indefinite integral of A = "

<< A.getIndefiniteIntegral().getDifferential() << endl;

cout << "Definite integral of A(xlow=0, xhigh=12.0) = "

<< A.getDefiniteIntegral( 0, 12.0 ) << endl;

if ( A == A.getIndefiniteIntegral().getDifferential() )

{

cout << "Polynomial operations are sound." << endl;

}

else

{

cout << "Polynomial operations are broken." << endl;

}

return 0;

}

Using -0.25x + 4.0 and 16 for the first calculation, the test code should produce the following reault.



ci Microsoft Visual Studio Debug Console Specify polynomial: 9.0 -0.25 = 4x^0 + -0.25x^1 Specify value of x: 16 (x) = 0 Indef


3 0
Add a comment Improve this question Transcribed image text
Answer #1

:Program files

Polynomial.h

#pragma once
#include
#define MAX_DEGREE 20+1+1 // max degree 0 to 20

class Polynomial
{
private:
int fDegree; // the maximum degree of the polynomial
double fCoeffs[MAX_DEGREE]; // the coefficients (0..10, 0..20)

public:
//default constructor
Polynomial();

// binary operator* to multiple to polynomials
// arguments are read-only, signified by const
// the operator* returns a fresh polynomial with degree i+j
Polynomial operator*(const Polynomial& aRHS) const;

// binary operator== to compare two polynomials
// arguments are read-only, signified by const
// the operator== returns true if this polynomial is
// structurally equivalent to the aRHS polynomial
bool operator==(const Polynomial& aRHS) const;

// input operator for polynomials
friend std::istream& operator>>(std::istream& aIStream, Polynomial& aObject);

// output operator for polynomials
friend std::ostream& operator<<(std::ostream& aOStream, const Polynomial& aObject);

// call operator to calculate polynomial for a given x (i.e., aX)
double operator()(double aX) const;

// compute differential
// the differential is a fresh polynomial with degree fDegree-1
// the method does not changethe current object
Polynomial getDifferential() const;

// computeindefinite integral
// the indefinite integral is a fresh polynomial with degree fDegree+1
// the method does not change the current object
Polynomial getIndefiniteIntegral() const;

// calculatedefinite integral
// the method does not change the current object
// the method computes the indefinite integral and then calculates it
// for xlow and xhigh and returns the difference
double getDefiniteIntegral(double aXLow, double aXHigh) const;
};
#pragma once #include <iostream> #define MAX_DEGREE 20+1+1 // max degree o to 20 class Polynomial private: int fDegree; doubl

Polynomial.cpp

#include "Polynomial.h"
#include
using namespace std;

//Default constructor
Polynomial::Polynomial()
{
fDegree = 0;
for (int i = 0; i < MAX_DEGREE; i++) {
fCoeffs[i] = 0.0;
}
}

//input operator overloading to store data of polynomial
istream& operator>>(istream& aIStream, Polynomial& aObject)
{
//receive the degree of the polynomial
aIStream >> aObject.fDegree;

//Check if user enter degree equal or less than 10 (required by the question)
while (aObject.fDegree > (MAX_DEGREE / 2))
{
cout << "Please enter degree equal or less than " << MAX_DEGREE / 2 << endl;
aIStream >> aObject.fDegree;
}

//read in polynomial coefficients
for (int i = 0; i <= aObject.fDegree; i++)
aIStream >> aObject.fCoeffs[i];

return aIStream;
}

//output operator overloading to display data of polynomial
ostream& operator<<(ostream& aOStream, const Polynomial& aObject)
{
bool flag = false; //to mark the first element, so that we don't need to put plus sign in front of it

//display the polynomial
for (int i = 0; i <= aObject.fDegree; i++)
{
//only display non-zero coefficients
if (aObject.fCoeffs[i] != 0.0)
{
if (flag) aOStream << " + "; //if this isn't the first element, then add plus sign in front of it
aOStream << aObject.fCoeffs[i] << "x^" << i;
flag = true;
}
}

return aOStream;
}

//equivalent comparison operator overloading to compare 2 polynomials
bool Polynomial::operator==(const Polynomial& aRHS) const
{
if (fDegree != aRHS.fDegree)
return false;

for (int i = 0; i <= fDegree; i++)
if (fCoeffs[i] != aRHS.fCoeffs[i])
return false;

return true;
}

//Multiplying operator overloading to find the product of 2 polynomials
Polynomial Polynomial::operator*(const Polynomial& aRHS) const
{
Polynomial aResult; //Result
aResult.fDegree = fDegree + aRHS.fDegree;
for (int i = 0; i <= fDegree; i++)
for (int j = 0; j <= aRHS.fDegree; j++)
aResult.fCoeffs[i + j] += fCoeffs[i] * aRHS.fCoeffs[j];
return aResult;
}

//bracket operator overloading to compute the polynomial at the given value
double Polynomial::operator()(double aX) const
{
double result = 0.0;
for (int i = 0; i <= fDegree; i++)
{
result += fCoeffs[i] * pow(aX, i);
}
return result;
}

//Find differential equation of the polynomial
Polynomial Polynomial::getDifferential() const
{
Polynomial result;
result.fDegree = fDegree - 1; //decrease the degree as this is the differential equation

//Find the coefficients of differential equation
for (int i = 0; i <= result.fDegree; i++)
result.fCoeffs[i] = fCoeffs[i + 1] * (i + 1);
return result;
}

//Find indefiniteIntegral equation of the polynomial
Polynomial Polynomial::getIndefiniteIntegral() const
{
Polynomial result;
result.fDegree = fDegree + 1; //increase the degree as this is the integral equation
result.fCoeffs[0] = 0;

//Find the coefficients of indefinite integral equation
for (int i = 1; i <= result.fDegree; i++)
result.fCoeffs[i] = fCoeffs[i - 1] / i;
return result;
}

//Compute the value of definite integral
double Polynomial::getDefiniteIntegral(double aXLow, double aXHigh) const
{
Polynomial integral = getIndefiniteIntegral();
return integral(aXHigh) - integral(aXLow);
}

#include Polynomial.h #include <cmath> using namespace std; //Default constructor Polynomial::Polynomial() fDegree = 0; for

if (fDegree != RHS.fDegree) return false; for (int i = 0; i <= fDegree; i++) if (fCoeffs[i] != arhs.fCoeffs[i]) return false;

return result; //Compute the value of definite integral double Polynomial::getDefiniteIntegral (double axLow, double axHigh)

main.cpp

#include
#include "Polynomial.h"
using namespace std;

int main()
{
//Enter data for the polynomial
Polynomial A;
cout << "Specify first polynomial (degree followed by coefficients):" << endl;
cin >> A;
//Display the entered data
cout << "A = " << A << endl;

//x is the value for variable in polynomial
double x;
cout << "Specify value of x:" << endl;
cin >> x;

//Calculate and display the polynomial at the given value of x
cout << "A(x) = " << A(x) << endl;
//Calculate and display the equation of indefinite integral
cout << "Indefinite integral of A = " << A.getIndefiniteIntegral() << endl;
//Calculate and display the differential equation
cout << "Differential of A = " << A.getDifferential() << endl;
//Calculate and display definite integral at given low and high
cout << "Differential of indefinite integral of A = " << A.getIndefiniteIntegral().getDifferential() << endl;
cout << "Definite integral of A(xlow=0, xhigh=12.0) = " << A.getDefiniteIntegral(0, 12.0) << endl;

//Announce if the process was successful
if (A == A.getIndefiniteIntegral().getDifferential())
{
cout << "Polynomial operations are sound." << endl;
}
else
{
cout << "Polynomial operations are broken." << endl;
}
return 0;
}

#include <iostream> #include Polynomial.h using namespace std; int main() //Enter data for the polynomial Polynomial A; cou

output

Specify first polynomial (degree followed by coefficients):
1
4.0 -0.25
A = 4x^0 + -0.25x^1
Specify value of x:
16
A(x) = 0
Indefinite integral of A = 4x^1 + -0.125x^2
Differential of A = -0.25x^0
Differential of indefinite integral of A = 4x^0 + -0.25x^1
Definite integral of A(xlow=0, xhigh=12.0) = 30
Polynomial operations are sound.

Specify first polynomial (degree followed by coefficients): 4.0 -0.25 A = 4x^0 + -0.25x^1 Specify value of x: 16 A(x) = 0 Ind

Hope this helps!

Please let me know if any changes needed.

Thank you!

Add a comment
Know the answer?
Add Answer to:
The goal of this problem set is to extend the solution oftutorial 3. In particular,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please provide full answer with comments this is just begining course of c++ so don't use...

    please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...

  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated!...

    I cannot get the C++ code below to compile correctly. Any assistance would be greatly appreciated! ***main.cpp driver file*** #include #include #include "vector.h" using namespace std; int main() {     vectorInfo v1(3, -1);     vectorInfo v2(2, 3);     cout << "v1 : ";     v1.print();     cout << "v2 : ";     v2.print();     cout << "v1 magnitude : " << v1.magnitude() << endl;     cout << "v1 direction : " << v1.direction() << " radians" << endl;     cout...

  • C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....

    C++ problem: Make a program that the user enter the degree and coefficients of a polynomial. Use the overload operators mentioned in the class below: #include <iostream> #include <stdlib.h> using namespace std; class Polynomial{ public:    Polynomial(); //Default constructor: it creates an Polynomial of degree 99 Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg Polynomial(const Polynomial &Original); //Copy Constructor ~Polynomial(); //Destructor: deallocate memory void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial void printPolynomial(); //printPolynomial Method:...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the...

    Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the prototype added to the .h file......and the implementation in the .cpp file....thanks! edited: I just need the above and then I was told to use the methods in the main program; for the vehicle. Any 3 attributes and any three methofds. source.cpp file // Header Comment #include #include #include #include "Automobile.h" using namespace std; struct Address{ string street; string city; string state; string zip;...

  • The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal...

    The goal is to reinforce the implementation of container class concepts in C++. Specifically, the goal is to create a static implementation of a set. Add the efficiency of each function to the documentation in the header file. Your program must compile. Use test_set.cpp as your test program. Set.h & Test_Set.cpp is code that is already given. All I need is for you add comments to Set.cpp describing each function. FILE: SET.H #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream>...

  • c++ Please help! i keep getting an error message. I think my main problem is not...

    c++ Please help! i keep getting an error message. I think my main problem is not understanding the circle calculations function and how both the area and the circumference is returned from the function. I know & needs to be used, but I am unsure how to use it. Copy the following code and run it. You should break it into the following 3 functions getValidinput - which asks the user to enter the radius and then make sure that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT