Question: Based on the code which has given , we need to write down a polyder function which will give an array with the Coefficients of

Based on the code which has given , we need to write down a polyder function which will give an array with the Coefficients

of the derivative polynomia. please take a look at the description .

 Based on the code which has given , we need to

#include

#include

using namespace std;

// Test case:

// Degree of polynomial: 5

// Coefficients (real then imag for each):

// -1 2

// 2 -3

// 3 -4

// 4 0

// 0 5

// 1 1

// Value of x (real then imag): 3 -2

// p(3-2j) = 63-1089j

struct Complex {

float a; // Used to hold real part of complex number

float b; // Used to hold imag part of complex number

};

struct Poly {

Complex* c; // Coefficient array for poly (highest power first)

int deg; // Degree of poly

};

// Operator overloads for Complex structures

Complex operator+(Complex,Complex);

Complex operator+(Complex,float);

Complex operator+(float,Complex);

Complex operator*(Complex,Complex);

Complex operator*(Complex,float);

Complex operator*(float,Complex);

Complex operator^(Complex,int);

ostream& operator

istream& operator>>(istream&,Complex&);

// Initializer and operator overload for Poly

Poly polyInit(int);

istream& operator>>(istream&,Poly&);

// polyval function prototype

Complex polyval(Poly,Complex);

int main(void) {

int n;

Complex x, res;

cout

cin >> n;

Poly p = polyInit(n);

cout

cin >> p;

cout

cin >> x;

res=polyval(p,x);

cout

return 0;

}

// Write code for polyval function here --

Complex polyval(Poly p, Complex s) {

Complex r=p.c[p.deg];

for (int k=1;k

return r;

}

Poly polyInit(int sz) {

Poly p;

p.deg=sz;

p.c = new Complex[sz+1];

for (int i=0;i

p.c[i].a=0;

p.c[i].b=0;

}

return p;

}

istream& operator>>(istream& i, Poly& p) {

for (int k=0;k> p.c[k];

return i;

}

// Overload for float*Complex

Complex operator*(float bar, Complex foo) {

Complex bas;

bas.a = bar*foo.a;

bas.b = bar*foo.b;

return bas;

}

// Overload for Complex*float

Complex operator*(Complex foo, float bar) {

return bar*foo; // Use previous overload and commutativity

}

// Overload for Complex+Complex

Complex operator+(Complex foo, Complex bar) {

Complex bas;

bas.a = foo.a + bar.a;

bas.b = foo.b + bar.b;

return bas;

}

// Overload for Complex+float

Complex operator+(Complex foo, float bar) {

Complex bas;

bas.a = foo.a + bar;

bas.b = foo.b;

return bas;

}

// Overload for float+Complex

Complex operator+(float foo, Complex bar) {

return bar+foo; // Use previous overload and commutativity

}

// Overload for Complex*Complex

Complex operator*(Complex foo, Complex bar) {

Complex bas;

bas.a = foo.a*bar.a-foo.b*bar.b;

bas.b = foo.a*bar.b+bar.a*foo.b;

return bas;

}

// Overload for Complex^int (for exponentiation)

Complex operator^(Complex z, int pwr) {

Complex r={1};

for (int k=1;k

return r;

}

// Overload for output operator (

ostream& operator

o

if (z.b>0) {

o

}

else if (z.b

o

}

return o;

}

// Overload for input operator (>> Complex)

istream& operator>>(istream& i, Complex& z) {

i >> z.a >> z.b;

return i;

}

// Complex conjugate

Complex conj(Complex foo) {

Complex bar={foo.a,-foo.b};

return bar;

}

// Complex magnitude (absolute value)

float abs(Complex z) {

Complex res;

res = (z*conj(z))^0.5;

return res.a;

}

Replace the last cout line in your cpoly3.cpp code from Exercise #3 to be instead cout

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!