Polynomial class addition operator with rational coefficients

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 3K views
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground. :cry:
The teacher created a class for rational numbers, and now we are working on some functions for a polynomial class which uses arrays of rationals from his class. My polynomials are arrays of coefficients with each stored at an index position representing the power of x the coefficient is multiplied to.
I'm embarrassed to ask for help because there are 4 files involved: the rational class header and implementation file and the polynomial class header and implementation file.
I know you people have better things to do this weekend, but if anyone is really bored, the assignment and files are here:
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/hw6.html
It's the polynomial.cpp file where we have to fill in the blanks.
One of the first things I tried to do was the operator overloading to define addition for polynomials. Here's what I tried:
Code:
// operator+
polynomial operator+(const polynomial & a,const polynomial & b)
{
	polynomial c;
	for (int num = 0; num < polynomial::SIZE; num ++) //fill array with zeros
		c[num] = 0;
	for (int i = 0; i < polynomial::SIZE; i ++) //compute sum
		c[i] = a[i] + b[i];
	return c;
}
So far, not so good. It would be a huge help if someone could help me get just this piece fixed up. Thanks,
MIH
 
Last edited by a moderator:
Physics news on Phys.org
Why does the class's name not appear in the declaration of the function? Does this function appear in the header, inside the class declaration, or in the implementation file?

- Warren
 
Math Is Hard said:
Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground. :cry:
The teacher created a class for rational numbers, and now we are working on some functions for a polynomial class which uses arrays of rationals from his class. My polynomials are arrays of coefficients with each stored at an index position representing the power of x the coefficient is multiplied to.
I'm embarrassed to ask for help because there are 4 files involved: the rational class header and implementation file and the polynomial class header and implementation file.
I know you people have better things to do this weekend, but if anyone is really bored, the assignment and files are here:
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/hw6.html
It's the polynomial.cpp file where we have to fill in the blanks.
One of the first things I tried to do was the operator overloading to define addition for polynomials. Here's what I tried:
Code:
// operator+
polynomial operator+(const polynomial & a,const polynomial & b)
{
	polynomial c;
	for (int num = 0; num < polynomial::SIZE; num ++) //fill array with zeros
		c[num] = 0;
	for (int i = 0; i < polynomial::SIZE; i ++) //compute sum
		c[i] = a[i] + b[i];
	return c;
}
So far, not so good. It would be a huge help if someone could help me get just this piece fixed up. Thanks,
MIH

The polynomial class has a data member coeff that holds the coefficients. Thus, need to use the construct:

c.coeff=a.coeff+b.coeff;

Right?

Oh yea, be nice if you could run this through a debugger. Microsoft Visual C++ has a nice one.
 
Last edited by a moderator:
MIH,

In general, please tell also provide us the compiler (and linker) output when asking questions like this. It's really hard (if not impossible) to diagnose a piece of code like this taken from a larger piece of unknown code. You didn't even tell us why it was "not so good."

- Warren
 
ok, sorry. I am using MS VC++ v 6. The whole thing (all four files) is at the link I listed in my OP. The class name is polynomial. It's defined in polynomial.h.
Saltydog - yes I believe that's the problem I was having. It was not using that coeff data member. That's where I was screwing up, I bet.
I wasn't sure if it was ok for me to use the brackets [], because I was getting errors (among the jillions of other errors) about [ not being defined. I felt so lost I didn't even know how to describe the problem I was having because there were so many.
Thanks so much for looking at that. I'll go work on this some more.
 
Math Is Hard said:
about [ not being defined.

You got that error because using the construct c implies an "array" of polynomials which you did not define. Use the same c.coeff construct for operator-, a little more complicated looping for operator*, and some if-statements for overloading the output operator and return "os" after you've filled it up with the polynomial.
 
cheers, SD! this helps a lot.