Help-New Student Trying Hard

B

beachlounger

Using C++ language to complete this homework

1. Develop class Polynomial. The internal representation of a
Polynomial is an array of terms. Each term contains a coefficient and
an exponent. The term

2x4
has the coefficient 2 and the exponent 4. Develop a complete class
containing proper constructor functions as well as set and get
functions. The class should also provide the following overloaded
operator capabilities:

(a) Overload the addition operator (+) to add two Polynomials.
(b) Overload the multiplication operator (*) to multiply two
Polynomials.
(c) Overload the subtraction assignment operator (=) to process two
Polynomials.

This is what I have so far


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


class Polynomial {
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const; // addition
Polynomial operator-( const Polynomial& ) const; // subtraction
Polynomial operator*( const Polynomial& ); // multiplication
const Polynomial operator=( const Polynomial&);// assignment
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
Polynomial& operator*=( const Polynomial& );
void enterTerms();
void printPolynomial() const;
private:
int exponents[ 100 ]; // exponent array
int coefficients[ 100 ]; // coefficients array
void polynomialCombine( Polynomial& ); // combine common terms
}; // end class Polynomial


#endif


#include <iostream>

using std::cout;
using std::endl;

#include "polynomial.h"


int main()
{
Polynomial a, b, c, t;

a.enterTerms();
b.enterTerms();
t = a; // save the value of a
cout << "First polynomial is:\n";
a.printPolynomial();
cout << "Second polynomial is:\n";
b.printPolynomial();
cout << "\nAdding the polynomials yields:\n";
c = a + b;
c.printPolynomial();
cout << "\n+= the polynomials yields:\n";
a += b;
a.printPolynomial();
cout << "\nSubtracting the polynomials yields:\n";
a = t; // reset a to original value
c = a - b;
c.printPolynomial();
cout << "\n-= the polynomials yields:\n";
a -= b;
a.printPolynomial();
cout << "\nMultiplying the polynomials yields:\n";
a = t; // reset a to original value
c = a * b;
c.printPolynomial();
cout << "\n*= the polynomials yields:\n";
a *= b;
a.printPolynomial();
cout << endl;
return 0;
} // end main

My error is c:\documents and settings\owner\connie marshall's week 7
homework.cpp(32) : fatal error C1083: Cannot open include file:
'polynomial.h': No such file or directory
Please help!!!!!!
 
F

Frederick Gotham

posted:
Using C++ language to complete this homework

1. Develop class Polynomial. The internal representation of a
Polynomial is an array of terms. Each term contains a coefficient and
an exponent.


Well here's one way of going about it. Perhaps firstly set the maximum
exponent:

enum { EXPONENT_MAX = 100 };

Then maybe create a simple structure like this:

struct CoAndExp {
unsigned co_max;
unsigned exp_max;
};

Then maybe place such an array inside your class:

class Polynomial {
private:

struct CoAndExp {
unsigned co_max;
unsigned exp_max;
};

CoAndExp terms[EXPONENT_MAX];

Polynomial() : terms() {}
};

class Polynomial {
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const; // addition
Polynomial operator-( const Polynomial& ) const; // subtraction
Polynomial operator*( const Polynomial& ); // multiplication


Depending on whether you want the built-in types to be able to implicitly
convert to a Polynomial, I would take these outside the class:

Polynomial operator+(Polynomial lhs, Polynomial const &rhs)
{
return lhs += rhs;
}

My error is c:\documents and settings\owner\connie marshall's week 7
homework.cpp(32) : fatal error C1083: Cannot open include file:
'polynomial.h': No such file or directory


Nothing magical here -- the file doesn't exist.

Try saving the file as "polynomial.h" and try again.

(One thing, I would advocate that you use ".hpp" rather than ".h" -- we're
dealing with C++, not C.)
 
S

Scott McPhillips [MVP]

My error is c:\documents and settings\owner\connie marshall's week 7
homework.cpp(32) : fatal error C1083: Cannot open include file:
'polynomial.h': No such file or directory
Please help!!!!!!

Does this h file exist? Is it in the same directory as the cpp file that
does the #include ?
 
B

Bernd Strieder

Hello,

Using C++ language to complete this homework

1. Develop class Polynomial. The internal representation of a
Polynomial is an array of terms. Each term contains a coefficient and
an exponent. The term

2x4
has the coefficient 2 and the exponent 4. Develop a complete class
containing proper constructor functions as well as set and get
functions. The class should also provide the following overloaded
operator capabilities:

(a) Overload the addition operator (+) to add two Polynomials.
(b) Overload the multiplication operator (*) to multiply two
Polynomials.
(c) Overload the subtraction assignment operator (?=) to process two
Polynomials.

Think about the mathematical structure. A univariate polynomial p looks
like p(x)=sum c_i x^i. Basically the sequence of the c_i is enough to
characterize it. Let there be a maximal i for every polynomial with
non-zero c_i. Now there are two observations possible.

The first is that polynomials form a module, or a vector space if the
coefficients are from a field. The vectors have a maximal index with
the corresponding entry non-zero. All coefficients up to this index are
stored, all others not.

The second observation is, that a sequence is a mapping from the natural
numbers to the coefficients, and zero coefficients are the default, so
you need to map only those finitely many exponents with non-zero
coefficient.

If you look through the tools the C++ standard library offers to you,
you will find vector and map. If you employ vector in the most direct
way, you will get something called dense polynomials, in the other case
with map you will get sparse polynomials. When dealing with polynomials
the selection between those two has to be done carefully to get
efficient solutions to the problem in question.

Now, for addition of polynomials, you have to add the corresponding
coefficients. You will have to create the result polynomial, traverse
both data structures of the corresponding polynomials and collect the
result. Subtraction is not much different. Multiplication is only a
little bit more involved. Look for formal definitions of the product of
polynomials or folding of sequences.

In the sparse case you will find a close similarity of addition and
subtraction of polynomials to the problem of merging two sorted
seqences. Look for mergesort to get the idea.
This is what I have so far


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

Everything within this #ifndef ... #endif should be in polynomial.h.
class Polynomial {
public:
Polynomial();
Polynomial operator+( const Polynomial& ) const; // addition
Polynomial operator-( const Polynomial& ) const; // subtraction
Polynomial operator*( const Polynomial& ); // multiplication
const Polynomial operator=( const Polynomial&);// assignment
Polynomial& operator+=( const Polynomial& );
Polynomial& operator-=( const Polynomial& );
Polynomial& operator*=( const Polynomial& );
void enterTerms();
void printPolynomial() const;
private:
int exponents[ 100 ]; // exponent array
int coefficients[ 100 ]; // coefficients array

Those arrays might be usable as dense and as sparse polynomials. What
special restrictions would the 100 imply, if you use them with dense
and with sparse in mind? If you use them as dense, what would the
content of exponents be in every case.
void polynomialCombine( Polynomial& ); // combine common terms
}; // end class Polynomial

And the bodies of those member functions of Polynomial are defined
where? polynomial.cc?

You could write all the code into a single file without including other
code you wrote, but this is bad style and prevents reuse. It should be
explained in textbooks where to put what, and there or in
compiler/linker manuals how to compile and link the parts together. You
could perhaps look for the C++ FAQ.

Bernd Strieder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top