I can get my code to compile and produce correctly (as I want it right now), except for one problem:

Output:

What Degree Polynomial: 2
Enter Coefficient #: 8
Enter Coefficient #: 5
Polynomial selected is: 8x^(0)5x^(1)
What Degree Polynomial: 3
Enter Coefficient #: 4
Enter Coefficient #: 5
Enter Coefficient #: 6
Polynomial selected is: 4x^(1)+5x^(2)+6x^(3)+Press any key to continue . . .

As you can see, my outputs are smashed together, without the proper addition symbol (i.e 8x^(0) + 5x^(1) + ....)

This is my code:

#include <iostream>

using namespace std;

class Polynomial
{
public:
	void Polynomial1 ();
	void Polynomial2 ();
	int Degree;
	int *coef1, *coef2;
	int *power1, *power2;

private:
	static const int ConstValue=3;
};

int main()
{
	Polynomial Poly1,Poly2;


	Poly1.Polynomial1();
	cout << endl;
	Poly2.Polynomial2();

	return 0;
}

void Polynomial::Polynomial1()
{
	int i,n;

	cout << "What Degree Polynomial: ";
	cin >> i;

	coef1 = new (nothrow) int[i];
	power1 = new (nothrow) int[i];

	for (n= 0; n<(i); n++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef1[n];
	}

	for (n=0;n<(i);n++)
	{
		power1[n]= n;
	}

	cout << "Polynomial selected is: ";

	for (n=0;n<(i);n++)
	{
		cout << coef1[n] << "x^(" << power1[n] << ")";
	}
}

void Polynomial::Polynomial2()
{
	int h,j;

	cout << "What Degree Polynomial: ";
	cin >> h;

	coef2 = new (nothrow) int[h];
	power2 = new (nothrow) int[h];
	

	for (j= 1; j<=h; j++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef2[j];
	}

	for (j=1;j<=h;j++)
	{
		power2[j]= j;
	}

	cout << "Polynomial selected is: ";

	for (j=1;j<=h;j++)
	{
		cout << coef2[j] << "x^(" << power2[j] << ")" << "+";
	}
}

I can't figure out how to put the code in properly to address the addition symbol so that my answers end properly, and don't have a hanging addition symbol (i.e. Polynomial selected is: 4x^(1)+5x^(2)+6x^(3)+ )

Edited 6 Years Ago by PDB1982: n/a

Change

for (j=1;j<=h;j++)
{
	cout << coef2[j] << "x^(" << power2[j] << ")" << "+";
}

to

for (j=1;j<=h;j++)
{
	if(j != h)
		cout << coef2[j] << "x^(" << power2[j] << ")" << "+";
	else
		cout << coef2[j] << "x^(" << power2[j] << ")";
}

So basically what're doing here is to tell the program that if j is = to h then its the last number so it doesn't need to put a plus.

Edited 6 Years Ago by Jiwe: n/a

One other thing, might be an issue:

The exponent on a variable in a term is called the degree of that variable in that term, the degree of the term is the sum of the degrees of the variables in that term, and the degree of a polynomial is the largest degree of any one term.

According to the definition you might need to take in another term so a polynomial of degree 2 will actually have 3 members (of course two of them could be 0). I believe I'm reading/remembering that correctly.

Edited 6 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.