Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication. Implement addition and subtraction functions first.
it should have three files and two txt file One is the main.cpp file One is the Polynomial.cpp file it's in the first picture One is the Polynomial.h file
One is A.txt One is B.txt
The contents of A and B.txt are in the picture.
I need h file and modify main file.
Here is the main.cpp code
#include <iostream> #include <fstream> #include "polynomial.h" using namespace std; int main() { ifstream infile; infile.open("A.txt"); if (infile.fail()) { cout << "Input file opening failed." << endl; exit(1); } int degree_A; infile >> degree_A; int* coef_A = new int[degree_A+1]; for (int i = 0; i < degree_A + 1; i++) coef_A[i] = 0; int coef, exp; while (!infile.eof()) { infile >> coef >> exp; coef_A[exp] = coef; } infile.close(); // read data from B.txt infile.open("B.txt"); if (infile.fail()) { cout << "Input file opening failed." << endl; exit(1); } int degree_B; infile >> degree_B; int* coef_B = new int[degree_B + 1]; for (int i = 0; i < degree_B + 1; i++) coef_B[i] = 0; while (!infile.eof()) { infile >> coef >> exp; coef_B[exp] = coef; } infile.close(); Polynomial p1(coef_A, degree_A); Polynomial p2(coef_B, degree_B); Polynomial p3 = add(p1, p2); Polynomial p4 = subtract(p1, p2); return 0; }
Transcribed Image Text:### Polynomial Operations
This educational content explains polynomial addition and subtraction using the example polynomials \( A \) and \( B \).
#### Polynomials:
- **Polynomial A**:
\[
A = 4x^7 + 5x^6 + 3x^3 + 2x^1 + 8x^0
\]
- **Polynomial B**:
\[
B = 6x^8 + 3x^6 + 4x^5 + 8x^3 + 3x^1 + 2x^0
\]
#### Object Representation:
1. **Object A**:
- Coefficients: \( (0, 0, 0, 3, 0, 0, 5, 4) \)
- Degree: 7
2. **Object B**:
- Coefficients: \( (2, 3, 0, 8, 0, 4, 3, 0, 6) \)
- Degree: 8
#### Polynomial Addition:
- **Operation:**
\[
A + B = 6x^8 + 4x^7 + 8x^6 + 4x^5 + 11x^3 + 5x^1 + 10x^0
\]
- **Addition Representation**:
- Coefficients: \( (10, 5, 0, 11, 0, 4, 8, 4, 6) \)
- Degree: 8
#### Polynomial Subtraction:
- **Operation:**
\[
A - B = 6x^8 + 4x^7 + 2x^6 - 4x^5 + -5x^3 + -x^1 + 6x^0
\]
- **Subtraction Representation**:
- Coefficients: \( (6, -1, 0, -5, 0, -4, 2, 4, 6) \)
- Degree: 8
These calculations illustrate how polynomials can be added and subtracted by aligning like terms, which are terms with the same power, and performing the arithmetic operations on their coefficients.
Transcribed Image Text:## Polynomial Class in C++
Here is a simple C++ class to represent and work with polynomials.
### Class Definition
```cpp
class Polynomial
{
public:
Polynomial();
Polynomial(int d);
Polynomial(int* c, int d);
int getDegree();
int* getCoef();
friend Polynomial add(Polynomial x, Polynomial y);
friend Polynomial subtract(Polynomial x, Polynomial y);
// friend Polynomial multiply(Polynomial x, Polynomial y); // extra point
private:
int* coef;
int degree;
};
```
### Notes
- The class includes constructors for different ways of initializing polynomials.
- Public methods `getDegree()` and `getCoef()` retrieve the degree and coefficients of the polynomial.
- Friend functions `add` and `subtract` allow for addition and subtraction of two polynomials.
- There is a commented-out prototype for a `multiply` function, which can be implemented for an extra point.
- The private section includes the coefficient array and degree of the polynomial.
A note in the code suggests adding additional member functions if needed.
### Polynomial Representation Example
#### A.txt
Polynomial \( A \) is given by:
\[ A = 4x^7 + 5x^6 + 3x^3 + 2x^1 + 8x^0 \]
- **Degree:** 7
- **Coefficients (coef)**: Dynamic array: [8, 2, 0, 0, 3, 0, 5, 4]
#### B.txt
Polynomial \( B \) is given by:
\[ B = 6x^8 + 3x^6 + 4x^5 + 8x^3 + 3x^1 + 2x^0 \]
- **Degree:** 8
- **Coefficients (coef)**: Dynamic array: [2, 3, 0, 8, 0, 4, 3, 0, 6]
### Explanation of Array Representation
- The coefficient arrays store polynomial coefficients with indices representing the corresponding powers of \( x \).
- The degree indicates the highest power of \( x \) in the polynomial.
- These dynamic arrays facilitate operations like addition and subtraction by aligning corresponding powers.
This structure allows for a clear and efficient way to manage polynomial expressions in a programming environment.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.