C++ assignment involving classes

Click For Summary
The discussion centers on a C++ assignment involving the implementation of a polynomial class that uses a rational number class. The user is struggling with operator overloading for polynomial addition and has provided their initial code attempt. Key feedback indicates that the user needs to reference the coefficients correctly using the `coeff` data member instead of attempting to access the polynomial directly. Additional advice includes using a debugger and providing compiler output for better assistance. The user expresses gratitude for the help and plans to revise their code accordingly.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 45 ·
2
Replies
45
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
Replies
81
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K