A = 4x² + 5x + 3x² + 2x² + 8x° Object A 0 1 2 3 4 5 6 7 coef 82030054 degree 7 B = 6x² + 3x² + 4x³ + 8x² + 3x² + 2xº Object B 12 3 coef 230804306 degree 8 5678 add 0 coef 10 degree 5 subtract 0 coef 6 8 N degree 8 0 3 4 11 0 1 2 3 -1 -5 0 in 5 6 7 8 6 10 10 5 8 4 2 -6

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Use C++

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; }

### 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 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.
## 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.
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
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Binary numbers
Learn more about
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.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education