I need help with polynomials using arrays

Hello c++! I'm here cause i need some help with my term proyect. This is my proyect description...

Design and implant a Polynomial class, in which a polynomial is implemented as a linked list of coefficients. Each node will have an integer to hold the exponent of the term and a real number to hold the coefficient the term. For example, 5.6x3- 6x2+3.5 will be in the linked list as (3, 5.6)  (2, ‐6)  (1, 0)  (0, 3.5)   
 

Overload  All the usual polynomial operators (+, –, *),  Input and output operators  The polynomial value when x value is given. I.e. f(x)= 5.6x3- 6x2+3.5 for x=1.5.

But instead of the linked list, I am using arrays. I have my header file, my implementation and my demo_program. It is running, it ask the user to enter 2 polynomial and its size, but here is where i need help. I need a function or a code to ask them if they want to add, substract or multiply those 2 polynomials. Can some one help me out to finish it please!? :C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Header file:

//Polynomial.h
// 
#ifndef polynomial_H
#define polynomial_H

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



class Polynomial
{
private:
	double *Ncoefficients; //It is the array where we store the coefficients

	int Pdegree; //It is the degree of the polynomial ( one less then the length of the array of coefficients)

public:
	Polynomial(); //the default constructor to initialize a polynomial equal to 0
	Polynomial(double[], int); //the constructor to initialize a polynomial with the given coefficient array and degree
	Polynomial(Polynomial&); //the copy constructor
	Polynomial(double); //the constructor to initialize a polynomial equal to the given constant
	double evaluateAt(double x);
	void display(void);
	~Polynomial() { delete Ncoefficients; } //the destructor to clear up the allocated memory

	//the operators to define for the Polynomial class
	friend Polynomial operator+(Polynomial & p1, Polynomial & p2)
	{
		return Polynomial(p1.Ncoefficients + p2.Ncoefficients, p1.exp);
	}
	Polynomial operator-(Polynomial p) const;
	Polynomial operator*(Polynomial p) const;
};

here is my implementation file!:

// Polynomial implementation file

#include <iostream>
#include <cstdlib>
#include "polynomial.h"
using namespace std;

void Polynomial();
Polynomial::Polynomial(double Coeffs[], int N_terms)
{
	Pdegree = N_terms;
	Ncoefficients = new double[Pdegree];// allocate an array to hold the coefficient values
	for (int i = 0; i<Pdegree; i++)
		Ncoefficients[i] = Coeffs[i];// assign in straight order
}






double Polynomial::evaluateAt(double x)
{
	double sum = 0.0;
	double xPow = 1.0;
	if (Ncoefficients)
		for (int i = 0; i<Pdegree; i++)
		{
			sum += xPow*Ncoefficients[i];// add up the terms
			xPow *= x;// build up the power of x from term to term
		}

	return sum;
}
// display it
void Polynomial::display(void)
{
	// 1st term
	cout << Ncoefficients[Pdegree - 1] << "x^" << Pdegree - 1;
	// remaining terms
	for (int i = Pdegree - 2; i >= 0; i--)
		cout << " + " << Ncoefficients[i] << "x^" << i;
	return ;
}


and here is my program main;

// Term Proyect Polynomial
// Demo_program
#include <iostream>
#include <string>
#include "polynomial.h"
using namespace std;


int main()
{
	int size;
	string answer;
	Polynomial num3;
	cout << "What is going to be the size?" << endl;
	cin >> size;
	double *array;
	array = new double[size];
	
	
	for (int i = 0; i < size; i++)
	{
		cout << "Enter the coeff of x^ " << i << " : ";
		cin >> array[i];
	}
	Polynomial num1(array, size);
	delete array;
	cin.ignore();
	size = 0;
	cout << "What is going to be the size for p2?" << endl;
	cin >> size;
	double *array2;
	array2 = new double[size];
	

	for (int i = 0; i < size; i++)
	{
		cout << "Enter the coeff of x^ " << i << " : ";
		cin >> array2[i];
	}
	Polynomial num2(array2, size);
		delete array2;

		cout << "What do you want to do? (add, substract or multiply)" << endl;
		cin >> answer;
	
		if (answer == "add")
		{
			int *sum = add()
		}
		cout << "num = ";

	num3.display();
	cout << "\n num(2.0) = " << num1.evaluateAt(2.0) << endl;
	system("pause");
	return 0;
}


Any help is welcome! Thanks guys! I would really appreciate your help :,)
Last edited on
given what you have I have to say you should be able to make a menu to ask the user what to do, so I will skip that.

You need to match powers. Generally one would start at the lowest power, which is zero, and work up through the powers, creating the new terms at each level.

for example

3x^2 + 2x + 4
+
x^3 - 2x^2 -2x
-------------------


start at the lowest term, the 0th power
are we done? No. do we have any? Yes... so
4 + 0 = 4. new lowest term is just 4, which is power zero, coefficient 4
are we done? no, next is power 1, do we have any? Yes
2x + -2x is zero! We won't store this term!
are we done? No. Next power is 2
3x^2 + -2x^2 is x^2, store (coefficient 1, power 2)
and so on in a loop until done, which you need a way to understand. You could have something silly like

3x^1000
-
2x^2

how do you know when to stop? You have to keep a counter of the # of terms in each poly!

Multiply will be more challenging, and division as well, but see if you can do add and subtract first. Baby steps...



THANKS! I kind of have an idea, but how can you code it? can you please help me out? Its cause I am a beginner and i have been struggling :s
Topic archived. No new replies allowed.