






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
. Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x4 has the coefficient 2 and the exponent 4. The index number of an array represents the exponent. โข Develop a complete class containing Make a Class Named Vector3D that represents a vector in 3D and it contains following Private data members I, J and K (of type double), with default values of 0.
Typology: Assignments
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Q1. Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x^4 has the coefficient 2 and the exponent 4. The index number of an array represents the exponent. ๏ท Develop a complete class containing proper constructor and destructor functions as well as set and get functions. ๏ท The class should also provide the following overloaded operator capabilities: o Overload the addition operator ( + ) to add two Polynomials. o Overload the subtraction operator ( - ) to subtract two Polynomials. o Overload the equal operator ( == ) to compare two Polynomials. o Overload the assignment operator to assign one Polynomial to another. o Overload the multiplication operator ( ***** ) to multiply two Polynomials. o Overload the addition assignment operator ( += ), subtraction assignment operator ( -= ), and multiplication assignment operator ( *= ). #include
bool operator ==(polynomial); void print(int i = 4){ for ( ; i >= 0 ; i-- ){ if ( i == 0 && co[i] > 0 ) { cout << " +"; cout << co[i] ;} else if ( i == 0 && co[i] < 0 ) { cout << " "; cout << co[i] ;} else if ( co[i] > 0) { cout << " +"; cout << co[i] << "x^" << i ;} else if ( co[i] < 0) { cout << " "; cout << co[i] << "x^" << i ;} } cout << endl; } }; polynomial polynomial :: operator + (polynomial P2) { polynomial P3; for ( int i = 0 ; i <= 4 ; i++ ){ P3.co [i] = co [i] + P2.co [i] ; } return P3; } polynomial polynomial :: operator - (polynomial P2) { polynomial P3; for ( int i = 0 ; i <= 4 ; i++ ){ P3.co [i] = co [i] - P2.co [i] ; } return P3; } polynomial polynomial :: operator *(polynomial P2){ polynomial P3; for( int i = 0 ; i <= 4 ; i++ ){ for( int j = 0 ; j <= 4 ; j++ ){ P3.co [i+j] += co [i] * P2.co [j] ; } } return P3; } bool polynomial :: operator ==(polynomial P2 ) { for (int i=0 ; i<=4 ; i++){
cout <<"Addition of two Polynomial is \n"; P3.print (); P3 = P1 - P2; cout <<"Subtraction of two polynomial is \n"; P3.print (); P3= P1 * P2; cout <<endl << "Multiplication of two polynomial is
n"; P3.print (9); system ("PAUSE"); return 0; } Q2. Create a class Polar that represents the points on the plain as polar coordinates ( radius and angle ). ๏ท Create an overloaded + operator for addition of two Polar quantities. ๏ท โAddingโ two points on the plain can be accomplished by adding their X coordinates and then adding their Y coordinates. ๏ท This gives the X and Y coordinates of the โanswer.โ ๏ท Thus youโll need to convert two sets of polar coordinates to rectangular coordinates, add them, then convert the resulting rectangular representation back to polar. #include
radius = r; angle = t; } void show_polar () { cout << "Radius = "<< radius <<" , Theeta "<< angle <<" Degrees"<< endl; } polar operator + (polar a) { double r,t; double x1,x2, y1,y2, x3,y3; x1 = radius * cos( Piangle / 180); y1 = radius * sin( Piangle / 180); x2 = a.radius * cos(Pia.angle / 180); y2 = a.radius * sin(Pia.angle / 180); x3 = x1 + x2; y3 = y1 + y2; r = sqrt( x3x3 + y3y3 ); t = atan(y3/x3); t = t*180 / Pi; // convert to degrees return polar (r,t); } }; int main() { polar P1(3,0); polar P2(4,90); cout <<"For P1: "; P1.show_polar(); cout <<"For P2: "; P2.show_polar(); polar P3 = P1 + P2; cout <<"P1+P2 = : ";P3.show_polar(); system("PAUSE"); return 0; }
return (sqrt ((ii) + (jj) + (kk))); } }; Vector3D Vector3D :: operator + (Vector3D v2) { Vector3D v3; v3.i = i + v2.i; v3.j = j + v2.j; v3.k = k + v2.k; return v3; } Vector3D Vector3D :: operator - (Vector3D v2) { Vector3D v3; v3.i = i - v2.i; v3.j = j - v2.j; v3.k = k - v2.k; return v3; } double Vector3D :: operator * (Vector3D v2) { // Dot Product return ( iv2.i + jv2.j + kv2.k ); } Vector3D Vector3D :: operator / (Vector3D v2) { // Cross Product Vector3D v3; v3.i = (j * v2.k) - (k * v2.j); v3.j = (k * v2.i) - (i * v2.k); v3.k = (i * v2.j) - (j * v2.i); return v3; } Vector3D Vector3D :: operator += (Vector3D v2) { i += v2.i; j += v2.j; k += v2.k; return Vector3D(i,j,k); } Vector3D Vector3D :: operator -= (Vector3D v2) { i -= v2.i; j -= v2.j; k -= v2.k; return Vector3D(i,j,k); } Vector3D Vector3D :: operator /= (Vector3D v2) { Vector3D v3; v3.i = j * v2.k - k * v2.j; v3.j = k * v2.i - i * v2.k; v3.k = i * v2.j - j * v2.i;
i = v3.i; j = v3.j; k = v3.k; return v3; } int main(){ Vector3D v1,v2, v3; cout << "Enter Values of 1st Vector: "<< endl; v1.getvalues(); cout << "\nEnter Values of 2nd Vector: "<< endl; v2.getvalues(); cout << "\n1st vector is "; v1.print(); cout <<"2nd vector is "; v2.print(); cout << "\nFor 1st vector, Length = " << v1.length()<<endl; cout << "For 2nd vector, Length = " << v2.length()<<endl; v3= v1 + v2; cout <<"\nAnswer of Addition is = "; v3.print (); v3= v1 - v2; cout <<"Answer of Subtraction is = "; v3.print (); cout << "\nDot product is = " << v1*v2 <<endl; v3 = v1 / v2; cout <<"Cross product is = "; v3.print (); v1 /= v2; cout <<"Cross product is = "; v1.print (); system ("PAUSE"); return 0; } Q4. Modify the Program at slide number 48 for 3D Polar to 3D Cartesian Coordinates conversion.
void Show() const{ cout << "X = " << X <<", Y = " << Y << ", Z = " << Z <<endl; } }; int main(){ Polar Pol(5,53.13, 45); // R = 5, Theeta = 53. Degrees, Psi = 45 Degrees Cartesian Cat = Pol; // Convert Polar to Cartesian Pol.Show(); // Show in Polar Cat.Show(); // Show in Cartesian system("PAUSE"); return 0; }