473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Polynomial class c++

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.   }
Nov 16 '08 #1
4 5000
weaknessforcats
9,208 Expert Mod 8TB
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.  
?
Nov 16 '08 #2
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...
Nov 17 '08 #3
boxfish
469 Expert 256MB
Perhaps in your /= operator you are using this as if it was the original polynomial after you have modified it.
Nov 17 '08 #4
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
Nov 17 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root()...
9
by: strotee76 | last post by:
What do I need to do to setTerm() for it to work properly? If you notice any other glaring issues, then please let me know. With this header file: ======================= #ifndef Polynomial_h...
1
by: Rubén Campos | last post by:
I've trying to implement polynomials of arbitrary order as a C++ template, as shown here: template <unsigned long int N> class Polynomial { public: Polynomial (); ~Polynomial ();
2
by: Chen L. | last post by:
Hi all, If I have a 32-bit data M, and the CRC genrator polynomial G(x),which power is 32, then I can get the CRC checkword R by the following algorithm: X^32*M(x) = Q(x)G(x) + R(x), But how...
14
by: Tiza Naziri | last post by:
Hi, Anybody have an idea on how to start writing a C code for generating the inverse of finite field GF(2^8) using extended Euclidean algorithm? What I mean is how to represent a polynomial,...
12
by: daniel.wolff | last post by:
I am looking for a quick C program that takes n+1 pairs of values (integers) (a_i, f(a_i)), i=0,...,n, generates the coefficients \alpha_i, i=0,...,n of the polynomial of degree n that fits these...
11
by: Josiah Manson | last post by:
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am...
1
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.