0

all. I'm trying to set up a program that evaluates a polynomial depending on the user input of X. Another part of the program I want is to then add these polynomials together. I'm using a 2D array to do this. What do you think would be the best way to write the evaluation function. Been working at this for hours and I'm still not quite sure how to do it. Thanks in advance.

polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
using namespace std;

#define MAX 100

class Polynomial {
      friend ostream &operator<< (ostream &, const Polynomial &);
      public :
             Polynomial ();
             void enterTerms();
             int evaluate(Polynomial p, int x);
             Polynomial operator +(const Polynomial & );
      private :
             int terms[MAX][2]; //either static size(MAX rows) or use "new" for dynamic allocation
             int n; //number of terms
};             

#endif

polynomial.cpp

#include "polynomial.h"

using namespace std;

ostream &operator<< (ostream & out, const Polynomial & p){
        for ( int i = 0 ; i < p.n ; i++ ){
            if ( i == p.n - 1 )//last term does not have + appended
                  out << p.terms[i][0] <<"x^"<<p.terms[i][1]<<endl;
            else
                  out << p.terms[i][0]<<"x^"<<p.terms[i][1]<<" + ";
        }
        return out;
}
Polynomial :: Polynomial(){
         for ( int i = 0; i < MAX; i++ ){
             terms[i][0] = 0;
             terms[i][1] = 0;  
         }           
}       
void Polynomial :: enterTerms(){//enterTerms() not in constructor so that no prompt for entering 
//terms while doing + - etc., they also produce Poylnomial instance (i.e. invoke constructor)
      int num;
      cout<<"enter number of terms in polynomial\n";
      cin >> num;
      n = num >= 0 ? num : 1;
      cout << "enter coefficient followed by exponent for each term in polynomial\n";
      for ( int i = 0; i < n ; i++)  
              cin >> terms[i][0] >> terms[i][1] ;
}
Polynomial Polynomial :: operator + ( const Polynomial & p ){
           Polynomial temp, sum;
           temp.n = n + p.n;
           int common = 0;

          // first write sum as concatenation of p1 and p2               
           for ( int i = 0 ; i < n ; i++ ){ 
                   temp.terms[i][0] = terms[i][0];
                   temp.terms[i][1] = terms[i][1];
           }
           //notice j and k for traversing second half of sum, and whole p2 resp
           for ( int j = n, k = 0; j < n + p.n, k < p.n ; j++, k++ ){  
                   temp.terms[j][0] = p.terms[k][0];
                   temp.terms[j][1] = p.terms[k][1];
           }
           for ( int l = 0; l < temp.n - 1 ; l++ ){ // 0 to 1 less than length
               for ( int m = l + 1 ; m < temp.n ; m++ ){ // 1 more than l to length,so that compared pairs are non redundant
                   if( temp.terms[l][1] == temp.terms[m][1] ){
                           common++;   //common terms reduce no. of terms in sum (see sum.n decl)
                           temp.terms[l][0] +=  temp.terms[m][0]; //coefficients added if exponents same
                           temp.terms[m][0] = 0;                
                   }  
               }
           } 
           sum.n = temp.n - common;  //if you place it above, common taken as 0 and sum.n is same as temp.n (logical error)         

           //just to debug, print temporary array
           cout << endl << temp;  

           for ( int q = 0, r = 0; q < temp.n; q++ ){
                    if ( temp.terms[q][0] == 0 )
                       continue;
                    else{
                         sum.terms[r][0] = temp.terms[q][0];
                         sum.terms[r][1] = temp.terms[q][1];
                         r++;
                    }
           }

           cout << endl << sum;
           return sum;
       }

int Polynomial :: evaluate(Polynomial p, int x)
{
    Polynomial terms;
    return 0;
}



int main()
{

    Polynomial p1 , p2;
    p1.enterTerms();
    p2.enterTerms();
    cout << "Please enter the value of x:" << endl;
    cin >> x;
    //evaluate(p1);
    //evaluate(p2);
    p1 + p2;
    system("PAUSE");
    //cin.get();
    return 1;
}
Amelia
  • 1
  • 1
  • 1
    Your code will likely be simpler if you store the coefficients only, and rely on the position in the array to determine the exponent. So that `terms[0]` is always just the constant term, and `terms[5]` is always the coefficient of `x^5`. – Arunas Oct 10 '13 at 15:37

2 Answers2

3

Please consider a simpler data structure. A common approach is to use a single array, where the index is the power of x. Just use zeros where no such term exists. Then x^3 + 2*x + 1 is written {1, 2, 0, 1}, since there's no x^2. Also note the reverse order, since p[0] represents x^0. This drastically simplifies operations like addition.

As far as evaluation, just think about the equation. If your polynomial is x^2 + 3*x + 5, and you want to evaluate for x=7, what do you do? Start with power 0, and accumulate each term into a single variable.

Peter
  • 14,559
  • 35
  • 55
0

You can follow and complete my functions here:

float polyval_point(Eigen::VectorXf v,float x)
{
    float s = 0;
    for (int i=0;i<v.size();i++)
    {
    s += v[i] * pow(x,i);
    }
    return s;
}

Eigen::VectorXf polyval_vector(Eigen::VectorXf v,Eigen::VectorXf X)
{
    Eigen::VectorXf S(X.size());
    for (int i=0;i<X.size();i++)
    {
    S[i] = polyval_point(v,X[i]);
    }
    return S;
}
NKN
  • 6,482
  • 6
  • 36
  • 55