Question

#include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int...

#include <iostream>

using namespace std;
struct node
{
int base=0;
int power=0;
};

void insert(node ptr[],int basee,int powerr)
{
ptr[powerr].power=powerr;
ptr[powerr].base=basee;

}

void subtract(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
for(int j=0;j<=size1;j++)
{ if(ptr1[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr1[j].base);
ptr3[j].power=ptr2[j].power;
}

}
for(int j=0;j<=size2;j++)
{
if(ptr2[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)-(ptr2[j].base);
ptr3[j].power=ptr2[j].power;
}

}

}


void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
for(int j=0;j<=size1;j++)
{ if(ptr1[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr1[j].base);
ptr3[j].power=ptr2[j].power;
}

}
for(int j=0;j<=size2;j++)
{
if(ptr2[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr2[j].base);
ptr3[j].power=ptr2[j].power;
}

}

}


void display(node ptr[],int size)
{
if(ptr[0].base!=0){
cout<<ptr[0].base<<"+";}
for(int i=1; i<=size; i++)
{
if(ptr[i].base!=0){
cout<<ptr[i].base<<"x^"<<ptr[i].power<<"+";}
}
}
void multiply(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
  
}

int main()
{
bool choice1=true;
bool choice2=true;
int size1,size2,base1,base2,power1,power2;
cout<<"enter the max power in polynominal 1: ";
cin>>size1;
node *a= new node[size1+1];
while(choice1==true)
{
cout<<" enter the base,the power of x: ";
cin>>base1>>power1;
insert(a,base1,power1);
cout<<endl<<"1:write another value in this equation, 0:end"<<endl;
cin>>choice1;
}
display(a,size1);

cout<<"enter the max power in polynominal 2: ";
cin>>size2;
node *b= new node[(size2+1)];

while(choice2==true)
{
cout<<" enter the base,the power of x: ";
cin>>base2>>power2;
insert(b,base2,power2);
cout<<endl<<"1:write another value in this equation, 0:end"<<endl;
cin>>choice2;
}
display(b,size2); cout<<endl;

/*node *add=new node[(size1+size2+1)];


addition(a,size1,b,size2,add);
display(add,(size1+size2+1)); */

/* node *sub=new node[(size1+size2+1)];


subtract(a,size1,b,size2,sub);
display(sub,(size1+size2+1));
*/


}

I need a multiply function for this single variable polynomial arthimethic operations program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//The program works fine now...
//I have made a few changes
//I included a function make zeroes instead of assigning values directly inside the struct definition so that works better
//Then there was a small error in the code for multiplication.. Then I corrected it too..
#include <iostream>
using namespace std;
struct node
{
int base;
int power;
};
void make_zeros(node ptr[],int size1)
{
for(int i=0;i<=size1;++i)
{
ptr[i].base=0;
ptr[i].power=0;
}
}
void insert(node ptr[],int basee,int powerr)
{
ptr[powerr].power=powerr;
ptr[powerr].base=basee;
}

void subtract(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
for(int j=0;j<=size1;j++)
{
if(ptr1[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr1[j].base);
ptr3[j].power=ptr2[j].power;
}

}
for(int j=0;j<=size2;j++)
{
if(ptr2[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)-(ptr2[j].base);
ptr3[j].power=ptr2[j].power;
}

}
}
void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
for(int j=0;j<=size1;j++)
{
if(ptr1[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr1[j].base);
ptr3[j].power=ptr2[j].power;
}

}
for(int j=0;j<=size2;j++)
{
if(ptr2[j].base!=0)
{
ptr3[j].base=(ptr3[j].base)+(ptr2[j].base);
ptr3[j].power=ptr2[j].power;
}

}
}


