Comparing two polynomials with vector's coefficients C++

stevenctran

This code is supposed to output the largest coefficient of 2 different polynomials, but if the first one is larger than the second, it doesn't compile correctly.

So, if the first polynomial is 1 - 2x +4x^3 and the second polynomial is -x + 5x^2 - 3x^6. It will work since the second polynomial is larger than the first.

It posts a "vector subscript out of range" error whenever the first polynomial is larger than the second

class Polynomial {
public:
Polynomial();
Polynomial(const vector<int>& coeffs);

int Degree() const;
int Coefficient(int k) const;
void print() const;
private:
vector<int>coefficient;

int main(){
//Variable and vector for inputs
vector<int> coefficient;
int input = 0;
//Welcome message
cout << "Welcome! Please input the coefficients of the first polynomial p(x).\nWhen you are finished, enter -12345.\n";

//While loop - if input isn't -12345, put the input into coefficient.
while (input != -12345){
    cin >> input;
    coefficient.push_back(input);
}

//Deletes -12345
coefficient.pop_back();

//Puts coefficient values into constructor
Polynomial first(coefficient);

//Prints first polynomial
cout << "\nYour first polynomial is p(x) = ";
first.print();

//Prints degrees of first polynomial
cout << ".\np(x) has degree " << first.Degree();
int degree1 = first.Degree();

//Prints transformation of first polynomial
cout << ".\nThe transform of p(x) is ";
first.Transform();

//clears the values in coefficient for second polynomial inputs.
coefficient.clear();

//Inputs the second polynomial's coefficients.
cout << ".\n\nPlease input the coefficients of the second polynomial q(x).\n";
//Had to use do-while because while loop wouldn't work.
do {
    cin >> input;
    coefficient.push_back(input);

} while (input != -12345);

//Deletes -12345
coefficient.pop_back();

//Puts coefficients into second polynomial
Polynomial second(coefficient);

//Prints second polynomial
cout << "\nYour second polynomial is q(x) = ";
second.print();

cout << ".\nq(x) has degree " << second.Degree();
int degree2 = second.Degree();

if (degree1 > degree2){
    cout << ".\n\nThe coefficient of x^" << degree1 << " in p(x) is " << first.Coefficient(degree1);
    cout << ".\nThe coefficient of x^" << degree1 << " in q(x) is " << second.Coefficient(degree1);
}
else{
    cout << ".\n\nThe coefficient of x^" << degree2 << " in p(x) is " << first.Coefficient(degree2);
    cout << ".\nThe coefficient of x^" << degree2 << " in q(x) is " << second.Coefficient(degree2);
}

int Polynomial::Degree() const{
int number = 0;
for (size_t i = 0, size = coefficient.size(); i < size; i++){
    if (coefficient[i] != 0)
        number = i;
}
return number;
}

int Polynomial::Coefficient(int k) const{
if (coefficient[k] != 0)
    return coefficient[k];
else
    return 0;
}
Oguk

Assuming your coefficient vector only runs up to the index of the highest power of the polynomial (i.e. it has 4 elements if x^3 is the highest power), try replacing the function by

int Polynomial::Coefficient(int k) const{
    if (k < coefficient.size())
        return coefficient[k];
    else
        return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SML - Multiplying Coefficients of Polynomials

From Dev

Sympy polynomials with `mpfr` coefficients?

From Dev

how to multiply Polynomials coefficients

From Dev

Coefficients of polynomials maxima

From Dev

Comparing two vector<bool> with SSE

From Dev

Comparing two arrays in C?

From Dev

Comparing two matrix in C

From Dev

Solving polynomials with complex coefficients using sympy

From Dev

numpy fit coefficients to linear combination of polynomials

From Dev

implement Horner's scheme in java for two variable polynomials

From Dev

Multiply Two Polynomials in Prolog

From Dev

Multiply Two Polynomials in Prolog

From Dev

Ocaml Product of two polynomials

From Dev

Generate all polynomials with coefficients 0 or 1 given degree n

From Dev

Comparing two passwords for validation in C++

From Dev

Comparing two strings in C (string pointers)

From Dev

Performance cost of comparing two C++ iterators

From Dev

C# Comparing two lists of lists of objects

From Dev

Comparing two arrays in C, element by element

From Dev

Comparing two SQLite databases in C++

From Dev

Comparing two arrays of integers and returning the subscript (C)

From Dev

Loop issue in C# Comparing two arrays

From Dev

Comparing two DataTables by specific column in C#

From Dev

comparing two arrays of integers in c acts odd

From Dev

Comparing two char strings for a password in C

From Dev

C# comparing two object if they are equal

From Dev

Comparing list counts with of two objects C#

From Dev

C: Comparing two elements not producing correct results?

From Dev

Comparing two images *using the GPU* in C#