Question

In: Computer Science

Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...

Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a default constructor.) Remove the existing constructor. Write one constructor that takes an array of doubles and the number of entries and uses the array to make a Polynomial object. Use the append method to do this. Overload the operators +, -, and * so you can add, subtract and multiply polynomials. Overload the output operator << so you can output a representation of your operator to cout or a file. Overload the [ ] operator so you can extract or change a coefficient of a polynomial. Write a method degree that returns the degree of the polynomial. Write a method eval that takes a double argument x and returns the value of the Polynomial at x. Be sure to write a destructor, copy constructor, and assignment operator. The Polynomial class should have private member variables coeffs_ for the array of coefficients, size_ for the number of positions used in the array, capacity_ for the capacity of the array of coefficients.

(C++)

Solutions

Expert Solution

Ans :

Program

plynomial.h file

#include<iostream>
using namespace std;
//Create a class Polynomial
class Polynomial {
//Attributes
private:
   double* coefficients;
   int size_used;
//Member functions
public:
   //Constructor
   Polynomial(double Coeffs[], int terms);
   //Destructor
   ~Polynomial();
   //Copy constructor
   Polynomial(const Polynomial& a);
   //Operator overloads
   Polynomial& operator= (const Polynomial& rhs);
   const Polynomial operator+ (const Polynomial& other) const;
   const Polynomial operator- (const Polynomial& other) const;
   const Polynomial operator* (const Polynomial& other) const;
   friend ostream & operator << (ostream &out, const Polynomial &c);
   double operator[](int index);
   //Evaluate a polynomial
   double evaluateAt(int x);
};


Polynomial.cpp

#include "plynomial.h"
//Constructor allocate array with passed size and allocate values
Polynomial::Polynomial(double Coeffs[], int N_terms) {
   size_used= N_terms;
   coefficients= new double[size_used];
   for (int i = 0; i < size_used; i++)
       coefficients[i] = Coeffs[i];
}
//Destructor, deallocate allocated memory
Polynomial::~Polynomial() {
   if (coefficients)
   {
       delete[] coefficients;
       coefficients = nullptr;
   }
}
//Copy constructor
Polynomial::Polynomial(const Polynomial& a) {
   size_used = a.size_used;
   coefficients = new double[size_used];
   for (int i = 0; i < size_used; i++)
       coefficients[i] =a.coefficients[i];
}
//Assignment operator overload
Polynomial& Polynomial::operator= (const Polynomial& a) {
   if (this == &a)
       return *this;

   this->size_used = a.size_used;
   coefficients = new double[size_used];
   for (int i = 0; i < size_used; i++)
       this->coefficients[i] = a.coefficients[i];
   return *this;
}
//Operator + overload
const Polynomial Polynomial::operator+ (const Polynomial& other) const {
   int maxSize = (size_used > other.size_used) ? size_used : other.size_used;
   double *a;
   a=new double[maxSize];
   for (int i = 0; i <= maxSize; i++) {
       a[i] = coefficients[i] + other.coefficients[i];
   }
   Polynomial p(a,maxSize);
   return p;
}
//Operator - overload
const Polynomial Polynomial::operator- (const Polynomial& other) const {
   int maxSize = (size_used > other.size_used) ? size_used : other.size_used;
   double *a;
   a = new double[maxSize];
   for (int i = 0; i <= maxSize; i++) {
       a[i] = coefficients[i] - other.coefficients[i];
   }
   Polynomial p(a, maxSize);
   return p;
}
//Operator * overload
const Polynomial Polynomial::operator* (const Polynomial& other) const {
   int maxSize = (size_used > other.size_used) ? size_used : other.size_used;
   double *a;
   a = new double[maxSize];
   for (int i = 0; i <= maxSize; i++) {
       a[i] = coefficients[i] * other.coefficients[i];
   }
   Polynomial p(a, maxSize);
   return p;
}
//Function to evaluate polynomial value
double Polynomial::evaluateAt(int x)
{
   double sum = 0.0;
   double xPow = 1.0;
   if (coefficients)
       for (int i = 0; i < size_used; i++)
       {
           sum += xPow * coefficients[i];
           xPow *= x;
       }

   return sum;
}
//Extraction operator overload
ostream& operator<< (ostream &out, const Polynomial &c)
{
   out <<c.coefficients[c.size_used-1]<< "x^" << c.size_used - 1;
   for (int i = c.size_used - 2; i >= 0; i--)
       std::cout << " + " << c.coefficients[i] << "x^" << i;
   return out;
}
//Overload index operator
double Polynomial::operator[](int index) {
   if (index >= size_used)
   {
       cout << "No element in that coefficient\n";
       return -1;
   }
   return coefficients[index];
}

test.cpp

/*
Test class
*/

#include "plynomial.h"

int main()
{
   //Two double arrays for check
   double coeff1[] = { 1.2,3.4 };
   double coeff2[] = { 1.2,3.4,1.5 };
   //Create 2 polynomials
   Polynomial p1(coeff1, 2);
   Polynomial p2(coeff1, 2);
   //Evaluate check
   cout <<"Evaluate check: "<<p1.evaluateAt(1) << endl;
   //Add check
   Polynomial p3=p1 + p2;
   cout <<"Addition check: "<< p3 << endl;
   //Assignment check
   p3 = p1;
   cout << "Assignment check: " << p3 << endl;
   //Subtract check
   Polynomial p4(coeff2, 3);
   p3 = p4-p3;
   cout << "Subtraction check: " << p3 << endl;
   //Index check
   cout << "Index check: " << p4[1] << endl;
}

Output :

Evaluate check: 4.6
Addition check: 6.8x^1 + 2.4x^0
Assignment check: 3.4x^1 + 1.2x^0
Subtraction check: 1.5x^2 + 0x^1 + 0x^0
Index check: 3.4

Thank you...


Related Solutions

           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Develop a class Polynomial. The internal representation of a Polynomial is an array of terms. Each...
Develop a class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. has the coefficient 2 and the exponent 4. The User should be able to add as many terms as he wants, so the array should be dynamic. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: 1. Overload the...
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT