Trouble with function args.

I'm making a simple program to multiply polynomials as an exercise to help myself understand classes and such.

I have narrowed down my problem and what I don't understand is why I cannot pass my polynomials between my global functions. I am running sup() which multiplies h (x^2+x+1) and p (x^3-3x+5) and getting 0's because there is some problem with passing p through Multpoly(h,p). Line 73 should be illustrative.

struct poly {
poly(){}
poly(int n); // Initializes an array of n 0's
~poly();
int GetDegree() { return degree; }
double GetC(int r) { int y = degree; if ( r > y ) { return 0;} else { return c[r]; } }
void SetC(int o, double t) { c[o] = t; }

private:
int *degree;
double *c;
};
poly::poly(int n)
{
degree = n;
c = new double[n+1];
for (int v = 0; v < n+1; v++)
c[v] = 0;
}

poly::~poly()
{
delete degree;
delete [] c;
}

void Showpoly(poly f)
{
int i, k, n;
double coeff;
n = f.GetDegree();

for ( i = 0; i < n+1; i++) {
k = n - i;
coeff = f.GetC(k);
if ( i == 0) {
if ( coeff != 1) { cout << coeff; }
cout << "x^" << k;
}
else {
if ( coeff != 0 ) {
if ( coeff > 0) { cout << " + "; } else { cout << " - "; }
if ( k >= 2 ) {
if ( coeff != 1) { cout << abs (coeff); }
cout << "x^" << k;
}
else if ( k == 1) {
if ( coeff != 1) { cout << abs (coeff); }
cout << "x";
}
else {
cout << abs (coeff);
}
}
if ( k == 0) {
cout << "\n";
}
}

}
}

poly Multpoly(poly f, poly g)
{
int n = f.GetDegree();
int m = g.GetDegree();
int k = n + m;
int l;
poly h(k);
double sum;

cout << f.GetC(0) << " is f(0) \n";
cout << g.GetC(0) << " is g(0) (Huh?)\n"; // What is going on here

cout << "The degree of f is " << n << " and the degree of g is " << m << "\n";

for (l = 0; l < n+1; l++){cout << f.GetC(l) << " is f sub "<< l << "\n";} // some kind of access problem?
for (l = 0; l < m+1; l++){cout << g.GetC(l) << " is g sub "<< l << "\n";}

for (int i = 0; i < k+1; i++) {
// cout << "I have made it to i = " << i << "\n";
sum = 0;
// cout << "and j = ";
for (int j = 0; j < i+1; j++) {
// cout << j << ",";
l = i-j;
// cout << "Multiplying f["<<j<<"] by g["<<l<<"] = " << f.GetC(j) << " * " << g.GetC(l) << " = " << (f.GetC(j) * g.GetC(l)) << "\n";
sum += (f.GetC(j) * g.GetC(l));
}
h.SetC(i,sum);
// cout << "\n I'm writing sum of " << sum << " to h.c[" << i << "] \n";
}

return (h);

}

void sup()
{
poly h(2);
poly p(3);

h.SetC(0,1);
h.SetC(1,1);
h.SetC(2,1);
p.SetC(0,5);
p.SetC(1,-3);
p.SetC(2,0);
p.SetC(3,1);

Showpoly(h);
Showpoly(p);

poly s(5);
s=Multpoly(h,p); // want: x^5 + x^4 - 2x^3 + 2x^2 + 2x + 5
Showpoly(s);


}


When I run this I get

>> sup()
x^2 + x + 1
x^3 - 3x + 5
1 is f(0)
2.85918e-316 is g(0) (Huh?)
The degree of f and the degree of g is 3
1 is f sub 0
1 is f sub 1
1 is f sub 2
3.17096e+180 is g sub 0
1.27665e-152 is g sub 1
2.31635e-152 is g sub 2
0 is g sub 3
0x^5 + 0x^2 + 0x + 0


Clearly something is bad with how I'm using the argument poly g but I can't figure out what. Any explanation greatly appreciated.
Last edited on
closed account (zwA4jE8b)
what does your user input look like.

if you want the user to enter something like
x^5
then you will need to parse that symbolically.
i.e. a finite state machine... a lexical analyzer.

if you are new to programming then this is quite a complex project.
Last edited on
I just make polynomials as an array with a degree. For now at least I'm only doing polynomials with one indeterminate element so poly just takes a list of values like this:

poly f(3);
f.SetC(0)=1;
f.SetC(1)=3;
f.SetC(2)=2;
f.SetC(3)=1;

will make a polynomial that returns x^3 + 2x^2 + 3x + 1 from Showpoly(f).

So, everything using a poly just uses an array of its coefficients. And yeah, I am new to programming.
Last edited on
closed account (zwA4jE8b)
i notice that in your sup() function you are setting some of the variables to 0
i.e. h.SetC(0,1); if you multiply anything by 0, you are going to get a zero returned.

also if you use the code tags <> it will be easier to read.
closed account (zwA4jE8b)
1
2
3
4
5
6
7
8
9
10
11
private:
 int *degree; 
 double *c;
 };
 poly::poly(int n)
 {
 degree = n;
 c = new double[n+1];
 for (int v = 0; v < n+1; v++)
 c[v] = 0;
 }


you are using an integer pointer but on line 7 you set degree to n. if degree is a pointer then you need to write
*degree = n;

this dereferences the pointer and sets it to 'n'.

why are you using pointers in the first place?

if you want the value stored in the pointer you need to dereference it. otherwise you are trying to assign/get an address.

Your concept seems like a good one though.
Last edited on
Thanks CreativeMFS, that turned out to be the problem. Must have left that * in there by accident from some earlier edit.
Topic archived. No new replies allowed.