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