Hi, I am having trouble coding. I need to add two polynomials in an array based
ID: 3723436 • Letter: H
Question
Hi, I am having trouble coding. I need to add two polynomials in an array based list in C++.
#include <iostream>
#include<iomanip>
#include<math.h>
using namespace std;
void print_polynomials(double* coe, int l)
{
for(int i = 0 ; i < l ; i++ )
{
if(coe[i]!=0 )
{
if(coe[i] >= 0 )
{
if(i!=0)
cout<< "+";
}
else
{
cout<< "-";
}
cout << coe[i];
if(l - i -1 != 0)
cout<<"x^"<< l - i -1;
}
}
}
void print_2ndpolynomials(double* seccoe, int len)
{
for(int j = 0 ; j < len ; j++ )
{
if(seccoe[j]!=0 )
{
if(seccoe[j] >= 0 )
{
if(j!=0)
cout<< "+";
}
else
{
cout<< "-";
}
cout << seccoe[j];
if(len - j -1 != 0)
cout<<"x^"<< len - j -1;
}
}
}
double evaluate(double* coe, int l, double x)
{
double sum = 0;
for(int i = 0 ; i < l ; i++ )
{
sum+= coe[i] * pow(x, l - i -1);
}
return sum;
}
double add (double coe[] ,double seccoe,int l,int len)
{
}
int main ()
{
int n, l , sec,len;
double x;
// first polynomial
cout<<"Enter the degree of the polynomial:";
cin>>n;
l = n + 1;
double* coe = new double[l];
cout<< "Enter the coefficient for each term from highest to lowest:" ;
for(int i = 0 ; i < l; i++ )
{
cin>>coe[i];
}
//printing first polynomial
print_polynomials(coe, l);
cout<<endl;
//evaluate x
cout<<"Enter the value of x :";
cin>> x;
double result = evaluate(coe, l, x);
cout<<"result:"<<result;
delete [] coe;
cout<<endl;
//second polynomial
cout<<"Enter the degree of the second polynomial:";
cin>>sec;
len = sec + 1;
double* seccoe = new double[l];
cout<< "Enter the coefficient for the second polynomial each term from highest to lowest:" ;
for(int j = 0 ; j < len; j++ )
{
cin>>seccoe[j];
}
//printing second polynomial
print_2ndpolynomials(seccoe, len);
delete [] seccoe;
This is my current code. Please help me add tow polynomials with this code.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void print_polynomials(double* coe, int l)
{
for (int i = 0; i < l; i++) {
if (coe[i] != 0) {
if (coe[i] >= 0) {
if (i != 0)
cout << "+";
}
else {
cout << "-";
}
cout << coe[i];
if (l - i - 1 != 0)
cout << "x^" << l - i - 1;
}
}
}
void print_2ndpolynomials(double* seccoe, int len)
{
for (int j = 0; j < len; j++) {
if (seccoe[j] != 0) {
if (seccoe[j] >= 0) {
if (j != 0)
cout << "+";
}
else {
cout << "-";
}
cout << seccoe[j];
if (len - j - 1 != 0)
cout << "x^" << len - j - 1;
}
}
}
double evaluate(double* coe, int l, double x)
{
double sum = 0;
for (int i = 0; i < l; i++) {
sum += coe[i] * pow(x, l - i - 1);
}
return sum;
}
double *add(double coe[], double seccoe[], int l, int len)
{
int max_len = l > len ? l : len;
double *arr = new double[max_len];
double c1, c2;
for(int i = 0; i < max_len; ++i) {
c1 = 0;
c2 = 0;
if(i < l) {
c1 = coe[i];
}
if(i < len) {
c2 = seccoe[i];
}
arr[i] = c1+c2;
}
return arr;
}
int main()
{
int n, l, sec, len;
double x;
// first polynomial
cout << "Enter the degree of the polynomial:";
cin >> n;
l = n + 1;
double* coe = new double[l];
cout << "Enter the coefficient for each term from highest to lowest:";
for (int i = 0; i < l; i++) {
cin >> coe[i];
}
//printing first polynomial
print_polynomials(coe, l);
cout << endl;
//evaluate x
cout << "Enter the value of x :";
cin >> x;
double result = evaluate(coe, l, x);
cout << "result:" << result;
delete[] coe;
cout << endl;
//second polynomial
cout << "Enter the degree of the second polynomial:";
cin >> sec;
len = sec + 1;
double* seccoe = new double[l];
cout << "Enter the coefficient for the second polynomial each term from highest to lowest:";
for (int j = 0; j < len; j++) {
cin >> seccoe[j];
}
//printing second polynomial
print_2ndpolynomials(seccoe, len);
delete[] seccoe;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.