void display(node ptr[],int size)
{
if(ptr[0].base!=0)
{
cout<<ptr[0].base<<"+";
}
for(int i=1; i<=size; i++)
{
if(ptr[i].base!=0)
{
cout<<ptr[i].base<<"x^"<<ptr[i].power<<"+";
}
}
}
void multiply(node ptr1[],int size1,node ptr2[],int size2,node ptr3[])
{
for(int i=0;i<=size1;i++)
{
for(int j=0;j<=size2;j++)
{
ptr3[ptr1[i].power+ptr2[j].power].base=ptr3[ptr1[i].power+ptr2[j].power].base+(ptr1[i].base*ptr2[j].base);
ptr3[ptr1[i].power+ptr2[j].power].power=ptr1[i].power+ptr2[j].power;
}
}
}
int main()
{
bool choice1=true;
bool choice2=true;
int size1,size2,base1,base2,power1,power2;
cout<<"Enter the max power in polynominal 1: ";
cin>>size1;
node *a= new node[size1+1];
make_zeros(a,size1+1);
while(choice1==true)
{
cout<<"Enter the base,the power of x: ";
cin>>base1>>power1;
insert(a,base1,power1);
cout<<endl<<"1:Write another value in this equation, 0:end"<<endl;
cin>>choice1;
}
display(a,size1);
cout<<"\nEnter the max power in polynominal 2: ";
cin>>size2;
node *b= new node[(size2+1)];
make_zeros(b,size2+1);
while(choice2==true)
{
cout<<"Enter the base,the power of x: ";
cin>>base2>>power2;
insert(b,base2,power2);
cout<<endl<<"1:Write another value in this equation, 0:end"<<endl;
cin>>choice2;
}
display(b,size2);
cout<<endl;

/*node *add=new node[(size1+size2+1)];


addition(a,size1,b,size2,add);
display(add,(size1+size2+1)); */

/* node *sub=new node[(size1+size2+1)];


subtract(a,size1,b,size2,sub);
display(sub,(size1+size2+1));
*/
node *mul=new node[(size1+size2+1)];
make_zeros(mul,size1+size2+1);
multiply(a,size1,b,size2,mul);
display(mul,(size1+size2+1));
}

If there is any code then refer to the images given for the proper indentation of the code

Hope this helps
In case of any queries feel free to ask me in the comments section below
Happy Coding :) :)
Have a good day

Add a comment
Know the answer?
Add Answer to:
#include <iostream> using namespace std; struct node { int base=0; int power=0; }; void insert(node ptr[],int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int...

    #include <iostream> using namespace std; struct node { int base; int power; }; void insert(node ptr[],int basee,int powerr) { ptr[powerr].power=powerr; ptr[powerr].base=basee; } void addition(node ptr1[],int size1,node ptr2[],int size2,node ptr3[],int size3) { for(int j=0;j<=size1;j++) { ptr3[j].base=ptr3[j].base+ptr1[j].base; } for(int j=0;j<=size2;j++) { ptr3[j].base=ptr3[j].base+ptr2[j].base; } } void display(node ptr[],int size) { if(ptr[0].base!=0) cout<<ptr[0].base<<"+"; for(int i=1; i<=size; i++) { if(ptr[i].base!=0) cout<<ptr[i].base<<"x^"<<ptr[i].power<<"+"; } } int main() { bool choice1=true; bool choice2=true; int size1,size2,base1,base2,power1,power2; cout<<"enter the max power in polynominal 1"; cin>>size1; node *a= new node[size1+1]; for(int...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int...

    #include <iostream> #include <cstdlib> using namespace std; int **dynArray(int row, int cols) { int **myPtr; int lab[4]; myPtr = new int *[row]; for(int i = 0; i < row; i++) myPtr[i] = new int[lab[i]]; for(int i = 0; i<row ; i++) if(myPtr[i] == 0) cout<<"empty"; return myPtr; } void getinput(int ID,int &Station,int &labnumb) { cout<<" Enter your ID number: "<<endl; cin>>ID; cout<<" Enter your station number: "<<endl; cin>>Station; cout<<" Enter your lab number: "<<endl; cin>>labnumb; return; } void logout(int ID,int...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int...

    #include <iostream> #include <queue> using namespace std; class Graph { public: Graph(int n); ~Graph(); void addEdge(int src, int tar); void BFTraversal(); void DFTraversal(); void printVertices(); void printEdges(); private: int vertexCount; int edgeCount; bool** adjMat; void BFS(int n, bool marked[]); void DFS(int n, bool marked[]); }; Graph::Graph(int n=0) { vertexCount = n; edgeCount = 0; if(n == 0) adjMat = 0; else { adjMat = new bool* [n]; for(int i=0; i < n; i++) adjMat[i] = new bool [n]; for(int i=0;...

  • Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

    Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1, int j=2,int k=3, double product =1.0) { i+=2; j*=2; k/=2; product=i*j*k; } void Magic(int& i, int& j, double& product) { i+=2; j=j*2+2; product=i*j; } void Magic(int* i,int* j) { double product; *i+=2; *j=*j*2+2; product=*i * *j; } int main() { double product; int i=0,j=0,k=0; product=i*j*k;    Magic(); cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;    Magic(2,4); cout<<"i, j, k and...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; }...

    C++ #include <iostream> using namespace std; bool checkinventoryid(int id) {    return id > 0; } bool checkinventoryprice(float price) {    return price > 0; } void endProgram() {    system("pause");    exit(0); } void errorID(string str) {    cout << "Invalid Id!!! " << str << " should be greater than 0" << endl; } void errorPrice(string str) {    cout << "Invalid Price!!!" << str << " should be greater than 0" << endl; } int inputId() {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT