Uploaded by tahirabdullah12

Computer Programming Assignment Bahria University Lahore

advertisement
Enrollment Number: 03-134231-062
Enrollment Number: 03-134231-062
Name: Zohaib Abdullah
Enroll No: 03-134231-062
Class: BS-CS 23
CODE
#include <iostream>
#include <cmath>
using namespace std;
//function for the evaluation of equation
double solution(double x, int power) {
double result = 0.0;
double coeff;
for (int i = 0; i <= power; i++) {
cout << "Enter the coefficient of x^" << power - i << ": ";
cin >> coeff;
result += coeff * pow(x, power - i);
}
return result;
}
//function for multiplying two polynomial equations
void multiply(int xpower2, int xpower1, int xpower0, int xpowersec2, int xpowersec1, int xpowersec0) {
int m4, m3, m2, m1, m0;
m4 = xpower2 * xpowersec2;
m3 = xpower2 * xpowersec1 + xpower1 * xpowersec2;
m2 = xpower2 * xpowersec0 + xpower1 * xpowersec1 + xpower0 * xpowersec2;
m1 = xpower1 * xpowersec0 + xpower0 * xpowersec1;
m0 = xpower0 * xpowersec0;
// print the result on screen
cout << "The product of the polynomials is: " << endl;
cout << m4 << "x^4 + " << m3 << "x^3 + " << m2 << "x^2 + " << m1 << "x + " << m0 << endl;
}
int main() {
int power;
double x;
cout << "Name: Zohaib Abdullah" << endl;
cout << "EnrNo.: 03-134231-062" << endl;
cout << "---------------------------------------------" << endl;
cout << "Program to Evaluate a polynomial equation" << endl << endl;
cout << "Enter the values for polynomial " << endl;
cout << "Enter the degree of the polynomial: ";
cin >> power;
Enrollment Number: 03-134231-062
cout << "Enter the value of x: ";
cin >> x;
//function calling
double result = solution(x, power);
cout << "The value of the polynomial at x = " << x << " is " << result << endl;
cout << "------------------------------------------" << endl;
//second task, to perform operations on two equations
int menu = 1;
int xpower4, xpower3, xpower2, xpower1, xpower0;
int xpowersec4, xpowersec3, xpowersec2, xpowersec1, xpowersec0;
cout << "Menu Driven Program to perform operations between two polynomial equations" << endl << endl;
//taking first equation from the user
cout << "=>" << "Enter the values for first equation" << endl;
cout << "Enter the coefficient of x^4: ";
cin >> xpower4;
cout << "Enter the coefficient of x^3: ";
cin >> xpower3;
cout << "Enter the coefficient of x^2: ";
cin >> xpower2;
cout << "Enter the coefficient of x^1: ";
cin >> xpower1;
cout << "Enter the coefficient of x^0: ";
cin >> xpower0;
cout <<"Equation one is: " << xpower4 << "x^4 " << "+ " << xpower3 << "x^3 " << "+ " << xpower2 << "x^2" << "+ "
<< xpower1 << "x" << "+ " << xpower0 << endl;
//taking second equation from the user
cout << "=>" << "Enter the values for second equation" << endl;
cout << "Enter the coefficient of x^4: ";
cin >> xpowersec4;
cout << "Enter the coefficient of x^3: ";
cin >> xpowersec3;
cout << "Enter the coefficient of x^2: ";
cin >> xpowersec2;
cout << "Enter the coefficient of x^1: ";
cin >> xpowersec1;
cout << "Enter the coefficient of x^0: ";
cin >> xpowersec0;
cout << "Equation two is: " << xpowersec4 << "x^4 " << "+ " << xpowersec3 << "x^3 " << "+ " << xpowersec2 <<
"x^2" << "+ " << xpowersec1 << "x" << "+ " << xpowersec0 << endl;
cout << "--------------------------------------------------" << endl;
//menu to choose from
cout << "Enter the operation you want to perform" << endl;
cout << "Press 1 for Adding first and second Polynomial Equation" << endl;
cout << "Press 2 for subtracting second Polynomial Equation from first" << endl;
cout << "Press 3 for Multiplying first and second Polynomial Equation" << endl << endl;
cout << "Enter the operation you want to perform (1-3) : ";
cin >> menu;
Enrollment Number: 03-134231-062
switch (menu)
{
case 1:
cout << "The result after Addition will be: " << endl;
cout << xpower4 + xpowersec4 << "x^4 " << "+ " << xpower3 + xpowersec3 << "x^3 " << "+ " << xpower2 +
xpowersec2 << "x^2" << "+ " << xpower1 + xpowersec1 << "x" << "+ " << xpower0 + xpowersec0 << endl;
break;
case 2:
cout << "The result after Subtraction will be: " << endl;
cout << xpower4 - xpowersec4 << "x^4 " << "+ " << xpower3 - xpowersec3 << "x^3 " << "+ " << xpower2 xpowersec2 << "x^2" << "+ " << xpower1 + xpowersec1 << "x" << "+ " << xpower0 - xpowersec0 << endl;
break;
case 3:
cout << "The result after Multiplication will be: " << endl;
//calling the multiply function
multiply(xpower2, xpower1, xpower0, xpowersec2, xpowersec1, xpowersec0);
break;
default:
cout << "Invalid Menu";
}
return 0;
}
OUTPUT
Operation performed : Addition
Enrollment Number: 03-134231-062
Operation Performed : Subtaction
Enrollment Number: 03-134231-062
Operation Performed : Multiplication
Enrollment Number: 03-134231-062
Download