- Polynomial Representation Using Linked List
- Polynomial Representation Using Array Programming
- Polynomial Representation Using Array Programs
- Array Representation Of Tree
- Polynomial Representation Using Array Programs
I'm looking for some assistance on an exercise for my C++ programming class. Unfortunately, I was rather ill this week and unable to attend class, meaning I have only been able to use the textbook as a resource, so I'm pretty lost when it comes to writing this program. Therefore, I figured I'd turn to the real pros here for assistance. I realize this is broad, but any help and/or tips would be appreciated. Here are the instructions we were given:
This assignment deals with representing and manipulating polynomials using simple arrays. A polynomial, such as anxn + an-1xn-1 + … + a0, will be implemented as an array of coefficients, with >coefficient ai being stored in location i of the array. The coefficients are floating point values (potentially negative), so we will use an array of type double. The array will be of size MAXPOLY (a constant variable set to 50) and so we will be limited to holding polynomials with maximum degree of MAXPOLY – 1 (or 49).
The file Poly.h describes all the functions provided by the class.
You are to implement the following set of functions:
- Default constructor to initialize a polynomial to the zero polynomial
- setCoeff to set a specific coefficient in the polynomial
- retrieveCoeff to get a specific coefficient from the polynomial
- incrementCoeff to add a value to a specific coefficient in the polynomial
- degree which determines the degree of the polynomial
- numOfTerms which determines the number of terms in the polynomial (i.e., how many array elements are nonzero)
- evaluate which evaluates the polynomial for a given value of X
- add which adds one polynomial to another, changing the polynomial added to
- derivative which computes the derivative of a polynomial
- equals which determines the equality of two polynomials
In this C program, we are reading the order of an array using ‘num’ variable and also the value of ‘x’ variable to multiply along with the coefficients. Enter the coefficients of the polynomial equation into an array using for loop.
Several functions are provided for you: (1) a toString function will be provided to you so that >all our polynomials will be displayed identically, (2) the insertion operator is defined so we can >easily print a polynomial, and (3) the equality, inequality, and addition operators are provided >and are simply defined in terms of your equals and add functions. You should not change any of the provided functions.
You will be supplied two starter files, Poly.cpp and Poly.h. The class declaration file Poly.h contains a complete specification of a class named Poly. Your task will be to implement >all the specified functions in the class definition file Poly.cpp (with the exception of the few >functions which have been provided for you.). You have also been provided with an initial test >program PolyTest.cpp. You should add code to the PolyTest.cpp file to fully test your Poly class(copy code from the PolyTest.cpp file you created for Project #1-Pre).
We were, indeed, supplied those files. The Poly.h file looks like this:
The Poly.cpp file looks like this:
Although I have a basic understanding of C++, this is only the second week of classes (apparently a bad one to miss), so I am still in the learning stages. Any help, even if it is just a place to start, would be greatly appreciated. Thank you!
Note: I am compiling in Microsoft Visual Studio if that is of any help
1 Answer
Couple of things with your posted code.
You need to switch the following lines in
Poly.h
:Otherwise, nothing from the file gets included.
It's a bad practice to use
in a .h file. Use explicit type names, such as
std::string
andstd::ostream
.
Coming to your main obstacle, you have to figure out how to implement the functions in Poly.cpp
. You can use a test driven approach to flesh out the contents of the file.
Let's say you have a file named TestPoly.cpp
. The file contains the main
function and drives testing of the implementation of Poly
.
You can start with:
How would you implement testSetCoeff
?
Here's something to start off:
The strategy followed in the function:
- Set some data on an object.
- Retrieve the data from the same object.
- Make sure that you get back a value that makes sense. Add an appropriate test for it.
In the function above, I chose to use
instead of
to make sure that we are able to deal with the inexact nature of floating point representations.
The implementation of almostEqual
is something like:
Putting these all together, the content of the starter version of TestPoly.cc
will be:
With the current state of Poly.cpp
, you will get FAILURE
status. Now you can go to Poly.cpp
and figure out how to change the implementations of setCoeff
and retrieveCoeff
to make that test pass.
Then, you can start adding other tests. They will most likely fail first. Then you implement the necessary functions until those tests pass.
Polynomial Representation Using Linked List
Update, in response to OP's comment
The coefficients can be initialized to 0
in the constructor using memset
.
Polynomial Representation Using Array Programming
P.S.: Remember to #include cstring
to use memset
.