Question

In: Computer Science

Hi, I have this code so far and need to modify it so that the output...

Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 =

For example if I input (assuming there is only one coefficient for each polynomial)

Coefficient 1 for polynomial 1: 1 1

Coefficient 1 for polynomial 2: 1 1

You get no output instead of 0.

Could you please edit my code so that it shows this, or teach me in detail of how to change this. Thank you.

#include <cmath>

#include <iostream>

using namespace std;

const int DEFAULTPOLY = 10; //Default size of our dynamic coefficient array

//DO NOT modify the class header

class Poly
{

private:
//Data members

int arraySize; //size of array

int *coeff; //dynamic array

public:

Poly();

Poly(int size);

Poly(const Poly& aPoly);

~Poly();

const Poly& operator=(const Poly& aPolt);

void grow(int newSize);

int degree() const;

void setCoeff(int value, int i);

int getCoeff(int i) const;

void negate();

friend Poly operator+(const Poly& aPoly, const Poly& bPoly);

friend Poly operator-(const Poly& aPoly, const Poly& bPoly);

friend bool operator==(const Poly& aPoly, const Poly& bPoly);

friend ostream& operator<<(ostream& out, const Poly &aPoly);

};

//default constructor
Poly::Poly()
{
arraySize = DEFAULTPOLY;
coeff = new int[DEFAULTPOLY];
for(int i=0; i<DEFAULTPOLY; i++)
coeff[i] = 0;
}

//constructor
Poly::Poly(int size)
{
arraySize = size;
coeff = new int[size];
for(int i=0; i<size; i++)
coeff[i] = 0;

}

//copy constructor
Poly::Poly(const Poly& aPoly)
{
arraySize = aPoly.arraySize;

if(coeff){

delete [] coeff;}

coeff = new int[arraySize];

for(int i=0; i<arraySize; i++)
coeff[i] = aPoly.coeff[i];
}

//destructor
Poly::~Poly()
{
delete []coeff;
}

// = operator overloading
const Poly& Poly::operator=(const Poly& aPoly)
{
if(this == &aPoly){

return *this;}

if(coeff){

delete [] coeff;}

arraySize = aPoly.arraySize;

coeff = new int[arraySize];

for(int i=0; i<arraySize; ++i)
{
coeff[i] = aPoly.getCoeff(i);
}
return *this;
}

// function to grow the polynomial
void Poly::grow(int newSize)
{
if(arraySize>=newSize) return;

Poly temp(newSize);

for(int i=arraySize; i<newSize; i++)
temp.coeff[i] = 0;

for(int i=0; i<arraySize; i++)
temp.coeff[i] = coeff[i];

delete []coeff;

coeff = temp.coeff;
}

// function to return the degree of the polynomial
int Poly::degree() const
{
int i;
for(i=DEFAULTPOLY-1; coeff[i]==0; i--);

return i;
}

// function to set the coefficient of the polynomial
void Poly::setCoeff(int value, int i)
{
if(i>arraySize) grow(i);

coeff[i] = value;
}

// function to return coefficient of the polynomial
int Poly::getCoeff(int i) const
{
if(i>=arraySize) return 0;

return coeff[i];
}

// function to negate the polynomial
void Poly::negate()
{
for(int i=0; i<arraySize; i++)
coeff[i] = -coeff[i];

}

// + operator overloading
Poly operator+(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;

int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] + bPoly.coeff[i];

return temp;

}

// - operator overloading
Poly operator-(const Poly& aPoly, const Poly& bPoly)
{
Poly temp;

int i;
for(i=0; i<DEFAULTPOLY-1; i++)
temp.coeff[i] = aPoly.coeff[i] - bPoly.coeff[i];

return temp;
}

// == operator overloading
bool operator==(const Poly& aPoly, const Poly& bPoly)
{
int i;

for(i=0; i<DEFAULTPOLY; i++)
if(aPoly.coeff[i]!=bPoly.coeff[i]) return false;

return true;
}

// << operator overloading
ostream& operator<<(ostream& out, const Poly &aPoly)
{
for(int i=aPoly.degree(); i>=0; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<" ";
}
out<<endl;

return out;
}

/////////////////////////////////////////////////////////////////////////////////////////
int main() {

int numCoeff, coeffValue, coeffDegree, x;

Poly poly1, poly2;

//prompt user for number of coefficients

cout << "How many coefficients for polynomial 1?" << endl;

cin >> numCoeff;


for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 1:";

cin >> coeffValue >> coeffDegree;

poly1.setCoeff(coeffValue, coeffDegree);

}


cout << endl << "How many coefficients for polynomial 2?" << endl;

cin >> numCoeff;


for(int i=0; i<numCoeff; ++i)

{

cout << "Coefficient " << i+1 << " for polynomial 2:";

cin >> coeffValue >> coeffDegree;

poly2.setCoeff(coeffValue, coeffDegree);

}


//sample test cases for degree and operator <<

cout << endl << "Polynomial 1 = " << poly1 << endl;

cout << "Polynomial 1 has degree " << poly1.degree() << endl;

cout << "Polynomial 2 = " << poly2 << endl;

cout << "Polynomial 2 has degree " << poly2.degree() << endl;

//Sample test cases for operator+ and operator-

cout << endl << "Polynomial 1 + Polynomial 2 = " << poly1 + poly2 << endl;

cout << "Polynomial 1 - Polynomial 2 = " << poly1 - poly2 << endl << endl;

//Sample test cases for operator ==

if(poly1 == poly2)

{

cout << "Two polynomials are the same." << endl;

}

else

{

cout << "Two polynomials are different." << endl;

}

return 0;

}

Solutions

Expert Solution

  • For the output in case of subtraction we can use this :


// << operator overloading
ostream& operator<<(ostream& out, const Poly &aPoly)
{
   bool check=false;
for(int i=aPoly.degree(); i>=1; i--)
{
if(aPoly.coeff[i]==0) continue;
if(i==aPoly.degree() || aPoly.coeff[i]<0)
out<<aPoly.coeff[i]<<"X^"<<i<<" ";
else
out<<"+ "<<aPoly.coeff[i]<<"X^"<<i<<" ";
check=true;
}
if(check==false||aPoly.coeff[0]!=0)
{
   if(check==true)
   out<<"+";
   out<<aPoly.coeff[0];
  
}
out<<endl;

return out;
}

As you were printing the x^0 with its coefficient within the loop,instead i took it separately.

And i used a check flag to see if the degree of polynomial1 - polynomial2 was zero or non zero.

  • And i strongly suggest you you to keep in check the degree of polynomial in case of + and - operations.

Related Solutions

hi i need a code that will give me this output, For the multiply_list, the user...
hi i need a code that will give me this output, For the multiply_list, the user will be asked to input the length of the list, then to input each element of the list. For the repeat_tuple, the user is only asked to enter the repetition factor, but not the tuple. Your program should take the list created before and convert it to a tuple. output expected: (**user input**) ******Create your List ****** Enter length of your list: 3 ******...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
Hi, I need a Matlab code satisfies the following question. Output should be a graph similar...
Hi, I need a Matlab code satisfies the following question. Output should be a graph similar to a sine or cosine graph or similar to electrocardiograms (EKG/ECG) graph. Please I'd appreciate it. All love. Thanks :) In elementary quantum mechanics, the square well is used to model the behavior of a bound particle, in which one or more forces (external potentials, interaction with other particles, etc) prevent or restrict its ability to move about. We have seen in class that...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
I have solved most of question and the answers I have so far are correct, I...
I have solved most of question and the answers I have so far are correct, I just need help finding out the lower bound and upper bound (All answers were generated using 1,000 trials and native Excel functionality.) Galaxy Co. distributes wireless routers to Internet service providers. Galaxy procures each router for $75 from its supplier and sells each router for $125. Monthly demand for the router is a normal random variable with a mean of 100 units and a...
Hi, I have created the following code and I was wondering if it is possible to...
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you. #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10;...
Hi I have a java code for my assignment and I have problem with one of...
Hi I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError Slice method spec: Method Name: slice Return Type: Tuple (with proper generics) Method Parameters: Start (inclusive) and stop (exclusive) indexes. Both of these parameters are "Integer" types (not "int" types). Like "get" above, indexes may be positive or negative. Indexes may be null. Description: Positive indexes work in the normal way Negative indexes are described...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT