Answer to Question #9769 in C++ for John

Question #9769
Can someone help me with this
#include <iostream>

class polynomial
{
public:
// CONSTANT
static const int CAPACITY = 15;

// CONSTRUCTOR
polynomial(double c = 0.0, int degree = 0);

// MEMBER FUNCTIONS
void add_to_coef(double amount, int degree);
void assign_coef(double c, int degree);
void clear();
double coefficient(int degree) const;
void print();
int degree() const;

private:
// DATA MEMBERS
double coef[CAPACITY];
int largest_degree;
};

#include "poly.h"
#include <cmath>
using namespace std;

static const int CAPACITY = 15;

// Start implementing your functions here
1
Expert's answer
2012-05-24T09:14:58-0400
#pragma once
#include <iostream>
class polynomial
{
public:
& //CONSTANT
& static const int CAPACITY = 15;
& // CONSTRUCTOR
& polynomial(double c = 0.0, int degree = 0);
& // MEMBER FUNCTIONS
& void add_to_coef(double amount, int degree);
& void assign_coef(double c, int degree);
& void clear();
& double coefficient(int degree) const;
& void print();
& int degree() const;
private:
& // DATA MEMBERS
& double coef[CAPACITY];
& int largest_degree;
};


#include "poly.h"
#include <cmath>
#include<process.h>
using namespace std;
polynomial::polynomial(double c , int degree )
{
if (degree<CAPACITY){
largest_degree=degree;
for(int i=0;i<=degree;i++)
coef[i]=c;
}
else cout<<"Bad degree";exit(0);//because it is very important to know
& //did we create element or not.

}

void polynomial::add_to_coef(double amount, int degree)
{
if(degree<=largest_degree)
coef[degree]+=amount;
else cout<<"bad range";
}

void polynomial::assign_coef(double c, int degree)
{
if(degree<=largest_degree)
coef[degree]=c;
else cout<<"bad range";
}

void polynomial::clear()
{
for (int i=0;i<largest_degree;++i)
coef[i]=0;
/*you may clear it by largest_degree=0; (it's up to you)*/
}

double polynomial::coefficient(int degree) const
{if(degree<=largest_degree)
return coef[degree];
else cout<<"bad range";
return -1;
}

void polynomial::print()
{for (int i=largest_degree;i>0;--i)
{if(coef[i]!=0)cout<<coef[i]<<"x^"<<i<<'+';}
if(coef[0]!=0)cout<<coef[0];
}

int polynomial::degree() const
{return largest_degree;}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS