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

You are going to implement a class representing a polynomial (e.g.: 4x^5 + 7x^3

ID: 3750236 • Letter: Y

Question

You are going to implement a class representing a polynomial (e.g.: 4x^5 + 7x^3 - x^ 2 + 9). The class will be called Polynomial. Implementation A polynomial can be represented by a collection of coefficient/exponent pairs. For example, 4x^5 + 7x^3 - x^2 + 9 can be represented by the following collection of number pairs: 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.(4, 5) (7, 3) (-1, 2) (9, 0) Methods You will need to implement the following methods: • void addTerm(int coefficient, int exponent) – add the specified term to the polynomial. This will be called repeatedly to construct the polynomial. If a pair with the given exponent is already part of the polynomial, throw an exception. • 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. For 4x^5 + 7x^3 - x^2 + 9: degree() 5 coefficient(2) -1 coefficient (5) 4 coefficient(0) 9 coefficient(7) 0 numberOfTerms() 4 value(2) 189 (4*25 + 7*23 – 1*22 + 9) Data Structures Fall 2018 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: File 2 (representing 10x7 + x4 - 3x3 + 5x2 – 11) For each execution, output the following: 1: 4x5 + 7x3 - x2 + 9 Output the values of all the coefficients from 0 thru 7, even though many of these wont exist in the polynomial and will therefore be 0 4 5 7 3 -1 2 9 0 -11 0 1 4 10 7 5 2 -3 3 (notice the terms are in no particular order) Degree: d # terms: t coeff(0): c coeff(1): c coeff(2): c coeff(3): c coeff(4): c coeff(5): c coeff(6): c coeff(7): c value(1): v value(2): v File 2: 10x7 + x4 - 3x3 + 5x2 – 11

Explanation / Answer

//.h file

#pragma once

#include<iostream>

#include<string>

#include<sstream>

using namespace std;

//strcu to hold coefficient and exponet

struct term

{

int coefficient;

int exponet;

};

class Polynomial

{

//define an array of term with size 10 in constructor

term *terms;

int currentSize;

public:

Polynomial();

~Polynomial();

void addTerm(int coefficient, int exponent);

int degree();

int coefficient(int power);

int value(int x);

string to_string();

int numberOfTerms();

//CHEGGEA to diplay coefficient and exponet pair

void display();

};

---------------------------------------------

//.cpp file

#include"Sep_19_polynomial.h"

Polynomial::Polynomial()

{

currentSize = 0;

terms = new term[10];

}

Polynomial::~Polynomial()

{

delete terms;

}

void Polynomial::addTerm(int coefficient, int exponent)

{

terms[currentSize].coefficient = coefficient;

terms[currentSize++].exponet = exponent;

}

int Polynomial::degree()

{

int max = terms[0].exponet;

for (int i = 0; i < currentSize; i++)

{

if (terms[i].exponet > max)

{

max = terms[i].exponet;

}

}

return max;

}

int Polynomial::coefficient(int power)

{

for (int i = 0; i < currentSize; i++)

{

if (power == terms[i].exponet)

{

return terms[i].coefficient;

}

}

return -1;

}

int Polynomial::value(int x)

{

double sum = 0;

for (int i = 0; i < currentSize; i++)

{

sum += terms[i].coefficient*pow(x, terms[i].exponet);

}

return (int)sum;

}

string Polynomial::to_string()

{

string poly_expr;

stringstream ss;

int coe = 0;

for (int i = 0; i < currentSize; i++)

{

coe = 0;

if (i == 0)

{

ss.str("");

if (terms[i].coefficient > 1)

{

coe = 1;

ss << terms[i].coefficient;

}

}

else

{

if (terms[i].coefficient < 0)

poly_expr += '-';

else

poly_expr += '+';

ss.str("");

if (terms[i].coefficient > 1)

{

coe = 1;

ss << terms[i].coefficient;

}

else

{

if (terms[i].coefficient*-1 > 1)

{

coe = 1;

ss << terms[i].coefficient*-1;

}

}

}

if (coe)

{

poly_expr += ss.str();

}

if (terms[i].exponet > 1)

poly_expr += "x";

ss.str("");

ss << terms[i].exponet;

if(terms[i].exponet >=2)

poly_expr += ss.str();

}

return poly_expr;

}

void Polynomial::display()

{

for (int i = 0; i < currentSize; i++)

{

cout << "Coe: " << terms[i].coefficient << "Exp: " << terms[i].exponet << endl;

}

}

int Polynomial::numberOfTerms()

{

return currentSize;

}

---------------------------------------------

//main.cpp

#include<fstream>

#include"Sep_19_polynomial.h"

using namespace std;

int main()

{

//create input stream object to read from file

ifstream in;

//open input file having polynomial expr

in.open("Polynomial.txt");

//chk if file open

if (!in)

{

cout << "Not able to open file" << endl;

return -1;

}

int coe, exp, count = 0;

//create polynomial obj

Polynomial poly;

//read coefficient and exponent pair from input

while (!in.eof())

{

in >> coe >> exp;

//add to object poly

poly.addTerm(coe, exp);

count++;

}

cout << "Coefficient and exponent pair read from file: " << endl;

//poly.display();

cout << "Highest degree returned from method degree: " << poly.degree() << endl;

cout << "coefficient of exponent 7 (-1 if polynomial has no exponent with that number): " << poly.coefficient(7) << endl;

cout << "Number of terms in polynomial: " << poly.numberOfTerms() << endl;

cout << "Polynomial: " << poly.to_string() << endl;

cout << "Value of poly for value 1: " << poly.value(1) << endl;

}

/*output

output for text file content 10 7 1 4 -3 3 5 2 -11 0
Coefficient and exponent pair read from file:
Highest degree returned from method degree: 7
coefficient of exponent 7 (-1 if polynomial has no exponent with that number): 10
Number of terms in polynomial: 5
Polynomial: 10x7+x4-3x3+5x2-11
Value of poly for value 1: 2
//output2 for text file content 4 5 7 3 -1 2 9 0
Coefficient and exponent pair read from file:
Highest degree returned from method degree: 5
coefficient of exponent 7 (-1 if polynomial has no exponent with that number): -1
Number of terms in polynomial: 4
Polynomial: 4x5+7x3-x2+9
Value of poly for value 1: 19
*/

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