Next project in my C++ course is to create a class Polynomial to add, subtract, multiply by a scalar, and multiply by another polynomial. I've written my code, and it works fine when I run it, but there's an application that is provided by our instructor to compare my .exe file with the solution.exe using an input file (.inp). I run the program myself and I see no errors, but when I compare it to the solution via this application, I get some odd answers like 68287e-313.
I've tried several input files and it seems that during the polynomial*polynomial section of the class is where the problems arise. This is my first experience with dynamic allocation, so I feel as though this may be the problem. I've tried peppering cout's throughout the code, and it gives me the correct numbers.
By no means have I written this the easiest or most conventional, but if anyone sees a possible problem with dynamic memory or anything else, I'd appreciate suggestions. I don't want to post the whole code as of yet, so I will post the information needed to look at my problem (compiled with Code::Blocks and windows 7):
//private constructor
Polynomial(const Polynomial& base,double scalar,int mdegree)
{
delete[] coef;
degree=base.degree+mdegree;
coef=new double[degree+1];
for(int i=degree;i>=0;i--)
coef[i]=0;
for(int i=base.degree;i>=0;i--)
{
if(base.coef[i] != 0)
coef[i+mdegree]=base.coef[i];
}
if(mdegree!=0)
coef[0]=0;
multc(scalar);
};
//uses private constructor to separate the second polynomial (pol2), multiplies it by a constant via multc(), and shifts its degree (due to multiplying exponents
//then, this polynomial is multiplied by the base polynomial and added to a "temporary result" polynomial which is transferred to the base polynomial after the whole process
void Polynomial::mult(const Polynomial& pol)
{
//another object is created for use within the private constructor
Polynomial copy;
copy.degree=degree;
copy.coef=new double[degree+1];
for(int i=degree;i>=0;i--)
copy.coef[i]=coef[i];
//create a result polynomial to store the coefficients, first initialized to 0 due to compiler error in prior debugging
Polynomial result;
result.degree=degree+pol.degree;
result.coef=new double[result.degree+1];
for(int n=result.degree;n>=0;n--)
result.coef[n]=0;
//this loop separates the second polynomial one term at a time then uses the private constructor to multiply the first polynomial by the factor
//and is added to the result polynomial. This process is repeated until there are no more terms in the second polynomial.
for(int i=pol.degree;i>=0;i--)
{
Polynomial p1(copy, pol.coef[i], i);
result.add(p1);
p1.~Polynomial();
}
//delete the original and copy the result into original
delete[] coef;
degree=result.degree;
coef=new double[degree+1];
for(int n=degree;n>=0;n--)
coef[n]=0;
for(int n=degree;n>=0;n--)
coef[n]=result.coef[n];
//prints the polynomial via a member function print() and deconstructs the temproary polynomials "copy" and "result"
cout<<"Updated Polynomial (poly*=poly2) : ";
print();
copy.~Polynomial();
result.~Polynomial();
}
//main function:
//polynomials initialized to 0 and coef=new double[1];
Polynomial pol1;
Polynomial pol2;
cout<< "Enter the second polynomial : \n";
pol2.getDegree();//inputs the coefficients and degree of polynomial 2
pol1.mult(pol2);//call to function for pol1*pol2