Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program having Polynomial.h, Polynomial.cpp and Main.cpp Implementation A po

ID: 3885751 • Letter: C

Question

C++ Program having Polynomial.h, Polynomial.cpp and Main.cpp

Implementation
A polynomial can be represented by a collection of coefficient/exponent pairs. For example, the above
polynomial can be represented by the following collection of number pairs:
(4,5) (7,3) (-1,2) (9,0)

One way to do this would be to use an array of structures. You may assume a limit of 10 terms in the
equation. Also, all exponents will be positive and integers. All coefficients will be integers.

Methods
You will need to implement the following methods:
void addTerm(int coefficient, int exponent) – add the specified term to the
polynomial. Used repeatedly to construct the polynomial.
int degree() - return the degree of the polynomial – that is, the highest exponent.
int coefficient(int power) – return the coefficient of the term with the given power, or 0
if there is no such term.
int numberOfTerms() - return the number of terms in the polynomial.
int value(int x) – return the value of the polynomial for the given value of x.


Main Program
• Your main program should read pairs of numbers from a file and create a Polynomial by
repeatedly calling the addTerm() method
• You will run your program 2 times, using 2 different input files:

For each execution, output the following:

Degree: d
# terms: t
coeff(0): c
coeff(1): c
:
:
coeff(7): c
value(1): v

4.75 + 7x® – 12 + 9

Explanation / Answer

#include<iostream>
#include<math.h>
#include<fstream>

using namespace std;

#define MAX 10

struct term{
   int coefficient;
   int power;
};

class Polynomial{
    private:
       term *poly;
       int size;

    public:
       Polynomial() {
         poly = new term[MAX];
         size = 0;
       }
       void addTerm(int a, int b){
          poly[size].coefficient = a;
          poly[size].power = b;
          size = size + 1;
       }
       int degree(){
          int max = 0;
          for (int i = 0; i<size; i++){
             if (poly[i].power > max)
                max = poly[i].power;
          }
          return max;
       }
       int coefficient(int pw){
          int res = 0;
          for (int i = 0; i<size; i++){
            
             if (poly[i].power == pw){
                res = poly[i].coefficient;
                break;
             }
          }
          return res;
       }
       int numberOfTerms(){
          return size;
       }
       int value(int x){
          int value = 0;
          for (int i = 0; i<size; i++){
              value = value + poly[i].coefficient * pow(x,poly[i].power);
            
          }
          return value;
       }

};

int main(){

   Polynomial p;
   ifstream fin;
   int a,b;

   fin.open("inputpoly.txt");
   if (!fin){
      cout << "Error opening file ";
      return 0;
   }
   while (fin >> a >> b){
       p.addTerm(a,b);
   }
   fin.close();
   cout << p.degree() << endl;
   cout << p.coefficient(2) << endl;
   cout << p.coefficient(5) << endl;
   cout << p.coefficient(0) << endl;
   cout << p.coefficient(7) << endl;
   cout << p.numberOfTerms() << endl;
   cout << p.value(2) << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote