Develop class Polynomial. The internal representation of a Polynomial is an array of terms.
Each term contains a coefficient and an exponent. The term
2x
4
has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor
and destructor functions as well as set and get functions. The class should also provide the following
overloaded operator capabilities:
a) Overload the addition operator (+) to add two Polynomials.
b) Overload the subtraction operator (-) to subtract two Polynomials.
c) Overload the assignment operator to assign one Polynomial to another.
d) Overload the multiplication operator (*) to multiply two Polynomials.
e) Overload the addition assignment operator (+=), subtraction assignment operator (-=),
and multiplication assignment operator (*=).

here is my code so far. I am getting error


Error 1 error C2109: subscript requires array or pointer type c:\users\coxxie\documents\visual studio 2010\projects\homework 11.12\homework 11.12\polynomial.cpp 275 1 homework 11.12
Error 2 error C2109: subscript requires array or pointer type c:\users\coxxie\documents\visual studio 2010\projects\homework 11.12\homework 11.12\polynomial.cpp 278 1 homework 11.12
3 IntelliSense: expression must have pointer-to-object type c:\users\coxxie\documents\visual studio 2010\projects\homework 11.12\homework 11.12\polynomial.cpp 275 13 homework 11.12
4 IntelliSense: expression must have pointer-to-object type c:\users\coxxie\documents\visual studio 2010\projects\homework 11.12\homework 11.12\polynomial.cpp 278 10 homework 11.12

yet when i use the & pointer i get some weird results. any help would be helpful.

my header file

#ifndef POLY_H
#define POLY_H

#include <iostream>
#include <iomanip>
using namespace std;

class PolyNom
{
public:
	PolyNom();
	PolyNom operator + ( const PolyNom& ) const;
	PolyNom operator - ( const PolyNom& ) const;
	PolyNom operator * ( const PolyNom& );
	PolyNom operator = ( const PolyNom& ); 

	PolyNom& operator +=( const PolyNom& );
	PolyNom& operator -=( const PolyNom& );
	PolyNom& operator *=( const PolyNom& );

	void enterTerms();
	void printPolyNom() const;

	int getNumOfTerms();
	int getTermExpo( int );
	int getTermCoeff( int );

	void setCoeff( int, int);
	~PolyNom();

private:

	int numOfTerms;
	int expo[ 100 ];
	int coeff[ 100 ];
	void polyNomCombine( PolyNom& );

};
#endif

my cpp file

#include "polynomial.h"

PolyNom::PolyNom()
{
	for ( int t = 0; t < 100; t ++)
	{
		coeff[t] = 0;
		expo[t] = 0;
	}
}

void PolyNom::printPolyNom() const
{
	int start;
	bool zero = false;

	if (coeff[ 0 ] )
	{
		cout << coeff[ 0 ];
		start = 1;
		zero = true;
	}
	else
	{
		if ( coeff[1])
		{
			cout << coeff[1] << 'x';

			if( ( expo[1] != 0) && (expo[1] != 1))
				cout << '^' << expo[1];

			zero = true;
		}
		
		start = 2;
	}
	for ( int x = start; x < 100; x++)
	{
		if( coeff[x] != 0)
		{
			cout << showpos << coeff[x] << noshowpos << 'x';

			if( (expo[x] !=0) && ( expo[x] != 1) )
				cout << '^' << expo[x];

			zero = true;
		}
	}

	if( !zero)
		cout << '0';

	cout << endl;
}

PolyNom PolyNom::operator = ( const PolyNom &r )
{
	expo[0] = r.expo[0];
	coeff[0] = r.coeff[0];

	for( int s = 1; ( s < 100); s++ )
	{
		if ( r.expo[s] != 0 )
		{
			expo[s] = r.expo[s];
			coeff[s] = r.coeff[s];
		}
		else
		{
			if( expo[s] == 0)
				break;

			expo[s] = 0;
			coeff[s] = 0;
		}
	}
	return *this;
}

PolyNom PolyNom::operator + ( const PolyNom &r ) const
{
	PolyNom temp;
	bool expoExists;
	int s;

	temp.coeff[0] = coeff[0] + r.coeff[0];

	for ( s = 1; ( s < 100) && ( r.expo[s] != 0 ); s++)
	{
		temp.coeff[s] = r.coeff[s];
		temp.expo[s] = r.expo[s];
	}
	for ( int x = 1; x < 100; x++ )
	{
	 expoExists = false;
		for (int t = 1; ( t < 100) && (!expoExists); t++)

			if( expo[x] == temp.expo[t] )
			{
				temp.coeff[t] += coeff[x];
				expoExists = true;
			}
			if( !expoExists )
			{
				temp.expo[s] = expo[x];
				temp.coeff[s] += coeff[x];
				s++;
			}
	}
	return temp;
}

PolyNom& PolyNom::operator +=( const PolyNom &r )
{
	*this = *this + r;
	return *this;
}

PolyNom PolyNom::operator - ( const PolyNom &r ) const
{
	PolyNom temp;
	bool expoExists;
	int s;

	temp.coeff[0] = coeff[0] - r.coeff[0];

	for( s = 1; (s < 100) && ( expo[s] != 0 ); s++)
	{
		temp.coeff[s] = coeff[s];
		temp.expo[s] = expo[s];

	}

	for( int x = 1; x < 100; x++ )
	{
		expoExists = false;

		for( int t = 1; ( t < 100) && (!expoExists); t++)

			if( r.expo[x] == temp.expo[t] )
			{
				temp.coeff[t] -= r.coeff[x];
				expoExists = true;
			}
			if( !expoExists )
			{
				temp.expo[s] = r.expo[x];
				temp.coeff[s] -= r.coeff[x];
				s++;
			}
	}
	return temp;
}

