Hey everyone,
I have an assignment to work on and it's a bit confusing.
Here's the problem:

Using dynamic arrays, implement a polynomial calss with polynomial additions, subtraction, and multiplication.

Discussion: A variable in a polynomial does nothing but act as a placeholder for the coefficients. Hence, the only interesting thing about polynomials is the array of coefficients and the corresponding exponent. Think about the polynomial

x*x*x + x + 1

Where is the term in x*x? One simple way to implement the polynomial class is to use an array of doubles to store the coeficients. The index of the array is the exponent of the corresponding term. If a term is missing, then it simply has a zero coefficient. There are techniques for representing polynomials of high degree with many missing terms. These use so-called sparse matrix techniques. Unless you already know these techniques, or learn very quickly, don't use these techniques.

Provide a default constructor, a copy constructor, and a parameterized constructor that enables an arbitrary polynomial to be constrcuted.

Supply an overloaded operator = and a desctructor.

Provide these operations:
polynomial + polynomial, constant + polynomial, polynomial + constant,
polynomial - polynomial, constant - polynomial, polynomial - constant,
polynomial * polynomial, constant * polynomial, polynomial * constant

Supply functions to assign and extract coefficients, indexed by exponent.
Supply a function to evaluate the polynomial at a value of type double.
You should decide whether to implement these functions as members, friends, or standalone functions.

I don't want anyone to do the problem for me or anything; I put the quote here simply for reference.

I am, however, quite confused on where to start. I'm not sure what the problem is even asking me to do really. Could someone show me an example of what the program should output and maybe a little sudocode as well? It would help tremendously.

what is this doing:

// a * x^b
public Polynomial(int a, int b) {
coef = new int[b+1];
coef = a;
deg = degree();
}
// return the degree of this polynomial (0 for the zero polynomial)
public int degree() {
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef != 0) d = i;
return d;
}
Member Avatar for iamthwee

// return the degree of this polynomial (0 for the zero polynomial)
public int degree() {
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef != 0) d = i;
return d;
}


Basically goes through the poly and returns the highest exponent of x.

For example

[tex]x^2-3+5x^7[/tex]

would return 7

// a * x^b
public Polynomial(int a, int b) {
coef = new int[b+1];
coef = a;
deg = degree();
}

That's creating a dynamic poly array on the fly, you could initialise the poly array to some large arbitary number, 100 for example. That way you wouldn't have to worry about dynamic arrays. 100 would of course be the largest exponent of x in your poly class.

That way you wouldn't have to worry about dynamic arrays. 100 would of course be the largest exponent of x in your poly class.

we have to use dynamic arrays for this particular problem.


I'm still completely lost; could you provide an example of what would be input/output in a test run?

Oh my bad; I meant I am completely lost on what the problem is asking me to do. How should the polynomials be input?

Member Avatar for iamthwee

That I don't know. You'll have to ask your teacher.

Supply functions to assign and extract coefficients, indexed by exponent.

Is quite vague...

yeah good idea. He's pretty understanding about late work; and considering we haven't finished the chapter lectures, I think he'll give us a couple days longer.

thanks

Member Avatar for iamthwee

I mean you could speculate that you are given a string for example:

x^2+3x-7x^3

And from it you have to get out the coeffs and exponents.
{1 2, 3 1, -7 3 }

i.e.
Arranging the exponents in ascending order for clarity {coef exponent, ...}

{ 3 1, 1 2, -7 3 }

Or you might just be given { 3 1, 1 2, -7 3 } straight away. Or you might just have to do something like:-

Polynomial a;
a.set(1,5); //where 1 is the coef and 5 is the expo

There is no point coding an example until you clarify that with your teacher.

When you get to the operator overloading bit perhaps you might want to have a look at this:-
http://www.daniweb.com/forums/thread44082.html

Which is a trivial example of how a fraction class would be implemented.

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.