### Polynomial Multiplication Function Help

I can not seem to complete this polynomial multiplication function for my Data Structures class. We had to figure out what to put for coeff_i, coeff_j, power, and coeff. I think i have the correct stuff for coeff_i, coeff_j, and power. However, i have no idea what to put for coeff. Also, initially, there was no Poly* temp; line. Because of this, it would compile with an error saying that temp was not declared. If anyone could be of any help, i would be very appreciative.

 ``123456789101112131415161718192021222324252627`` ``````Poly* Poly::multiply(Poly* other) { double TOL = .00001; int n = degree; int m = other->degree; Poly* temp; for (int i = 0; i <= n; i++) //loop over coeffs { for (int j = 0; j <= m; j++) //loop over second coeffs { double coeff_i = coeffs[i]; double coeff_j = other -> coeffs[j]; if (fabs(coeff_i) > TOL && fabs(coeff_j) > TOL) //if either is close to zero, nothing to do { int power = (i + j); double coeff = ; temp->setCoeff(power, coeff + (coeff_i * coeff_j)); } } } return temp; }``````
Line 8, you declared a pointer of type Poly, but you did not initialise it. So to do that, will look something like `Poly* temp = new Poly`

Line 16 will always evaluate to true. Only time they will evaluate to false is if you have a number that is less than 0.00001. I'm not sure if this is what you want, but I just thought to point that out.

Line 19, I'm not sure what your input looks like, so I can't give a response for that
Did you come up with the function signature on your own or was it given to you?

Typically such a function would look like:

 ``1234`` ``````Poly Poly::multiply( const Poly & other ) const { // ... }``````
I'll change the Poly declaration to what you said Smac89. Line 16 was given to us, so i don't think we are allowed to mess around with that, even if it is always true. The function was also given to us cire. I'll also show you the .h file that we had to write. If it helps, i'll put the whole code up here.

This is our .h file:

 ``123456789101112131415161718192021222324252627`` ``````#if !defined (POLY) #define POLY class Poly { private: double* coeffs; int max_power; int degree; public: Poly(int max_power); ~Poly(); double getCoeff(int power); void setCoeff(int power, double val); double evaluate(double x); Poly* multiply(Poly* other); void printPoly(); static Poly* readPoly(const char* file_name); void writePoly(const char* file_name); }; #endif ``````

And this is our main .cpp driver:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207`` ``````#include "Poly.h" #include "String.h" #include "FileIO.h" #include #include #include using namespace std; using namespace CSC2110; Poly::Poly(int m_p) { max_power = 3; if (m_p >= 0) { max_power = m_p; } //DO THIS //complete the constructor //remember to initialize the elements in the array coeffs = new double[max_power + 1]; for(int i = 0; i <= (max_power + 1); i++){ coeffs[i] = 0.0; } } Poly::~Poly() { //DO THIS //complete the destructor delete[] coeffs; } //check power validity double Poly::getCoeff(int power) { //DO THIS if(power <= (max_power)){ return coeffs[power]; } else{ return -1; } } //check power validity void Poly::setCoeff(int power, double cf) { //DO THIS //remember to update degree if necessary if(power <= (max_power)){ coeffs[power] = cf; } if(degree < power){ degree = power; } } double Poly::evaluate(double x) { //DO THIS //use Horner's method for efficiency double answer = coeffs[degree]; for(int i = degree; i >= 0; i--){ answer *= x; answer += coeffs[i - 1]; } return answer; } Poly* Poly::multiply(Poly* other) { double TOL = .00001; int n = degree; int m = other->degree; Poly* temp; for (int i = 0; i <= n; i++) //loop over coeffs { for (int j = 0; j <= m; j++) //loop over second coeffs { double coeff_i = coeffs[i]; double coeff_j = other -> coeffs[j]; if (fabs(coeff_i) > TOL && fabs(coeff_j) > TOL) //if either is close to zero, nothing to do { int power = (i + j); double coeff = temp->setCoeff(power, coeff + (coeff_i * coeff_j)); } } } return temp; } void Poly::printPoly() { double TOL = .00001; cout << coeffs[degree] << "x^" << degree; for (int i = degree - 1; i >= 0; i--) { double coeff = coeffs[i]; if (fabs(coeff) > TOL) { cout << " + " << coeff << "x^" << i << " "; } } cout << endl; } //assumes a specific format for the file Poly* Poly::readPoly(const char* file_name) { FileIO* file = new FileIO(file_name, ' '); //for reading String* degree_str = file->readLine(); int degree = degree_str->parseInt(); delete degree_str; Poly* poly = new Poly(degree); for (int i = 0; i <= degree; i++) { String* coeff_str = file->readLine(); float coeff = coeff_str->parseFloat(); delete coeff_str; poly->setCoeff(i, (double) coeff); } delete file; return poly; } //assumes a specific format for the file void Poly::writePoly(const char* file_name) { FileIO* file = new FileIO(file_name, WRITE); //for writing String* degree_str = String::intToString(degree, 2); file->writeLine(degree_str); delete degree_str; for (int i = 0; i <= degree; i++) { float coeff = (float) getCoeff(i); String* coeff_str = String::floatToString(coeff, 3); file->writeLine(coeff_str); delete coeff_str; } delete file; } int main() {/* Poly* p_test = Poly::readPoly("poly_in_1.txt"); double eval = p_test->evaluate(2.861); cout << eval << endl; */ Poly* p_1 = Poly::readPoly("poly_in_1.txt"); p_1->printPoly(); Poly* p_2 = Poly::readPoly("poly_in_2.txt"); p_2->printPoly(); Poly* p_3 = p_1->multiply(p_2); p_3->printPoly(); p_3->writePoly("poly_out.txt"); double eval = p_3->evaluate(2.861); cout << eval << endl; delete p_1; delete p_2; delete p_3; }``````

I'll also throw in the two text files it reads from:

3
8.2
-1.4
-0.9
4.6

and

2
-6.4
5.7
14.5

I hope this helps, because this is becoming frustrating for just a lab.
Just skimming over..

Line 26: ` for(int i = 0; i <= (max_power + 1); i++){`
should be: ` for(int i = 0; i < (max_power + 1); i++){`

Line 53: ` if(power <= (max_power)){`
should be: ` if(power < (max_power)){`

Lines beginning at 69:
 ``123456`` `````` if(power <= (max_power)){ coeffs[power] = cf; } if(degree < power){ degree = power; }``````

should be:
 ``12345`` `````` if(power < (max_power)){ coeffs[power] = cf; if (degree < power) degree = power; }``````

line 86: ` for(int i = degree; i >= 0; i--){`
should be: ` for(int i = degree; i > 0; i--){`

You need to be much more careful to use valid indices into your arrays.

[edit: Oh, and your instructor should be shot for exposing you to this horrible design. =P]
Last edited on
Thank you for the help cire! I wasn't too sure on those equal signs, anyway. I was going to wait for it to compile and work so i could test them, but i couldn't get it to work. I'm glad someone agrees that his assignments are terrible. I'll have to fiddle around with it a bit more and see if i can get it to work. If you or anyone else has any ideas, i'd be glad to hear them.
If you're unable to get it to compile, posting the first few error messages the compiler spits out when you try might prove fruitful. One can't just toss your code in a compiler and see what you see - the FileIO and String stuff aren't available to us. I find it odd such things are used when C++ has perfectly functional versions of those you need to learn anyway.
The FileIO and String stuff are their own cpp and h files that he gives us and we aren't allowed to mess around with those, so they should work fine on their own. Because i still have no idea about what the double coeff variable needs to be, i set it to one to check what it would do when i compiled it. I also set Poly* temp = new Poly;. Initially, he had no declaration of temp, so i've been working on the assumption that we need to declare it somewhere. When i try to compile it, the FileIO and String are fine, but it gives me an error related to declaring temp.

It does this:

 ```Poly.cpp: In member function 'Poly* Poly::multiply(Poly*)': Poly.cpp:104:21: error: no matching function for call to 'Poly::Poly()' Poly.cpp:104:21: note: candidates are: Poly.cpp:12:1: note: Poly::Poly(int) Poly.cpp:12:1: note: candidate expects 1 argument, 0 provided Poly.h:4:7: note: Poly::Poly(const Poly&) Poly.h:4:7: note: candidate expects 1 argument, 0 provided g++: error: Poly.o: No such file or directory ```

Line 104 should be:
`Poly* temp = new Poly(degree);`

Also when you compile, try compiling directly to an executable if you are working on a small project like this

so g++ -o exec_file_name [this.cpp,...]
Ok, i changed the temp declaration again, i had to use (n+m) instead of just degree. I did this because if you multiply the poly's, the largest degree is the sum of the largest degrees. It seems to print off something similar to what it needs to print off, however, it does not print the X^0 number correctly. It shifts all the numbers down a degree and just doesn't print the x^0. I also still have no idea what double coeff should be, i just have it set to 1 right now.

It gives me this:

 ``` C:\Users\AustinWade\Desktop\Lab 3>poly.exe 4.6x^3 + -0.9x^2 + -1.4x^1 + 8.2x^0 14.5x^2 + 5.7x^1 + -6.4x^0 67.7x^5 + 27.22x^4 + -28.44x^3 + 6.76x^2 + 9.96x^1 + -51.48x^0 14167.2 ```
Topic archived. No new replies allowed.