PolyNom& PolyNom::operator -=( const PolyNom &r )
{
	*this = *this - r;
	return *this;
}

PolyNom PolyNom::operator * ( const PolyNom &r)
{
	PolyNom temp;
	int s = 1;

	for( int x = 0; ( x < 100) && ( x == 0 || r.coeff[x] != 0); x++)

		for( int y = 0; (y < 100) && y == 0 || r.coeff[y] != 0; y++)

			if( (expo[x] == 0) && ( r.expo[y] == 0) )
				temp.coeff[0] += coeff[x] * r.coeff[y];

			else
			{
				temp.coeff[s] = coeff[x] * r.coeff[y];
				temp.expo[s] = expo[x] + r.expo[y];
				s++;
			}
			polyNomCombine( temp );
			return temp;
}

void PolyNom::polyNomCombine( PolyNom &w)
{
	PolyNom temp = w;
	int exp;

	for( int x = 0; x < 100; x++ )
	{
		w.coeff[x] = 0;
		w.expo[x] = 0;
	}

	for( int x = 1; x < 100; x++ )
	{
		exp = temp.expo[x];

		for( int y = x + 1; y < 100; y++)

			if( exp == temp.expo[y] )
			{
				temp.coeff[x] += temp.coeff[y];
				temp.expo[y] = 0;
				temp.coeff[y] = 0;
			}
	}
	w = temp;
}

PolyNom& PolyNom::operator *=( const PolyNom &r)
{
	*this = *this * r;
	return *this;
}

void PolyNom::enterTerms()
{
	bool found = false;
	int c, e, term;

	cout << "\nEnter number of polynomial terms: ";
	cin >> numOfTerms;

	for( int n = 1; n <= numOfTerms; n++ )
	{
		cout << "\nEnter a coefficient: ";
		cin >> c;
		cout << "Enter exponent: ";
		cin >> e;

		if( c != 0 )
		{
			if( e == 0)
			{
				coeff[0] += c;
				continue;
			}

			for( term = 1; (term < 100) && ( coeff[term] != 0); term++)

				if ( e == expo[term] )
				{
					coeff[term] += c;
					expo[term] = e;
					found = true;
				}

				if( !found )
				{
					coeff[term] += c;
					expo[term] = e;
				}
		}
	}
}


int PolyNom::getNumOfTerms()
{
	return numOfTerms;
}

int PolyNom::getTermExpo( int term )
{
	return expo[term];
}

int PolyNom::getTermCoeff( int term )
{
	return coeff[term];
}

void PolyNom::setCoeff( int term, int coeff)
{
	if( coeff[ term ] == 0)
		cout << "No term at this here, can't set term." << endl;
	else
		coeff[ term ] = coeff;
}

PolyNom::~PolyNom()
{

}

my driver program

#include "polynomial.h"

int main()
{
	PolyNom a, b, c, t;

	a.enterTerms();
	b.enterTerms();
	t = a;

	cout << "First polynomial is: \n";
	a.printPolyNom();

	cout << "Second polynomial is: \n";
	b.printPolyNom();

	cout << "\nAdding the polynomials yields: \n";
	c = a + b;
	c.printPolyNom();

	cout << "\n+= the polynomials: \n";
	a += b;
	a.printPolyNom();

	cout << "\nSubtracting the polynomials: \n";
	a = t;
	c = a - b;
	c.printPolyNom();

	cout << "\n-= the polynomials: \n";
	a -= b;
	a.printPolyNom();

	cout << "\nMultiplying the polynomials: \n";
	a = t;
	c = a * b;
	c.printPolyNom();

	cout << "\n*= the polynomials: \n";
	a *= b;
	a.printPolyNom();

	cout << endl;

	system("pause");

	return 0;


}

Look at line 275 and 278 as your errors mention. What's the definition of coeff from your parameter list, and how are you using it? Are they the same, or even similar?

What seems to be happening here is that you are getting a conflict between two variables named coeff : one is an instance variable of the class, which is an array of int , the other is an argument of the method, which is an int . Because of the scoping rules, the argument is masking the instance variable.

You need to either a) rename the argument to something other than coeff , or b) use this->coeff[] when referring to the instance variable. I would recommend the former, as having two variables of the same name is still likely to cause confusion even when using this to avoid ambiguity.

Look at line 275 and 278 as your errors mention. What's the definition of coeff from your parameter list, and how are you using it? Are they the same, or even similar?

parameters are the same as well as the def. used the same data members through out the code so can't figure out why it isn't working and why its asking for pointers.

parameters are the same as well as the def. used the same data members through out the code so can't figure out why it isn't working and why its asking for pointers.

They are?
I see in the parameter list a single int. ( void PolyNom::setCoeff( int term, [B]int coeff[/B]) )
I see in the function an array being used ( coeff[ term ] )
They are not the same.

what is meant by "The internal representation of a Polynomial is an array of terms."
please describe me I also have same question as an assignment. but I don't understand what does the above statement mean... kindly guide please......

This article has been dead for over six months. Start a new discussion instead.