Sum of arrays

I need to do a sum of polinomies but I don't know how to do this sum method. I'd really appreciate some help.

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
#include "Monomio.h"


Monomio::Monomio() {
	signo=' ';
	coeficiente=0.0;
	parteLiteral=' ';
	grado=1;
}

Monomio::Monomio(char s,float c, char pl, int g) {
	signo=s;
	coeficiente=c;
	parteLiteral=pl;
	grado=g;
}

Monomio::Monomio(Monomio &m){
	signo=m.getSigno();
	coeficiente = m.getCoeficiente();
	parteLiteral = m.getParteLiteral();
	 grado = m.getGrado();
}

void Monomio::setSigno(char s){
	signo=s;
}

void Monomio::setCoeficiente(float c) {
	coeficiente=c;
}

void Monomio::setparteLiteral(char pl) {
	parteLiteral=pl;
}

void Monomio::setGrado(int g) {
	grado=g;
}

char Monomio::getSigno(){
	return signo;
}

float Monomio::getCoeficiente() {
	return coeficiente;
}

char Monomio::getParteLiteral() {
	return parteLiteral;
}

int Monomio::getGrado() {
	return grado;
}

string Monomio::toString(){
	stringstream s;
	if(grado==1){
		s<<signo<<coeficiente<<parteLiteral;
	}

	else{
		s<<signo<<coeficiente<<parteLiteral<<"^"<<grado;
	}
	s<<" ";
	return s.str();
	s<<endl;
}

Monomio::~Monomio(void)
{
}
#include "Polinomio.h"


Polinomio::Polinomio(int n) {
	cantidad =0;
	tamano = n;
	vector = new Monomio*[n];
	for(int i=0;i<n;i++){
		vector[i]=new Monomio;
	}
}

void Polinomio::insertaElementos(Monomio* m) {
	vector[cantidad] = m;
	cantidad++;
}


void Polinomio::imprimeElementos(){
	for(int i=0;i<cantidad;i++){
		cout<<vector[i]->toString();
	}
}

void Polinomio::sumaPolinomios(){
	
	for(int i=0;i<cantidad;i++){
	}
}

void Polinomio::ordenaPolinomio(){
	
}

Polinomio::~Polinomio(void) {
	cantidad=0;
	for(int i=0;i<cantidad;i++)
	delete vector[i];
}
--------------------------------------------------------------------------------
#include"Monomio.h"
#include"Polinomio.h"

void main(){
	int n, m;

	cout<<"Digite la cantidad de monomios que hay en el polinomio: ";
	cin>>n;

	Polinomio a(n);
	
	Monomio *m1 = new Monomio('+',2,'x',3);
	Monomio *m2 = new Monomio('-',1,'x',1);
	Monomio *m3 = new Monomio('+',8.25,' ',1);

	a.insertaElementos(m1);
	a.insertaElementos(m2);
	a.insertaElementos(m3);

	a.imprimeElementos();

	cout<<endl;

	cout<<"Digite la cantidad de monomios que hay en el polinomio: ";
	cin>>m;

	Polinomio b(m);

	Monomio *m4 = new Monomio('+',5,'x',5);
	Monomio *m5 = new Monomio('-',2,'x',3);
	Monomio *m6 = new Monomio('+',7,'x',2);
	Monomio *m7 = new Monomio('-',3,' ',1);

	b.insertaElementos(m4);
	b.insertaElementos(m5);
	b.insertaElementos(m6);
	b.insertaElementos(m7);

	b.imprimeElementos();
	
	cout<<endl;

	a.~Polinomio();
	b.~Polinomio();
	delete m1;
	delete m2;
	delete m3;
	delete m4;
	delete m5;
	delete m6;	
	delete m7;

	system("pause");
}
First off, this code is incorrect:
1
2
3
4
5
6
7
8
9
a.~Polinomio();
b.~Polinomio();
delete m1;
delete m2;
delete m3;
delete m4;
delete m5;
delete m6;	
delete m7;
You should never explicitly call destructors.
m1, m2, ..., m7 are already being deleted in Polinomio::Polinomio().



Now, on to your question. The representation you chose for the polynomial makes computing the sum more difficult. This representation is sufficient for most purposes:

double polynomial[n];

where n == the degree of the polynomial + 1. The representation is interpreted as:

P(x) = sum i = 0 .. degree { polynomial[i] * x ^ i }

So, for example,

double polynomial[] = { 1, -2, 0, -4 };

would represent

sum i = 0 .. 3 { polynomial[i] * x ^ i } = polynomial[0] * x ^ 0 + polynomial[1] * x ^ 1 + polynomial[2] * x ^ 2 + polynomial[3] * x ^ 3 = 1 - 2 * x - 4 * x ^ 3

Adding two polynomials in this representation is trivial.
1
2
3
4
5
6
double polyA[n];
double polyB[m];
double sum[max(n, m)];

for (i = 0; i < max(n, m); i++)
    sum[i] = (i >= n ? 0 : polyA[i]) + (i >= m ? 0 : polyB[i]);
Last edited on
Topic archived. No new replies allowed.