473,394 Members | 1,755 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,394 software developers and data experts.

Polynomials in C++

Hi all! I'm new to here .. so please excuse me if my questions are unclear etc :)

I am trying to store polynomials in C++ using linked lists.
My class is called Polynomial and i've created a structure that represents each term in the polynomial (a node in the linked list). A node is called 'term'.
However, I am having a problem visualizing how to link all these 'term's together to create a 'Polynomial' that i can actually call.
I can add terms, delete terms etc from a single list, but when it comes to dealing with a Polynomial object passed in to some function, I have problems breaking it into its 'term's.

In essence, its the constructor and actually building the list to call it a Polynomial object that is giving be problems.

I thought of making the Polynomial object point to the head of the list?
Does that make sense? Any suggestions from anyone :)?

~Thanks a lot.

-dicapryl
Nov 14 '06 #1
2 2465
macklin01
145 100+
Hi all! I'm new to here .. so please excuse me if my questions are unclear etc :)

I am trying to store polynomials in C++ using linked lists.
My class is called Polynomial and i've created a structure that represents each term in the polynomial (a node in the linked list). A node is called 'term'.
However, I am having a problem visualizing how to link all these 'term's together to create a 'Polynomial' that i can actually call.
I can add terms, delete terms etc from a single list, but when it comes to dealing with a Polynomial object passed in to some function, I have problems breaking it into its 'term's.

In essence, its the constructor and actually building the list to call it a Polynomial object that is giving be problems.

I thought of making the Polynomial object point to the head of the list?
Does that make sense? Any suggestions from anyone :)?

~Thanks a lot.

-dicapryl
Hi, and welcome!

In my opinion, linked lists aren't ideal for this. Really, you only need an array of coefficients, which puts them all adjacent in memory. I'd go with a class instead. Remember: all that's important for a polynomial are the coefficients, as well as how to evaluate it. Something like this:

Expand|Select|Wrap|Line Numbers
  1. class Polynomial
  2. {
  3.  private:
  4.   int degree;
  5.   double* coefficients;
  6.  public:
  7.   Polynomial();
  8.   ~Polynomial();
  9.   bool SetDegree( int NewDegree );
  10.   int GetDegree( void );
  11.   bool SetCoefficient( int term, double entry );
  12.   double GetCoefficient( int term );
  13.  
  14.   double Evaluate( double x );
  15.   bool display( void );
  16. };
  17.  
  18. Polynomial::Polynomial()
  19.  degree = 0;
  20.  coefficients = new double [degree+1];
  21.  for( int i=0; i <= degree ; i++ )
  22.  { coefficients[i] = 0; }
  23. }
  24.  
  25. Polynomial::~Polynomial()
  26. {
  27.  delete [] coefficients;
  28.  
  29. bool Polynomial::SetDegree( int NewDegree )
  30. {
  31.  delete [] coefficients;
  32.  degree = NewDegree;
  33.  coefficients = new double [degree+1];
  34.  for( int i=0; i <= degree ; i++ )
  35.  { coefficients[i] = 0; }
  36.  return true;
  37. }
  38.  
  39. int Polynomial::GetDegree( void );
  40. { return degree; }
  41.  
  42. bool Polynomial::SetCoefficient( int term, double entry )
  43. {
  44.  if( term > degree || term < 0 )
  45.  { return false; } 
  46.  coefficients[term] = entry;
  47.  return true;
  48. }
  49.  
  50. double Polynomial::GetCoefficient( int term )
  51. {
  52.  if( term > degree || term < 0 )
  53.  { return 0.0; } 
  54.  return coefficients[term];
  55. }
  56.  
  57. double Polynomial::Evaluate( double x )
  58. {
  59.  double output = 0.0; 
  60.  for( int i=degree ; i >= 0 ; i-- )
  61.  { 
  62.   output *= x;
  63.   output += coefficients[i];
  64.  }
  65.  return output;
  66. }
  67.  
  68. bool Polynomial::display( void )
  69. {
  70.  for( int i=0 ; i <= degree ; i++ )
  71.  {
  72.   cout << coefficients[i] << "x^" << i;
  73.   if( i % 5 == 0 )
  74.   { cout << endl; }
  75.   else
  76.   { cout << "\t"; }
  77.  }
  78.  cout << endl;
  79.  return true;
  80. }
  81.  
Nov 14 '06 #2
macklin01
145 100+
I had hoped to append this to my post, but ran out of time.

There should be a copy constructor, too, in the public: section:

Expand|Select|Wrap|Line Numbers
  1.  public:
  2.   ...
  3.  Polynomial( Polynomial& Input );
  4.   ...
  5. };
  6.  
  7. Polynomial::Polynomial( Polynomial& Input )
  8. {
  9.  degree = Input.GetDegree();
  10.  coefficients = new double [degree+1];
  11.  for( int i=0 ; i <= degree ; i++ )
  12.  { coefficients[i] = Input.GetCoefficient(i); }
  13. }
  14.  
You can then add operators to add and subtract polynomials, etc. Using the operator() for evaluation might be nice, too. e.g.,

Expand|Select|Wrap|Line Numbers
  1. double Polynomial::operator()( double x )
  2. {
  3.  return Evaluate(x);
  4. }
  5.  
All polynomial operations should be regarded as operations on coefficients, so it makes sense to store and manipulate the coefficients.

Please let me know if you have any problems here, as I just wrote this off the top of my head and haven't had a chance to test. (I'm only worried about the copy constructor. ;)) Thanks -- Paul

Expand|Select|Wrap|Line Numbers
  1. Sample use:
  2.  
  3. // Here's the polynomial 1 + 2x + x^2;
  4.  
  5. Polynomial P;
  6. P.SetDegree(2);
  7. P.SetCoefficient(0,1);
  8. P.SetCoefficient(1,2);
  9. P.SetCoefficient(2,1);
  10.  
  11. P.display();
  12.  
  13. cout << "P(1) should be 4: " << P(1) << endl;
  14.  
  15. // remind me what the coefficient of x is again:
  16.  
  17. cout << "coefficient of x: " << P.GetCoefficient(1) << endl;
  18.  
  19.  
Nov 14 '06 #3

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

Similar topics

3
by: Chris | last post by:
Does anyone know of a good standalone implementation of multivariable polynomials in python? Thanks, Chris
7
by: adrin | last post by:
hi, anyone knows how does an algorithm for calculating GCD of two polynomials look like ? please help me if you can, or give some relevant links :)
0
by: galathaea | last post by:
you will fall in love !! these are the cutest critters !! 3-ary polynomial: http://i10.tinypic.com/2hwlir6.png 4-ary polynomial: full http://i13.tinypic.com/47u0yfm.png
1
by: beck2 | last post by:
hello i am angela and i would like if its kind of anyone to help me by writing me the code of adding 2 oplynomials? polynomials should be entered as where 3,5 and 1 are coefficients of X to the...
4
by: Matthias | last post by:
how do i make a program that gets numbers from a user. and then stores it into an array. using polynomials if the user entered 7 5 4 3 2 the polynomial would be 7x^4+5x^3+4x^2+3x+2 the program...
7
by: yodadbl07 | last post by:
hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im...
2
by: manohara | last post by:
WAP a program to add 2 polynomials using a DLL
1
by: unknowncute | last post by:
can you help me make a code about division of polynomials using linked list? thnx!
1
by: Motanyane Tlotliso | last post by:
I wish someone to help me in creating a programm in C++ that can be able to sum two polynomials, subtract one from the other and multiply both polynomials together using a ADT's(struct term). I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.