for some reason this program doesnt want to run, can someone help me. i always g
ID: 643273 • Letter: F
Question
for some reason this program doesnt want to run, can someone help me. i always get a break point error and the values im outputing are insane
#include <cassert>
#include <cmath>
#include <iostream>
#include "poly.h"
using namespace std;
polynomial::polynomial()
{
coef = new double[size];
for (int i = 0; i < size; i++)
{
coef[i] = 0;
}
}
polynomial::polynomial(const polynomial& source)
{
coef = new double[source.size];
size = source.size;
current_degree = source.current_degree;
for (int i = 0; i < size; i++)
coef[i] = source.coef[i];
}
polynomial::~polynomial()
{
delete[] coef;
}
unsigned int polynomial::getsize() const
{
return size;
}
void polynomial::setcurrent_degree(int init_degree)
{
current_degree = init_degree;
}
void polynomial::setsize(int init_size)
{
size = init_size;
}
double polynomial::coefficient(unsigned int exponent) const
{
return coef[exponent];
}
unsigned int polynomial::degree() const
{
{
int d = 0;
for (int i = 0; i < 10; i++)
if (coef[i] != 0) d = i;
return d;
}
}
ostream & operator<<(ostream & cout, const polynomial & r)
{
if (r.degree() == -1)
{
cout << 0;
return cout;
}
cout << r.coefficient(r.degree()) << "x^" << r.degree();
return cout;
}
polynomial operator+(const polynomial & l, const polynomial & r)
{
polynomial result;
int leftDegree = l.degree(), rightDegree = r.degree(), degree;
double sum;
if (leftDegree > rightDegree)
degree = leftDegree;
else
degree = rightDegree;
for (int i = degree; i >= 0; i--)
{
sum = l.coefficient(i) + r.coefficient(i);
}
return result;
}
polynomial operator-(const polynomial& l, const polynomial& r)
{
polynomial result;
int leftDegree = l.degree(), rightDegree = r.degree(), degree;
double sum;
if (leftDegree > rightDegree)
degree = leftDegree;
else
degree = rightDegree;
for (int i = degree; i >= 0; i--)
{
sum = l.coefficient(i) - r.coefficient(i);
}
return result;
}
void polynomial::operator =(const polynomial& source)
{
double *new_coef;
if (this == &source)
return;
if (size != source.size)
{
new_coef = new double[source.size];
delete[] coef;
coef = new_coef;
size = source.size;
}
current_degree = source.current_degree;
for (size_t i = 0; i < size; i++)
coef[i] = source.coef[i];
}
void polynomial::reserve(unsigned int number)
{
double *larger_array;
if (number == size)
return;
if (number < current_degree)
number = current_degree;
larger_array = new double[number]();
for (int i = 0; i < number; i++)
{
larger_array[i] = coef[i];
}
delete[] coef;
coef = larger_array;
size = number;
}
void polynomial::assign_coef(double coefficient, unsigned int exponent)
{
coef[exponent] = coefficient;
if (coefficient != 0 && exponent > current_degree)
current_degree = coefficient;
int i = current_degree;
while (i >= 0 && coef[i] == 0.0)
{
current_degree--;
i--;
}
}
double polynomial::eval(double x) const
{
double total = 0;
double pow = 1;
for (unsigned
i = 0; i <= current_degree; i++)
{
total += coef[i];
pow *= current_degree;;
}
return total;
}
Explanation / Answer
hope it may helps you..
when i compiled it shows the error in this part of code...
polynomial result;
int leftDegree = l.degree(), rightDegree = r.degree(), degree;
double sum;
if (leftDegree > rightDegree)
degree = leftDegree;
else
degree = rightDegree;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.