#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 i=0; i<size1+1; i++)
a[i].base=0;
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];
for(int i=0; i<size2+1; i++)
b[i].base=0;
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);
node *add=new node[size1+size2+1];
for( int i=0; i<size2+1+size1; i++)
add[i].base=0;
addition(a,size1,b,size2,add,size2+size1+1);
display(add,size1+size2+1);
}
addition isnt working correctly. the individual a and b are storing it perfectly by the addition isnt working. please also explain the reason why its not giving accurate value as well as fix it
Please Upvote, If you are impressed with the answer
If you have any doubt, you can comment. I will explain
Answer:
Code after fixing the errors in the given code:
There are some indentation errors in the code, because of those errors in some lines of code result in wrong output, I fixed those errors , please check the below code and run in any online C++ compiler for better experience
#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 i=0; i<(size1+1); i++)
a[i].base=0;
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)];
for(int i=0; i<(size2+1); i++)
b[i].base=0;
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);
node *add=new node[(size1+size2+1)];
for( int i=0; i<(size2+1+size1); i++)
add[i].base=0;
addition(a,size1,b,size2,add,(size2+size1+1));
display(add,(size1+size2+1));
}
Output:
#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=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)...
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 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 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 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> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
#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" <<...
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 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...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...