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.



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

1
2
3
4
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:

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
#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:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "Poly.h"
#include "String.h"
#include "FileIO.h"

#include <math.h>
#include <stdlib.h>
#include <iostream>
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:
1
2
3
4
5
6
   if(power <= (max_power)){
      coeffs[power] = cf;
   }
   if(degree < power){
      degree = power;
   }

should be:
1
2
3
4
5
   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.