Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 11th, 2006, 02:05 AM
beachlounger@aol.com
Guest
 
Posts: n/a
Default Help-New Student Trying Hard

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!!!!!!

  #2  
Old August 11th, 2006, 02:45 AM
Frederick Gotham
Guest
 
Posts: n/a
Default Re: Help-New Student Trying Hard

posted:
Quote:
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() {}
};

Quote:
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;
}

Quote:
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.)

--

Frederick Gotham
  #3  
Old August 11th, 2006, 02:45 AM
Scott McPhillips [MVP]
Guest
 
Posts: n/a
Default Re: Help-New Student Trying Hard

beachlounger@aol.com wrote:
Quote:
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 ?

--
Scott McPhillips [VC++ MVP]

  #4  
Old August 11th, 2006, 02:55 PM
Bernd Strieder
Guest
 
Posts: n/a
Default Re: Help-New Student Trying Hard

Hello,

beachlounger@aol.com wrote:
Quote:
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.
Quote:
>
This is what I have so far
>
>
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
Everything within this #ifndef ... #endif should be in polynomial.h.
Quote:
>
>
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.
Quote:
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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles