Polynomial help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 4
Reputation: trac15 is an unknown quantity at this point 
Solved Threads: 0
trac15 trac15 is offline Offline
Newbie Poster

Polynomial help!

 
-1
  #1
Oct 4th, 2009
I am working on a project to solve a class in polynomial. I have successfully managed to do the header file and 75% of the code..but two functions to write and add the polynomials don't seem to work. Can someone help? The last part of the code!
  1. #include <cassert>
  2. #include <iostream>
  3. #include "poly.h"
  4.  
  5. using namespace std;
  6.  
  7. #define NULL 0
  8.  
  9. const unsigned int poly::SIZEINCR = 5;
  10.  
  11. poly::poly()
  12. {
  13.  
  14. variable = 'x';
  15. nterms = 0;
  16. terms = new term[MAXSIZE = SIZEINCR];
  17. };
  18.  
  19. void poly::copy (const poly & p)
  20. {
  21.  
  22.  
  23. variable = p.variable;
  24. nterms = p.nterms;
  25. terms = new term[MAXSIZE = p.MAXSIZE];
  26. for (unsigned int i=0; i<nterms; i++)
  27. terms[i] = p.terms[i];
  28. };
  29.  
  30. poly::poly (const poly & p)
  31. {
  32.  
  33. copy (p);
  34. };
  35.  
  36. void poly::free (void)
  37. {
  38.  
  39. nterms = 0;
  40. delete [] terms;
  41. terms = NULL;
  42. };
  43.  
  44. poly::~poly (void)
  45. {
  46.  
  47. free ();
  48. };
  49.  
  50. poly & poly::operator= (const poly & p)
  51. {
  52.  
  53. if (this != &p)
  54. {
  55. free ();
  56. copy (p);
  57. };
  58. return (*this);
  59. };
  60.  
  61. void poly::expand ()
  62. {
  63.  
  64. termptr p;
  65.  
  66. p = new term[MAXSIZE += SIZEINCR];
  67. for (unsigned int j=0; j<nterms; j++) p[j] = terms[j];
  68. delete [] terms;
  69. terms = p;
  70. };
  71.  
  72. void poly::InsertTerm (term t)
  73. {
  74.  
  75.  
  76. int i = nterms-1;
  77. unsigned int e = t.exp;
  78.  
  79. if (nterms == MAXSIZE) expand ();
  80.  
  81. nterms++;
  82. while ((i>=0) && (e > terms[i].exp))
  83. {
  84. terms[i+1] = terms[i];
  85. i--;
  86. };
  87.  
  88. assert ((i<0) || (e != terms[i].exp));
  89. terms[i+1] = t;
  90. };
  91.  
  92. void poly::read ()
  93. {
  94.  
  95.  
  96. poly temp;
  97. int coeff;
  98. int exp;
  99. cout << "Input a polynomial by specifying the variable and all terms in any order." << endl
  100. << "Each term is specified by an integer coefficient and" << endl
  101. << "a non-negative integer exponent." << endl
  102. << "Indicate END by specifying a dummy term with" << endl
  103. << "a zero coefficient and/or a negative exponent." << endl;
  104. cin >> temp.variable;
  105. do
  106. {
  107. cin >> coeff;
  108. if (coeff)
  109. {
  110. cin >> exp;
  111. if (exp >= 0)
  112. temp.InsertTerm (term(coeff, exp));
  113. }
  114. else
  115. while (cin && (cin.peek() != '\n')) cin. ignore();
  116. } while (coeff && (exp >= 0));
  117. *this = temp;
  118. };
  119.  
  120. void poly::write() const
  121. {
  122. // I need help!!!
  123. };
  124.  
  125. poly poly::plus (const poly & right) const
  126. {
  127.  
  128. };
  129.  
  130. poly poly::minus (const poly & right) const
  131. {
  132.  
  133. };
  134.  
  135. float poly::evaluate (float value) const
  136. {
  137.  
  138. };
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,705
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Polynomial help!

 
1
  #2
Oct 4th, 2009
I would insert each term in terms in ascending order based on the exponent so that the exp term with exponent 0 has index 0 in terms, etc. I would expand terms if the exponent of a term to be added to terms is above value of nterms. The default coefficient of each term in terms would be zero. Each term inserted into terms would appropriately adjust the value of the coeffient of the term with the same exponent.

To write in standard polynomial form with largest exponent first I would then start at the end of terms and loop backward. Each term could be output only in the value of the coefficient wasn't zero. The display format would be coefficient followed by variable followed by ^ sign followed by exponent without spaces between each of the values in the display.

To add two terms I would determine the polynomial with the smallest value of nterms and expand it to the same number of nterms as the larger one. Looping through each term in the polynomials and adding ( or subtracting as the case may be) the coefficients of terms with the same exponent will result in the coefficient of the term a third polynomial representing the sum.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: trac15 is an unknown quantity at this point 
Solved Threads: 0
trac15 trac15 is offline Offline
Newbie Poster

Re: Polynomial help!

 
0
  #3
Oct 4th, 2009
Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Polynomial help!

 
2
  #4
Oct 4th, 2009
Originally Posted by trac15 View Post
Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
Post poly.h and poly::write (). We have no idea what you might be doing wrong unless you post the code.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: trac15 is an unknown quantity at this point 
Solved Threads: 0
trac15 trac15 is offline Offline
Newbie Poster

Re: Polynomial help!

 
1
  #5
Oct 4th, 2009
this is the poly.h file
  1. class poly
  2. {
  3. public:
  4. poly();
  5. poly (const poly &);
  6. ~poly ();
  7. poly & operator= (const poly &);
  8. void read ();
  9. void write () const;
  10. poly plus (const poly &) const;
  11. poly minus (const poly &) const;
  12. float evaluate (float) const;
  13.  
  14. private:
  15. struct term
  16. {
  17. int coeff;
  18. unsigned int exp;
  19. term () {}; declaration.
  20. term (int c, unsigned int e) : coeff(c), exp(e) {};
  21. char sign () const {return ((coeff<0)?'-':((coeff==0)?'0':'+'));};
  22. };
  23. typedef term * termptr;
  24. static const unsigned int SIZEINCR;
  25. unsigned int MAXSIZE;
  26. unsigned int nterms;
  27. termptr terms;
  28. char variable;
  29.  
  30. void copy (const poly &);
  31. void free ();
  32. void InsertTerm (term);
  33. void expand ();
  34. };
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Polynomial help!

 
0
  #6
Oct 4th, 2009
Originally Posted by trac15 View Post
Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
The poly::write () function you posted in post 1 is empty. You're saying "it doesn't seem to work", so presumably it's no longer empty. Please post the updated poly::write () function.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: trac15 is an unknown quantity at this point 
Solved Threads: 0
trac15 trac15 is offline Offline
Newbie Poster

Re: Polynomial help!

 
0
  #7
Oct 4th, 2009
That is where i am working on! am not able to go further
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Polynomial help!

 
1
  #8
Oct 4th, 2009
Originally Posted by trac15 View Post
That is where i am working on! am not able to go further
Read Lerner's earlier post again. You want to loop through your terms[] array. For each element, print the sign, then print the coefficient, then print the variable symbol ('x'), then the '^' symbol, then the exponent.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Polynomial help!

 
0
  #9
Oct 5th, 2009
Maybe the following may be of some help:

http://www.daniweb.com/code/snippet217091.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC