Need help with polynomials assignment
-
QUESTION: Hi Zlatko, I'm having an assigned, it says to create a class for working with polynomials up to degree4. Five private data members are needed to store the coefficient for each degree of the polynomial, so 1, 2, 3, -4 ,5 corresponds to x^4 + 2x^3 + 3x^2 + 5 Now, the following methods should be included in your class: 1. default constructor that uses default arguments in case no initializers are included in main 2. add two polynomials and store the sum 3. multiply two quadratic polynomials and store the sum (A quadratic polynomial has degree 2 only) 4. Print a polynomial (so you can print out results of the addition and multiplications done). Also, your main should first instantiate two polynomials. The you should print out the two polynomials to be added together and its sum. Finally, instantiate and print out the two polynomials to be multiplied and its product. So I have now: class Polynomial { public: PolynomialDegree (int = 0, int = 0, int = 0, int = 0, int = 0); void calcSum; void calcDifference; void calcProduct; void printPolynomial; private: int c1, c2, c3, c4 This part, I believe should initialze the variables to 0 if no initializers are found in main and we have declared private the coefficients so it never can be changed. Now, I added this next: Polynomial :: PolynomialDegree(int t1, t2, t3, t4) { c1 = t1; c2 = t2; c3 = t3; c4 = t4; } Is it correct that this is just assigning values to the coefficients of the polynomials that we are looking at or what does it mean at all if this is correctly used? Next: To add two polynomials, I know you add like terms so coefficients corresponding to same degrees of two polynomials get added together. I was thinking you probably want to store coefficients of each polynomial into arrays and then just add the corresponding elements and print out result. This right or is there a simpler or better strategy for this? Finally, to multiply two quadratics, I know you have to multiply out the terms so for example, a(b+c) you'd get ab + ac. Now, again, I had a thought of just going to the arrays again and have each element multiply the other respective elements or was there a better/simpler way for this? As for printing out polynomials, that should be simple enough is it? Thank you very much. I'm sorry if this seems a little long but thanks for the help. Mike ANSWER: Hello Mike I think you have the right idea about how to do the assignment. You have a few issues with your class syntax, so lets start with that. Your class definition is basically correct, but the constructor name needs to match the class name and your methods need parameters and return values. Here I am assuming that when you add two polynomials together, the result goes into a new polynomial, so that the original polynomials are not changed. Here is how the class declaration should look. class Polynomial { public: Polynomial(int t1 = 0, int t2 = 0, int t3 = 0, int t4 = 0, int t5 = 0); Polynomial calcSum(const Polynomial& other) const; Polynomial calcDifference(const Polynomial& other) const; Polynomial calcProduct(const Polynomial& other) const; void printPolynomial(void) const; private: int c1, c2, c3, c4, c5; }; Notice you need 5 constants for degree 4 ax^4 + bx^3 + cx^2 + dx + e Lets look at the calc declarations in detail. Each is showing that it returns a Polynomial. Each is taking a Polynomial called other by reference but promising not to change it. That is why other is declared const. Finally the function itself is declared const (right at the end of the line) as a promise that the function will not change "this" instance of the Polynomial. All the consts and reference ideas are optional, especially for such a small class, so feel free to leave them out if you are not comfortable with them. Later on, when you are dealing with classes holding lots of data, or classes that should not be copied, the reference idea will be important. Your constructor definition is basically correct, except for the name. It should be Polynomial :: Polynomial(int t1, int t2, int t3, int t4, int t5) { c1 = t1; c2 = t2; c3 = t3; c4 = t4; c5 = t5; } That is all the constructor needs to do. Sorry, no shortcuts allowed, you must specify the datatype for each parameter. You can store the coefficients as c1,c2,c3,c4,c5 or you can store them in an array. The choice is yours. If storing as an array, you might have an easier time changing the class to hold higher order polynomials if you can find a general algorithms for some of your operations. For example, the addition and subtraction are particularly simple. Just have a loop to go through the array adding corresponding elements. With an array, you would not even have to touch the addition and subtraction code to handle higher degrees. Finally, don't forget what a quadratic is. The general form is P1: ax^2 + bx + c P2: dx^2 + ex + f When multiplying (ax^2 + bx + c)(dx^2 + ex + f) you need to multiply each element of P1 with each element of P2 and add like terms together. You should end up with an order 4 poly like this: (ad)x^4 + (ae+bd)x^3........ + cf I leave it for you to work out what the new coefficients should be. Remember that your calcProduct cannot handle polynomials of degree greater than 2, so if "this" or the "other" polynomial have non zero constants for the higher degrees, then you cannot do the operation and you should throw an exception. If you have not studied exceptions, then ask your instructor what method they would like to see. Probably an error message printed and some invalid result returned. Finally, consider renaming your coefficients from c1...c5 to c0...c4 so that the number after the 'c' matches the exponent of the polynomial variable. It will help keep things straight. Good luck with it. I think you'll do fine. ---------- FOLLOW-UP ---------- QUESTION: Hi Zlatko, Thank you for the help Just one question: when you have Polynomial calcSum(const Polynomial& other) const; in the class declaration, what does the &other part that you added in mean? but what is its purpose: to specify you are saving to a new polynomial? Does this mean I need do something specific elsewhere (like in the main), because it seems to be a reference parameter. Thanks once again, Mike const, I understand ANSWER: Hi Mike. The word "other" is the name i gave to the polynomial being passed into the calcSum method. Here is a sample main program which will make things clearer. int main(void) { Polynomial p1(1,2,3,4,5); Polynomial p2(6,7,8,9,10); // Here I am calling calcSum on p1 // This will add p1 + p2 and store the answer in result Polynomial result = p1.calcSum(p2); } We have 2 Polynomial instances p1, and p2. When you call calcSum on p1, the calcSum code runs on the instance of p1. The p2 is passed in as the other parameter. You can access the c1 data member of p1 by simply referring to c1, or you can use the "this" pointer by writing this->c1. You access the c1 of p2 by writing other.c1. So if you wanted to add the two together, you would say c1 + other.c1; or this->c1 + other.c1; Here is a sample implementation of the calcSum Polynomial Polynomial::calcSum(const Polynomial& other) { Polynomial result; // Do calculation result.c1 = c1 + other.c1; // and so on for the other constants return result; } The idea above is one way of defining the calcSum operation. Another way is to define it to add p2 to p1, so that when the call p1.calcSum(p2) is made, p2 is actually added to p1 and p1 changes. In that case no result needs to be returned. The result is stored as a change to p1. It all depends on what you want to the calcSum operation to do. Make sure you understand the difference between those two choices and make all the operations behave the same way. By the way, if you are interested in overloading the addition operator, you can define the addition in another way. You can create a standalone function like this: Polynomial operator+(const Polynomial& p1, const Polynomial& p2) { Polynomial result; // Do calculation result.c1 = p1.c1 + p2.c1; // and so on for the other constants. return result; } This function is not a member of the Polynomial class, but it can access Polynomial private members because in the Polynomial class, we declare the function to be a friend like this: class Polynomial { friend Polynomial operator+(const Polynomial& p1, const Polynomial& p2); }; Here is a main showing how the operator+ would be used. int main(void) { Polynomial p1(1,2,3,4,5); Polynomial p2(6,7,8,9,10); Polynomial result = p1 + p2; // this calls the operator+ function } I hope you are not overwhelmed by the choices. I'd suggest you stick with the ideas you have studied in your course. ---------- FOLLOW-UP ---------- QUESTION: Hi Zlatko, Here is what I have done so far. I decided to just include 2 additional polynomials (the 2 quadratics to be multiplied) and so I have now p3 and p4. I think my professor would be ok with this and don't need to add in the error message). Did I do the calcProduct part correct? note also, the assignment said please DO NOT OVERLOAD ANY OPERATORS and that's because we haven't gotten there in the course yet so wondering if what I have is appropriate, it seems I'm doing it right. Another thing, I would like a little help on figuring out how to get the polynomials to print right for the printPolynomial(). Best way to get the printPolynomial function to print out the polynomial? Several things I'd like now for the printing: 1. If the coefficient is zero, then you shouldn't show the term at all. But if all of the coefficients are zero, then the entire polynomial should show as "0". 2. If the coefficient is 1 or -1, you shouldn't show it (e.g., "- x^2" instead of "- 1x^2"), and if you see something like 4x^4 + -2x^3, you should just print out 4x^4 - 2x^3. 3. If the power is 1 then you shouldn't show the power (e.g., "35x" instead of "35x^1"). 4. If the power is zero then you shouldn't show the x factor (e.g., "-18" instead of "-18x^0"). I'm thinking probably most of those need an if statement. If I used arrays, it probably would be bit easier, but I'm not using arrays so how would it go to keep everything straight? using namespace std; class Polynomial { public: Polynomial(int t1 = 0, int t2 = 0, int t3 = 0, int t4 = 0, int t5 = 0); Polynomial calcSum(const Polynomial& p2); Polynomial calcDifference(const Polynomial& p2); Polynomial calcProduct(const Polynomial& p3, const Polynomial& p4); void printPolynomial(); private: int c0, c1, c2, c3, c4; }; Polynomial :: Polynomial(int t1, int t2, int t3, int t4, int t5) { c0 = t1; c1 = t2; c2 = t3; c3 = t4; c4 = t5; } void Polynomial :: printPolynomial() { cout Polynomial sum; sum.c0 = c0 + p2.c0; sum.c1 = c1 + p2.c1; sum.c2 = c2 + p2.c2; sum.c3 = c3 + p2.c3; sum.c4 = c4 + p2.c4; return sum; } Polynomial Polynomial:: calcDifference(const Polynomial& p2) { Polynomial difference; difference.c0 = c0 - p2.c0; difference.c1 = c1 - p2.c1; difference.c2 = c2 - p2.c2; difference.c3 = c3 - p2.c3; difference.c4 = c4 - p2.c4; return difference; } Polynomial Polynomial:: calcProduct(const Polynomial& p3, const Polynomial& p4) { Polynomial product; product.c0 = p3.c2 * p4.c2; product.c1 = (p3.c2 * p4.c3) + p3.c3; product.c2 = (p3.c3 * p4.c4) + (p3.c3 * p4.c3) + (p3.c4 * p4.c2); product.c3 = (p3.c3 * p4.c4) + (p3.c4 * p4.c3); product.c4 = p3.c4 * p4.c4; return product; } int main() { Polynomial p1(1, 2, 3, -4, -5); Polynomial p2(2, 3, -5, 6, 9); Polynomial p3(0, 0, 1, 2, 3); Polynomial p4(0, 0, 1, 2, 3); cout << "The first polynomial is "; p1.printPolynomial(); cout << "\nThe second polynomial is "; p2.printPolynomial(); Polynomial sum = p1.calcSum(p2); cout << "\n\nThe sum is "; sum.printPolynomial(); Polynomial difference = p1.calcDifference(p2); cout << "\nThe difference is "; difference.printPolynomial(); cout << endl; cout << "\nThe first quadratic polynomial is "; p3.printPolynomial(); cout << "\nThe second quadratic polynomial is "; p4.printPolynomial(); cout << endl << endl; return 0; } thank you very much, Mike
-
Answer:
Edit: October 26 Mike, when printing out your polynomial, don't try to do it all in one cout statement. You can use many cout statements and print a small section of the polynomial in each. Let me know how it goes. Hi Mike. Lets start with the calcProduct function. Notice that it does not follow the same pattern as the addition and subtraction methods, but it should. The add and subtract methods use the data of the calling object and the data of p2 passed in to do the operations. Your version of calcProduct does not use any data from the calling object. In such a case the function should be made static. If you don't know what that means, don't worry about it, The point is that the functions should all follow the same pattern. I strongly suggest that you change calcProduct to look like this: Polynomial Polynomial:: calcProduct(const Polynomial& p2) { } Now for the implementation. I think you have it right, but I'm not 100% sure. I thought that you were going to have the naming convention of "ci" where "i" matches the exponent. So c4 would be the coefficient for x^4, c3 for x^3, etc. You seem to have it in reverse but I think you did the algebra correctly. Anyway, here is my derivation for you. (Ax^2 + Bx + C)(Dx^2 + Ex + F) ADx^4 + (AE+BD)x^3 + (AF+BE+CD)x^2 + (BF+CE)x + CF A*D*x^4 + (A*E+B*D)x^3 + (A*F+B*E+C*D)^x2 + (B*F+C*E)x + C*F Then you use the text editor's search and replace function to the the following replacements. A -> c2 B -> c1 C -> c0 D -> p2.c2 E -> p2.c1 F -> p2.c0 That gives you this code: Polynomial Polynomial:: calcProduct(const Polynomial& p2) { Polynomial result; result.c4 = c2*p2.c2; result.c3 = c2*p2.c1 + c1*p2.c2; result.c2 = c2*p2.c0 + c1*p2.c1 + c0*p2.c2; result.c1 = c1*p2.c0 + c0*p2.c1; result.c0 = c0*p2.c0; return result; } Now for the printing. This actually requires a little thought, so I'm going to leave some work for you. You are correct that an array will make things easier and it's not too late to use an array without re-doing everything else. With an array you can have a single loop with the same algorithm running for each coefficient. Without an array, and loop, you will have a lot of repeating code. You are correct that you need to handle special cases with "if" statements. The whole thing is not too long, but can be tricky to get right. I will help you by setting up the function. I am putting in a few extra print statements which will help while debugging. Later you can take them out. void Polynomial :: printPolynomial() { cout /* first is a flag. It is useful to know if the first coefficient has been printed out. Why ? Because if you're working on the first coefficient, and it is positive, you don't want the leading "+" being printed. Also, if you get to the end of the print function and first is still true, then it means that nothing has been printed so far */ bool first = true; /* Work from the highest exponent to the lowest. Notice that the exp variable can also be used to print the number after the exponent sign "^" */ for(int exp = 4; exp >= 0; --exp) { /* about 12 lines of code go here to do the printing */ } cout } The approach I would use is to start by doing something simple in the loop, like cout +1 All the coefficients of +1 are also handled the same. So you really need to thoroughly test with coefficients of -2,-1,0,+1,+2 Also, the way a particular coefficient is printed does not depend on how the previous or next will be printed. It matters only if the coefficient is the first, or in the middle. With that in mind, I've made up a test function for you. You run the test on your printing algorithm and check the results by eye. Here is the test function: void PolynomialPrintTest() { Polynomial().printPolynomial(); Polynomial(1, 1, 1, 1, 1).printPolynomial(); Polynomial(1, 1, 1, 1).printPolynomial(); Polynomial(1, 1, 1).printPolynomial(); Polynomial(1, 1).printPolynomial(); Polynomial(1).printPolynomial(); Polynomial(0, 0, 0, 0, 1).printPolynomial(); Polynomial(0, 0, 0, 1, 1).printPolynomial(); Polynomial(0, 0, 1, 1, 1).printPolynomial(); Polynomial(0, 1, 1, 1, 1).printPolynomial(); Polynomial(-1, -1, -1, -1, -1).printPolynomial(); Polynomial(-1, -1, -1, -1).printPolynomial(); Polynomial(-1, -1, -1).printPolynomial(); Polynomial(-1, -1).printPolynomial(); Polynomial(-1).printPolynomial(); Polynomial(0, 0, 0, 0, -1).printPolynomial(); Polynomial(0, 0, 0, -1, -1).printPolynomial(); Polynomial(0, 0, -1, -1, -1).printPolynomial(); Polynomial(0, -1, -1, -1, -1).printPolynomial(); Polynomial(2, 2, 2, 2, 2).printPolynomial(); Polynomial(2, 2, 2, 2).printPolynomial(); Polynomial(2, 2, 2).printPolynomial(); Polynomial(2, 2).printPolynomial(); Polynomial(2).printPolynomial(); Polynomial(0, 0, 0, 0, 2).printPolynomial(); Polynomial(0, 0, 0, 2, 2).printPolynomial(); Polynomial(0, 0, 2, 2, 2).printPolynomial(); Polynomial(0, 2, 2, 2, 2).printPolynomial(); Polynomial(-2, -2, -2, -2, -2).printPolynomial(); Polynomial(-2, -2, -2, -2).printPolynomial(); Polynomial(-2, -2, -2).printPolynomial(); Polynomial(-2, -2).printPolynomial(); Polynomial(-2).printPolynomial(); Polynomial(0, 0, 0, 0, -2).printPolynomial(); Polynomial(0, 0, 0, -2, -2).printPolynomial(); Polynomial(0, 0, -2, -2, -2).printPolynomial(); Polynomial(0, -2, -2, -2, -2).printPolynomial(); } The results will look something like this, but there will be more of them: [Coeff 0 0 0 0 0] 0 [Coeff 1 1 1 1 1] x^4+x^3+x^2+x+1 [Coeff 0 1 1 1 1] x^3+x^2+x+1 [Coeff 0 0 1 1 1] x^2+x+1 [Coeff 0 0 0 1 1] x+1 [Coeff 0 0 0 0 1] 1 If all the results look correct, it means your printing algorithm is correct. Ok, now its your turn to do some work on the printing function.
Miningco.com Visit the source
Related Q & A:
- I need help on what I need to buy or do.Best solution by Yahoo! Answers
- I need help with some horse questions, can you help me.Best solution by Yahoo! Answers
- I need help with Adobe Photoshop.Best solution by Yahoo! Answers
- I did something really bad and now i need help please help me.Best solution by Yahoo! Answers
- What sort of opportunities are there to help people in Africa or places where people need help?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.