c. Add two polynomials. Prompt the user to enter two polynomials (as above in step a), then add these two polynomials and print out the result. e.g.: for the first polynomial, the user enters 3 for the degree of the polynomial and enters 7, 0, 9, 10 for each term from the highest degree to the lowest degree; for the second polynomial, the user enters 2 for the degree of the polynomial and enters 1, 2, 3 for each term from the highes degree to the lowest degree, then you have: 1st polynomial: 7x^3+9x+10 2nd polynomial: x^2+2x+3 After you add these two polynomials: your have 7x^3+x^2+11x+13.
here is a and b part plz solve c part
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <cmath>
using namespace std;
int main(){
/*Variable declarations*/
int higestDegree,higestDegree1;
int diffDegree,smallDegree;
std::stringstream polynomial,addpolynomial;
/*prompt for degree*/
std::cout<<"Please enter higest degree of polynomial"<<std::endl;
std::cin>>higestDegree;
std::cout<<"Please Enter highest degree of 2nd polynomial"<<std::endl;
std::cin>>higestDegree1;
int *cofficient=new int[higestDegree+1];//cofficient array creation for 1st polynomail
int *cofficient1=new int[higestDegree1+1];//cofficient array creation for 2st polynomail
/*taking cofficient for 1st polynomial*/
std::cout<<"Please enter cofficient of each term from higest degree to lowest for 1st polynomial"<<std::endl;
for(int i=0;i<=higestDegree;i++){
std::cin>>cofficient[i];
}
/*taking cofficient for 2st polynomial*/
std::cout<<"Please enter cofficient of each term from higest degree to lowest for 2nd polynomial"<<std::endl;
for(int i=0;i<=higestDegree1;i++){
std::cin>>cofficient1[i];
}
/*Polynomial creation*/
int degree=higestDegree;
for(int j=0;j<=higestDegree;j++){
if(cofficient[j]==0){
degree--;
continue;
}else{
if(j==higestDegree){
polynomial<<cofficient[j];
}else{
if(degree==1){
polynomial<<cofficient[j]<<"x"<<"+";
}else{
polynomial<<cofficient[j]<<"x^"<<degree<<"+";
}
}
degree--;
}
}
/*Printing Polynomial*/
std::cout<<polynomial.str()<<std::endl;
/*Prompt for user value*/
int xvalue;
std::cout<<"Please Enter x value"<<std::endl;
std::cin>>xvalue;
/*Evulating Expression*/
int value=0;
int deg=higestDegree;
for(int k=0;k<=higestDegree;k++){
value+=(cofficient[k]*pow(xvalue,deg));
deg--;
}
/*Printing polynomial evulated value*/
std::cout<<"The value of 1st polynomial is "<<"="<<value<<std::endl;
/*Adding two polynomial*/
if(higestDegree>higestDegree1){
degree=higestDegree;//keeping highest degree original
smallDegree=higestDegree1;//storing lower degree value
diffDegree=higestDegree-higestDegree1; //storing difference of degree
/*Running loop upto diff*/
for(int i=0;i<diffDegree;i++){
cout<<cofficient[i]<<"x^"<<degree<<"+";
degree--;
}
/*Loop for remaining adding of polynomial 1 and 2*/
for(int i=0;i<=smallDegree;i++){
if(i==smallDegree){
std::cout<<cofficient[diffDegree]+cofficient1[i]<<std::endl;
diffDegree++;
}else{
if(degree==1){
std::cout<<cofficient[diffDegree]+cofficient1[i]<<"x"<<"+";
}else{
std::cout<<cofficient[diffDegree]+cofficient1[i]<<"x^"<<degree<<"+";
}
degree--;
diffDegree++;
}
}
}else{
degree=higestDegree1;//keeping highest degree original
smallDegree=higestDegree;//storing lower degree value
diffDegree=higestDegree1-higestDegree; //storing difference of degree
/*Running loop upto diff*/
for(int i=0;i<diffDegree;i++){
cout<<cofficient1[i]<<"x^"<<degree<<"+";
degree--;
}
/*Loop for remaining adding of polynomial 1 and 2*/
for(int i=0;i<=smallDegree;i++){
if(i==smallDegree){
std::cout<<cofficient[i]+cofficient1[diffDegree]<<std::endl;
diffDegree++;
}else{
if(degree==1){
std::cout<<cofficient[i]+cofficient1[diffDegree]<<"x"<<"+";
}else{
std::cout<<cofficient[i]+cofficient1[diffDegree]<<"x^"<<degree<<"+";
}
degree--;
diffDegree++;
}
}
}
return 0;//returning to OS
}



Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images









