Connecting Tech Pros Worldwide Help | Site Map

Polynomial class c++

Newbie
 
Join Date: Nov 2008
Posts: 28
#1: Nov 16 '08
im making a polynomial class, operator / is working while the operator /= overloaded function is not working.Someone pleasssssssssssssseeeee help out. ive to submit it next week.

Expand|Select|Wrap|Line Numbers
  1.     int degree;
  2.     double ab[100];
  3.     char dummy;
  4.  
  5. /////////////////////////////Division////////
  6.  
  7.    Poly  Poly::operator/(Poly& a)
  8.    {
  9.        Poly temp;
  10.        double result[20],temp1[20],temp2[20],temp3[20];
  11.        int count=0,cnt,i=0,j=0,k=0;
  12.  
  13.        for(i=0 ; i<=20 ; i++)
  14.        {
  15.            result[i]=0;temp1[i]=0;temp2[i]=0;temp3[i]=0;
  16.  
  17.        }
  18.  
  19.        if(degree>=a.degree)
  20.        {
  21.            for(i=degree; i>=0 ; i--)
  22.            temp1[i]=ab[i]; 
  23.            for(i=a.degree-1 ; i>=0 ;i--)
  24.            temp2[i]=a.ab[i]*-1; 
  25.  
  26.  
  27.            for(i=degree ; i>=a.degree ; i--)
  28.            {
  29.                cnt=0;
  30.  
  31.              result[count]=temp1[i];
  32.  
  33.                for(j=a.degree-1 ; j>=0 ; j--)
  34.                {
  35.                    temp3[j]=temp2[j]*temp1[i];
  36.                }
  37.  
  38.                for(k=i-1 ; k>=0 ; k--)
  39.                {
  40.                    if(a.degree-1-cnt>=0)
  41.                    {
  42.                        temp1[k]+=temp3[a.degree-1-cnt];
  43.                        cnt++;
  44.                    }
  45.  
  46.                }
  47.  
  48.                count++;
  49.  
  50.            }
  51.              count-=1;
  52.  
  53.            for(i=count ; i>=0 ; i--)
  54.                temp.ab[count-i]=result[i];
  55.            temp.degree=count;
  56.            temp.dummy=dummy;
  57.        }
  58.  
  59.        else
  60.            cout<<" invalid input "<<"\n";
  61.  
  62.        return temp;
  63.    }
  64.  
  65.  
  66. //////////not working
  67.  
  68.      Poly&  Poly::operator/=(Poly& a)
  69.    {
  70.        double result[20],temp1[20],temp2[20],temp3[20];
  71.        int count=0,cnt,i=0,j=0,k=0;
  72.  
  73.        for(i=0 ; i<=20 ; i++)
  74.        {
  75.            result[i]=0;temp1[i]=0;temp2[i]=0;temp3[i]=0;
  76.  
  77.        }
  78.  
  79.  
  80.        if(degree>=a.degree)
  81.        {
  82.  
  83.            for(i=degree; i>=0 ; i--)
  84.            temp1[i]=ab[i]; 
  85.            for(i=a.degree-1 ; i>=0 ;i--)
  86.            temp2[i]=a.ab[i]*-1; 
  87.  
  88.  
  89.            for(i=degree ; i>=a.degree ; i--)
  90.            {
  91.                cnt=0;
  92.  
  93.              result[count]=temp1[i];
  94.  
  95.                for(j=a.degree-1 ; j>=0 ; j--)
  96.                {
  97.                    temp3[j]=temp2[j]*temp1[i];
  98.                }
  99.  
  100.                for(k=i-1 ; k>=0 ; k--)
  101.                {
  102.                    if(a.degree-1-cnt>=0)
  103.                    {
  104.                        temp1[k]+=temp3[a.degree-1-cnt];
  105.                        cnt++;
  106.                    }
  107.  
  108.                }
  109.  
  110.                count++;
  111.  
  112.            } count--;
  113.  
  114.               this->degree=count;
  115.  
  116.            for(i=degree ; i>=0 ; i--)
  117.             this->ab[degree-i]=result[i];
  118.  
  119.            cout<<"done";
  120.  
  121.  
  122.        }
  123.  
  124.        else
  125.            cout<<" invalid input "<<"\n";
  126.  
  127.        return *this;
  128.   }
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Nov 16 '08

re: Polynomial class c++


If your Poly::operator/() is working then why not just:
Expand|Select|Wrap|Line Numbers
  1. Poly& Poly::operator/=(Poly& a)
  2. {
  3.      *this = *this / a;
  4.      return *this;
  5. }
  6.  
?
Newbie
 
Join Date: Nov 2008
Posts: 28
#3: Nov 17 '08

re: Polynomial class c++


Quote:

Originally Posted by weaknessforcats

If your Poly::operator/() is working then why not just:

Expand|Select|Wrap|Line Numbers
  1. Poly& Poly::operator/=(Poly& a)
  2. {
  3.      *this = *this / a;
  4.      return *this;
  5. }
  6.  
?



u r genius.......oooohhhh how stupid of me.....its working now....thanks a lot
a lot....im so happy......thanksss
but still why that function /= isn't working ??? i jst copy paste that / code and change the assignment of result to temporary object to this pointer...
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#4: Nov 17 '08

re: Polynomial class c++


Perhaps in your /= operator you are using this as if it was the original polynomial after you have modified it.
Newbie
 
Join Date: Nov 2008
Posts: 28
#5: Nov 17 '08

re: Polynomial class c++


Quote:

Originally Posted by boxfish

Perhaps in your /= operator you are using this as if it was the original polynomial after you have modified it.


but ive used this pointer when all the calculations are done....at the end before ending if ive changed the the original attributes it should work
Reply