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

I don't know if Polynomial p1(copy, pol.coef[i], i) should take "copy" as the object for that argument in this segment:

for(int i=pol.degree;i>=0;i--)
    {
        Polynomial p1(copy, pol.coef[i], i);
        result.add(p1);
        p1.~Polynomial();
    }

P.S. Go Gators!

Hey,

if you want, email me your code so can comlile it, and the comparison solution exe, and I'll look it over.

I'm thinking you need to add another delete function in there, and I can let you know where. I had a similiar problem with a comparison program before.

Good Luck!

[email]email snipped[/email]

Hello guys,,
I have a problem also with Polynomial.
Actually, it can be run to show Evaluate and also Addition. But when it goes to Subtraction, it doesnt want to compute from the beginning. It just continue calculating Addition result minus first function.
PLease anyone can help me?

this is mine:

#include <iostream>
#include <math.h>
//#include "polynomial.h"
using namespace std;

//FUNCTION DECLARATION
class polynomial
{
      int    order;
      double *coeff;      

      public:                    
             int get_order(){return order;};
             double get_coeff(int i){return coeff[i];};

             polynomial();                      
             polynomial(polynomial & p); 
             ~polynomial();

             void get_data();
             void display();

             polynomial &evaluate(int X);
             polynomial &addition(polynomial p);
             polynomial &subtraction(polynomial p);
             polynomial &multiplier(double f);
             polynomial &multiply(polynomial p);
};


//FUNCTION IMPLEMENTATION             
polynomial::polynomial()                //default constructor
{    order = 0;
     coeff = new double[order+1];
     for (int i=0; i<=order; i++)
     {     coeff[i]=0; } }

polynomial::polynomial(polynomial &p)
{     order = p.get_order();
      coeff = new double[order+1];
      for (int i=0; i<=order; i++)
      {   coeff[i] = p.get_coeff(i);}
}

polynomial::~polynomial()               //Destructor
{     delete [] coeff; }

void polynomial::get_data()             //ambil data
{   cout << "Order Polynomial n: ";
    cin >>order;
    for (int i=order; i>=0; --i)
    {   cout <<"Enter coefficient X^"<<i<<" : ";
        cin >> coeff[i]; }
}


void polynomial::display()
{     int i=get_order();
      if (i>=2) {
      for (int i=order; i>=2; --i)
       {    cout <<coeff[i]<<"X^"<<i<<" + ";} }

       else if (i<2);
       {    cout <<coeff[1]<<"X + "<<coeff[0]<<endl; } }


polynomial &polynomial::evaluate(int X)
{  
   int x = X;
   double p;
   double sum=0;
   for (int i=0; i<=order; i++)
   {   p = pow(x,i)*coeff[i]; 
       sum = sum+p;
       }
       cout << " Y = " <<sum << endl;
return *this;} 

polynomial &polynomial::addition(polynomial p)
{  int ord, i;

   if (p.get_order()>order)
      {   
      for (i=p.get_order(); i>order; i--)
         { coeff[i] = p.get_coeff(i); 
           ord = order;
           order = p.get_order();
         }}
            else (ord = p.get_order());
   {     for (int i=ord; i>=0; i--)
        { coeff[i] += p.get_coeff(i);}
   }
return *this; }

polynomial &polynomial::subtraction(polynomial p)
{    polynomial s;      
     s = p.multiplier(-1);
     addition(s); 
     return *this; }

polynomial &polynomial::multiplier(double f)
 {
  int i;
  for(i=0;i<=order;i++)
  {coeff[i]*=f; }
  return *this; }   

polynomial &polynomial::multiply(polynomial p)
{
   int ord;
   polynomial q(*this);
   ord = q.get_order() + p.get_order();
   for (int i=0; i<=ord; i++)
   { coeff[i]=0; }
   for (int i=0; i<=q.get_order(); i++)
   { for (int j=0; j<=p.get_order(); j++)
    { coeff [i+j]+= (q.get_coeff(i)*p.get_coeff(j)) ;
      order = ord; }}
return *this; }

//MAIN PROGRAM
int main()
{   polynomial p1, p2, p3, p4;
    int x;

    cout <<" FIRST POLYNOMIAL " <<endl;
    p1.get_data();
    p1.display();

    cout <<endl;
    cout <<" SECOND POLYNOMIAL " <<endl;
    p2.get_data();
    p2.display();

    cout <<"Enter value X: ";
    cin >>x;
    p1.evaluate(x);
    p2.evaluate(x);

  p3 = p1.addition(p2);
  p3.display();

   p4= p1.subtraction(p2);
   p4.display();

    system ("pause");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.