Hi everyone this is my first post on here because I am having trouble with an assignment in my second level C++ class. I have taken 2 C++ classes and have had terrible teachers for both. I had one teacher who wasnt even a real teacher he was a student teacher that was teaching that class to get experience and now I have a teacher who doesnt explain anything just expects you to know it so I am really struggling and would appreciate any help that anyone could give me. Here is what my assignment is supposed to be it was given to me today and is due tomorrow before midnight.
Polynomials
Using dynamic arrays, implement a polynomial class with polynomial addition, subtraction, and multiplication.
Discussion: A variable in a polynomial does very little other than 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. One simple way to implement the polynomial class is to us an array of doubles to store the coefficients. The index of the array is the exponent of the corresponding term. Where is the term in x*x in the previous example? 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 polynomial 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 constructed. Also supply an overloaded operator = and a destructor. 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 am not asking people to do it for me but I am just wondering if people can basically teach me how to do it since I have absolutley not one clue how to. Maybe someone could gibe me a basis of code to go off of and explain what I have to do because I dont even know where to start.
Well, polynomial 3x3-2x+1 would in your program be represented as array {1, -2, 0, 3}. Note that I've put terms in reverse order so that nth element of the array would be the coefficient before xn.
Polynomial addition is just adding up the coefficients in the same positions. {1, 0, 1} + {2, 1} = {3, 1, 1}
Polynomial multiplication is a bit more complex. Consider A*B (capitals represent polynomials). For each coefficient of A, construct P by multiplying each coefficient of B by it. Then shift P by the position of that coefficient and add it to R. {1, 0, 3} * {2, 1} = 1*{2, 1} >> 0 + 0*{2, 1} >> 1 + 3*{2, 1} >> 2 = {2, 1} + 0 + {0, 0, 6, 3} = {2, 1, 6, 3}
Do you know the required methods fairly well? Can you work with dynamic arrays? How are you with operator overloading? Hamstermans explanations of how the operations could be carried out are good. Do you have anything yet?
Please say this assignment isn't due today!
Well basically i am not very good at anything programming like i said this is my second and final programming course i got a c in the first one and am struggling in this one and the teachers i have had have been jokes but thank you for the basis. Um no the project is due by midnight friday and its worth 100 points and i basically know nothing on how to do it so i figured id join to see if i could get some help. Thanks again!
Basically i need taught this stuff because i dont know it since my first class of the basics was taught by a guy who wasnt a teacher he was a student that was allowed to teach a basic class so i have never really learned this stuff you know. I am not trying to have ppl do my work but i have no clue so im willing to learn on the fly to get this done. Thanks again!
class Polynomial
{
private:
double *coefficients; //this will be the array where we store the coefficients
int degree; //this is the degree of the polynomial (i.e. one less then the length of the array of coefficients)
public:
Polynomial(); //the default constructor to initialize a polynomial equal to 0
Polynomial(double[], int); //the constructor to initialize a polynomial with the given coefficient array and degree
Polynomial(Polynomial&); //the copy constructor
Polynomial(double); //the constructor to initialize a polynomial equal to the given constant
~Polynomial() { delete coefficients; } //the deconstructor to clear up the allocated memory
//the operations to define for the Polynomial class
Polynomial operator+(Polynomial p) const;
Polynomial operator-(Polynomial p) const;
Polynomial operator*(Polynomial p) const;
};
//This is what the default constructor should look like
Polynomial::Polynomial() {
degree = 0;
coefficients = newdouble[degree + 1];
coefficients[0] = 0;
}
//You must define the rest of the functions
Thank you for the start on this assignment but where do I go from here it was due on friday but I dont know what to do so I am just turning it in late when I finally get it figured out. I am not trying to have people do it for me but I am totally lost with this class so I have no clue what I am doing with this stuff so this helps for a start but after this I dont know what to do.
Thanks again.
1. Do you know what a class is?
2. Do you know what a polynomial is?
Example:
1 2 3 4 5
ax2 + bx + c // is a standard form quadratic (2nd degree) polynomial
= ax2 + bx1 + cx0 // meens the same thing
// so this polynomial could be defined (in c++) with an array, like so...
array: { c, b, a } // the coefficients
indexes: [0][1][2] // their place (i.e. xn, where n is the index)
A polynomial has more then one coefficient rana110. And when you create an array of doubles for them, will not need an array of exponents because the indexes of the array are = to the exponents for that coefficient. (Unless I read your code wrong...)
thanks Mathhead200 for replying ,,,,to be honest I did'nt understand what you just said,,,,
but in my Assignment, the teacher told us that the exponent should be an integer and the coefficient should be double ,,, here's my Assignment :
We want to manipulate polynomials. A polynomial is a sequence of terms. A term is a pair (exponent, coefficient) where the exponent is a non-negative integer and the coefficient is a real number. For example, we want to handle the polynomial: 3.4 x3 - 1.2 x + 126
1- Implement the class Polynomial. The operations on polynomials are:
a. Add 2 polynomials
b. Multiply 2 polynomials
c. Print a polynomial
2- Test your class Polynomial and its methods.
is that what you were asking about ??
and I didn't finish the code above,,, but I wanted to see if my start is good or not ,,
Now let's translate that to C++:
- an exponent is type int (possibly unsigned)
- a coefficient is type double
- a Polynomial is an array of coefficients (exponents are the indexes)
An array is not only a list of values, but it also has indexes that span from 0 to n - 1 (int's >= 0). So for example, if we want to express a quadratic polynomial (2nd dgree polynomial) say "-2x2 + 2.5x - 3.14" in code we could expree it as the array { -3.14, 2.5, -2 } ( see above process http://cplusplus.com/forum/beginner/39735/#msg216664 )
I hope that helps. Ask if you don't understand something.
it was really helpfull ,, so now I'm thinking to make class "term" which : term((double coef, int expon)
and then I'll make:" polynomial" class and I'll inherits class "term" and use it in "polynomial" class to make the "Add" and the "multiply" ...am I thinking right or still not?
You can do it that way, but don't "inherit" Term, just store an array of them in Polynomial.
I don't think this is the easiest way to do this, but it should work fine. It may make the operations a bit more involved.
1 2 3 4 5 6 7 8 9 10 11 12
class Term {
int exponent;
double coefficient;
public:
...
};
class Polynomial {
Term terms[];
public:
...
};
I think that the Term class idea is heading in the wrong direction. The exponent is just the array index in the polynomial.
Perhaps a lot more help is justifiable in this case. rana110 is trying but doesn't appear to be getting anywhere.
Mathhead200 posted a great start some posts ago in this thread, see:
http://www.cplusplus.com/forum/beginner/39735/#msg214808
rana110, the assignment given by the OP in this thread appears to be much more involved than yours. Is this all you need to do (from your earlier post}?
1- Implement the class Polynomial. The operations on polynomials are:
a. Add 2 polynomials
b. Multiply 2 polynomials
c. Print a polynomial
2- Test your class Polynomial and its methods.
Or, do you need to have overloaded + and - operators with poly+poly, double+poly, poly+double, etc. as per the 1st post in this thread? Do you need an assignment operator (=)? A copy constructor? What is the complete scope of your assignment?
I have a Polynomial class already written, so I can readily help with the parts you need.
Here's a start on the basics until you reply regarding the detailed requirements:
wow !! thank you fun2code for replying ,,,it's a smart code I think I'll need a hundred years to be good at programing like you,,,,,, and my homework like what I post in previous message,,,any tips to get better at programing
You're welcome. I hope you can work off that example.
I think you get good at programming the same way you get to Carnegie hall.
I face a never ending road myself. Programming is deep, but if it wasn't it wouldn't be very interesting!
Post back if you have trouble with the adding or multiplying parts.