So my readPoly fct assigns the values i want to my dynamic array, but when the fct send Polynomial p back to main to be assigned to a and b(fct called twice) the first value of the array is set to 0. I've output the array inside readPoly and it gives valid values, then as soon as it is passed to main i output again and i have lost
a->coeffs[0] and b->coeffs[0]! Any help would be greatly appreciated! Here's my whole code so far, though the error i believe is between readPoly(Polynomial* p) and the main()fct.It could be the outputPoly fct but i don't think it is.Alse, my addPoly fct breaks the program, but im more concerned about storing the polynomial correctly
#include <iostream>
#include <cstdlib>
using namespace std;
struct Polynomial
{
int degree;
int *coeffs;
Polynomial();
};
Polynomial* readPoly();
Polynomial* readPoly2();
void outputPoly(Polynomial* p);
Polynomial* addPoly(Polynomial* a,Polynomial* b);
Polynomial* multPoly(Polynomial* a,Polynomial* b);
void deletePoly(Polynomial* &p);
int z = 1;//BAD PRACTICE, but i just wanted a quick way to identify which polynomial was being read into.
int main()
{
Polynomial *a;
Polynomial *b;
Polynomial *c;
a = new Polynomial;
b = new Polynomial;
c = new Polynomial;
a = readPoly();
cout << "\ncompleted readPoly(a)\n";
b = readPoly();
cout << "\ncompleted readPoly(b)\n";
if(a-> coeffs == NULL || b-> coeffs == NULL)
{
cout << "Error: Insuficient memory...exiting program..\n";
exit(1);
}
cout << "\nInside main:\nDegree:"<<a->degree;
cout << "\nCofficients:";
for(int j=0;j<a->degree+1;j++)
{cout << a->coeffs[j];
}
outputPoly(a);
outputPoly(b);
//c = addPoly(a,b);
}
Polynomial::Polynomial() : degree(0),coeffs(NULL)//constructor
{}
Polynomial* readPoly()
{
int deg;
/*------------------------Read in polynomial---------------------------*/
cout << "Enter degree of polynomial"<<z<<":";
cin >> deg;
cout << "Enter the coefficients of polynomial"<< z <<" starting with\n"
<< "the lowest degree term and ending with the highest degree term\n";
int* coefficients;
coefficients = new int[deg+1];
for(int i =0;i<deg+1;i++)
{
cout << ":";
cin >> coefficients[i];// a test used to examine values of coefficients
}
/*---------------------------------------------------------------------*/
Polynomial * p = new Polynomial;
p -> degree = deg;
p -> coeffs = coefficients;//Error here when coefficients copies to p->coeffs
cout << "Degree:"<<p->degree;
cout << "\np->coeffs:";
for(int j=0;j<p->degree+1;j++)//a test used to examine values of p->coeffs
{cout << p->coeffs[j];
}
delete []coefficients;
z++;
return p;
}
Polynomial* addPoly(Polynomial* a,Polynomial* b)
{
cout << "Program entered addPoly\n";
int max = 0;
int min = 0;
int tracker = 0;
if(a->degree > b-> degree)//Determines which polynomial has a higher degree of a and b, because c degree should match higher degree
{
max = a-> degree;
min = b-> degree;
}
else
{
max = b-> degree;
min = a-> degree;
}
Polynomial *c;
c = new Polynomial[max+1];
for(int i = 0;i<min+1;i++)
{
c->coeffs[i] = a->coeffs[i] + b->coeffs[i];
tracker = i;
}
if(a->degree > b-> degree)//Determines which polynomial has a higher degree of a and b, because c degree should match higher degree
{
for(int j = 0;j<max-min;j++)
{
c->coeffs[j+tracker+1] = a->coeffs[j+tracker+1];
}
c->degree = a->degree;
}
else
{
for(int j = 0;j<max-min;j++)
{
c->coeffs[j+tracker+1] = b->coeffs[j+tracker+1];
}
c->degree = b->degree;
}
return c;
}
void outputPoly(Polynomial *p)
{
int deg = p->degree;
cout << "\nInside fct outputPoly:\nDegree:"<<p->degree;
cout << "\nCofficients:";
for(int j=0;j<p->degree+1;j++)
{cout << p->coeffs[j];
}
cout << endl;
for(int i=0;i<deg+1;i++)
{
cout << p->coeffs[deg - i] << "x^" <<deg-i;
if(i<p->degree)
{
cout << "+";
}
}
cout << "\nCompleted outputPoly\n";
return;
}
void deletePoly(Polynomial* &p)
{
delete []p;
p = NULL;
}