Loading...

Forum Beginners Windows Programming UNIX/Linux Programming General C++ Programming Lounge Jobs

Work in HCI research?

Go

Beginners

ok

Not logged in

Help with Polynomial assignment

register

log in

Work in HCI research? Help with Polynomial assignment dhoff024 (4)

Mar 31, 2011 at 8:52am

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. Thanks in advance!

hamsterman (4538)

Mar 31, 2011 at 12:50pm

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} edit: typo Last edited on Apr 1, 2011 at 8:21am closed account (D80DSL3A)

Mar 31, 2011 at 3:44pm

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! I can help with the framework.

dhoff024 (4)

Apr 1, 2011 at 1:44am

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!

dhoff024 (4)

Apr 1, 2011 at 1:49am

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!

Mathhead200 (1008)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Apr 1, 2011 at 2:11am

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 = new double[degree + 1]; coefficients[0] = 0; } //You must define the rest of the functions Last edited on Apr 1, 2011 at 2:13am

dhoff024 (4)

Apr 5, 2011 at 8:45am

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.

Mathhead200 (1008)

Apr 6, 2011 at 1:03am

1. Do you know what a class is? 2. Do you know what a polynomial is? Example: 1 ax2 + bx + c // is a standard form quadratic (2nd degree) polynomial 2 2 1 0 3 = ax + bx + cx // meens the same thing 4 // so this polynomial could be defined (in c++) with an array, like so... 5 array: { c, b, a } // the coefficients indexes: [0][1][2] // their place (i.e. xn, where n is the index) Last edited on Apr 6, 2011 at 1:03am rana110 (6)

Apr 6, 2011 at 11:09pm

ohhh.. I have the same homework in my class...If I understood any thing I'll help you ...I hate homeworks

rana110 (6)

Apr 9, 2011 at 9:41pm

is my code right or not??

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include

double Polynomial::getCoefficient() const { return Coefficient; } // end function getCoefficient

void Polynomial::setExponent(int val) { exponent = val; } // end function setExponent

void Polynomial::setCoefficient(double val) { coefficient= val; } // end function setCoefficient

Polynomial Polynomial::add(Polynomial num) const { Polynomial result( ); return result; }

Mathhead200 (1008)

Apr 10, 2011 at 12:46am

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

rana110 (6)

Apr 10, 2011 at 11:58am

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

Mathhead200 (1008)

Apr 10, 2011 at 1:43pm

Let's find the key phrases: the exponent is a non-negative integer the coefficient is a real number A term is a pair (exponent, coefficient) A polynomial is a sequence of terms 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 (2 nd 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. Last edited on Apr 10, 2011 at 1:44pm rana110 (6)

Apr 12, 2011 at 12:19am

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?

Mathhead200 (1008)

Apr 12, 2011 at 12:28am

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

closed account (D80DSL3A)

Apr 12, 2011 at 12:54am

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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#include

Edit & Run

class Polynomial { private: int Nterms; double* pCoeffs;// from lowest to highest order public: // functions double evaluateAt(double x); void print(void);

// constructor Polynomial( double Coeffs[], int N_terms );// full construction from given array of coefficients // destructor ~Polynomial();// destructor VERY important this case }; // full constructor. Must be passed an array of coeffs. and the array size. Polynomial::Polynomial( double Coeffs[], int N_terms ) { Nterms = N_terms; pCoeffs = new double[ Nterms ];// allocate an array to hold the coefficient values for(int i=0; i

rana110 (6)

Apr 14, 2011 at 1:28am

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

closed account (D80DSL3A)

Apr 14, 2011 at 1:49am

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. Last edited on Apr 14, 2011 at 1:52am

Topic archived. No new replies allowed.

Work in HCI research? Home page | Privacy policy © cplusplus.com, 2000-2017 - All rights reserved - v3.1 Spotted an error? contact us

Loading...