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