The Program has been started but I am having issues finishing the public class in the header and finishing the functions inside the main file.

Header File:

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>

/**
   This class implements a polynomial of the form
    c0 + c1 x + c2 x^2 + c3 x^3 + ...

   As a simplification, this class will be limited to polynomials
   of order 99 or less.  (The order of a polynomial is the largest
   power of x having a non-zero coefficient, or zero if the polynomial
   has all zero coefficients.) Note that adding polynomials
   together or scaling polynomials (multiplying by a constant) could
   reduce the order of the result.

*/




class Polynomial {
public:
  
  Polynomial ();


  // Create a polynomial with nCoeff coefficients,
  // drawn from the coeff array.
  //
  // E.g.,
     //double c[3] = {1.0, 2.0, 3.0};
     //Polynomial p (3, c);
  // creates a polynomial p representing: 1.0 + 2.0*x + 3.0*x^2
  //
  Polynomial (int nCoeff, double coeff[]);


  // Create a polynomial with nCoeff coefficients,
  // all set equal to c.
  //
  // E.g.,
     //Polynomial p (3, 1.0);
  // creates a polynomial p representing: 1.0 + 1.0*x + 1.0*x^2
  //
  Polynomial (int nCoeff, double c);


  // Compute the value of this polynomial at a given value of x.
  // E.g.,
     //double c[3] = {1.0, 2.0, 3.0};
     //Polynomial p (3, c);
     //cout << p.evaluate(2.0);
  // will print 17.0
  double evaluate (double x) const;

  // The order of a polynomial is the largest exponent of x with a
  // non-zero coefficient. E.g., for the sample polynomial used
  // above, the order is 2.
  int getOrder() const;

  // Add two polynomials, returning the polynomial representing
  // their sum.
  Polynomial operator+ (const Polynomial& p) const;

  // Multiply a polynomial by a floating point value.
  Polynomial operator* (double scale) const;

private:
  int order;
  double* coefficients; // array of coefficients.
                        //  Positions [0..order] are significant
                        //   All positions > order are ignored and
                        //   assumed to be zero.

  friend std::istream& operator>> (std::istream&, Polynomial&);
  friend std::ostream& operator<< (std::ostream&, const Polynomial&);

  // Internal utility function - coputes and remembers the order
  // of a polynomial. Use this at the end of any function that can change
  // the coefficients of the polynomial.
  void checkOrder();

};

// Read a polynomial from an istream.
//   Polynomials are input in the format:
//    #coefficients c0 c1 ...
//   where the ci are floating point coefficients of x^i
std::istream& operator>> (std::istream&, Polynomial&);

// Write a polynomial to an ostream
std::ostream& operator<< (std::ostream&, const Polynomial&);


inline
Polynomial operator* (double scale, const Polynomial& p)
{ return p * scale; }

/* Note: a member function of the Polynomial class must have a polynomial
   as the first argument (e.g., poly.operator*(x) <===> poly * x). This
   function simply allows for the multiplication to be written with the
   polynomial on the right. */

#endif

Main File:

#include "polynomial.h"

using namespace std;


Polynomial::Polynomial ()
  : order(0)
{
  coefficients = new double[1];
  coefficients[0] = 0.0;
}

Polynomial::Polynomial (int nCoeff, double coeff[])
// code here
  : order(1)
{
  checkOrder();
}

Polynomial::Polynomial (int nCoeff, double c)
  : order(nCoeff-1)
{
  coefficients = new double[nCoeff];
  for (int i = 0; i < nCoeff; ++i)
    coefficients[i] = c;
  checkOrder();
}





void Polynomial::checkOrder ()
{
  while (order > 0 && coefficients[order] == 0.0)
    --order;
}

double Polynomial::evaluate (double x) const
{
  double sum = 0.0;
  for (int i = 0; i <= order; ++i)
    sum = x * sum + coefficients[order - i];
  return sum;
}

int Polynomial::getOrder() const
{
  return order;
}


Polynomial Polynomial::operator+ (const Polynomial& p) const
{
  //code here
  Polynomial result (1, 0.0);
  result.checkOrder();
  return result;
}


Polynomial Polynomial::operator* (double scale) const
{
  Polynomial result (*this);
  for (int i = 0; i <= order; ++i)
    result.coefficients[i] = scale * coefficients[i];
  return result;
}



std::istream& operator>> (std::istream& in, Polynomial& p)
{
  int r;
  in >> r;
  Polynomial result(r, 1.0);
  result.order = r - 1;
  for (int i = 0; i < r; ++i)
    in >> result.coefficients[i];
  result.checkOrder();
  p = result;
  return in;
}


std::ostream& operator<< (std::ostream& out, const Polynomial& p)
{
  for (int i = 0; i <= p.order; ++i)
    {
      if (i > 0)
	{
	  out << " + ";
	}
      out << p.coefficients[i];
      if (i > 0)
	{
	  out << " x";
	  if (i > 1)
	    {
	      out << "^" << i;
	    }
	}
    }
  return out;
}

"I'm having issues" ...

Hmm. That's not a very specific question.

Okay, I'm sorry. To be more specific I am not sure how to create the polynomial within the public class of the header file. And within the main file its not clear to me what needs to be done within the polynomial.

A few comments:

  • If you think of a polynomial as an ordered collection of coefficients, then it should be obvious how to store them in the class instance. Hint: Zero is a legal coefficient
  • Once you see how to store them, it should be obvious how to do various things to them
  • Why are you using arrays when std::vector is available to you?

Do I need to set nCoeff to a set of numbers then and go from there? And I guess I should have mentioned that the reasons I am using arrays is because I was given this program to test my program with.

#include <iostream>
#include <sstream>
#include <cassert>

#include "polynomial.h"

using namespace std;

int main (int argc, char** argv)
{
  cout << "Enter a polynomial.\nHow many coefficients will be in the first polynomial? " << flush;
  int nCoeff1;
  cin >> nCoeff1;

  cout << "\nEnter the coefficients, in order of increasing power of x:\n  " << flush;

  double coeff1 [nCoeff1];
  for (int i = 0; i < nCoeff1; ++i)
    cin >> coeff1[i];

  Polynomial p1 (nCoeff1, coeff1);

  double x;
  cout << "\nEnter a value at which to evaluate the polynomial: " << flush;
  cin >> x;

  cout << p1 << "\n  at x=" << x << " evaluates to " << p1.evaluate(x)
       << endl;


  cout << "\nEnter a second polynomial, in the same format\n(# of coefficients, then each coefficient in order of increasing power of x).\n  " << flush;
  Polynomial p2;
  cin >> p2;

  double scale;
  cout << "\nEnter a scaling factor (a floating point number): " << flush;
  cin >> scale;

  {
    Polynomial p3 = p1 * scale;

    cout << "\n\n(" << p1 << ") * "
	 << scale
	 << "  = " << p3 << endl;
  }

  {
    Polynomial p4 = p1 + p2;

    cout << "\n\n" << p1 << "\n   +\n"
	 << " (" << p2 << ")\n"
       << "  = " << p4 << endl;
  }

  {
    Polynomial p5 = p1 + scale * p2;

    cout << "\n\n" << p1 << "\n   +\n"
	 << scale << " * (" << p2 << ")\n"
	 << "  = " << p5 << endl;

    cout << "\nThis polynomial has order " << p5.getOrder()
	 << " and, at x=" << x << ", evaluates to " << p5.evaluate(x)
	 << endl;
  }

  return 0;
}

std::vector versus array: At line 71 you have double* coefficients; // array of coefficients. I suggest you should prefer a std::vector. Yes, you have to accept count and array as parameters, but you don't have to use them internally.

nCoeff as a set? No, you need an indexed collection. Such as std::vector, not a set. And I think you didn't mean the count of coefficients, but the collection of them.

Why prefer std::vector?

  • memory management is done for you
  • higher level API implies less coding, easier to understand and maintain
  • consists of high quality code that you didn't have to write

So would std:: double polynomial; do the tick then?

And as for the array of coefficients I went back and read my guidelines and that is how my Professor would like us to set it up instead of vectors, I suppose it would be to easy that way. He is trying to get us to prove our skills in ADT and mine are lacking apparently.

Well if the prof is teaching C++ as C, there's not much I can do about it... but you might consider asking him why. One of the major advantages of C++ is that you get all the expressive power for free. Not using it is pretty much like wrestling with one arm disabled.

There's no such thing as std::double.

Well I appreciate your time....I suppose he is just trying to make our jobs alot harder then need be.

I expect your prof has taught a lot of C and feels comfortable with C, somewhat less so with C++; or maybe he just hasn't fully understood the expressive power of C++ yet. Or, maybe he feels that to "get" C++ you have to start with C. That last is one side of a long discussion, and it is the side I disagree with. I taught myself C++, having learned C. It took me twice as long to "get" what C++ offered as it would have (in my opinion) if I had started out without the C background.

I strongly encourage you, if C++ is something you will be using other than for an elective, to get a good introductory book and read it in parallel with your classwork -- assuming you can find the time. :)

I have a book from my introductory courses and I will try and use that to get the programs that I need to get finished done. It is a little confusing to me but I am trying to work my way up to learning C++. Best way I have heard is to just practice.

Back to your question, though, you need to implement the Polynomial constructor taking arbitrary coefficients (inserting code in your "main file" before line 17) ... the code in the following variant of the constructor (lines 20-27) is already close.

You also need to implement polynomial addition in method operator+().

Finally, it looks like your operator<<() and operator>>() aren't parallel (you output a different format from the one you input), though that may be intentional.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.