You are on page 1of 2

#include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct term {
double coeff;
int degree;
};

struct term ARR[250];


int last = 0; // This will not be the index of term with the last element but the
one after that that we will use next in any operation...
void quickSort(int arr[], int left, int right) ;
void takeInput(int nuTerm, int start ){
// nuTerm = no. of terms, start = starting element in the ARR
for (int i=start; i<nuTerm+start;i++){
cin >> ARR[i].degree ;
}
for (int i=start; i<nuTerm+start;i++){
cin >> ARR[i].coeff ;
}
last += nuTerm;
}

int addLast; // Last element of addition i.e. whatever the last index of addition
is +1
void add (int x, int y ){
bool common;
for (int i=0;i<x;i++){
common = false;
for (int j=x; j<x+y; j++){
if (ARR[i].degree == ARR[j].degree){
ARR[last].degree = ARR[i].degree;
ARR[last].coeff = ARR[i].coeff + ARR[j].coeff;
common = true;
last ++;
}
}
if (common == false){
ARR[last] = ARR[i];
last ++;
}
}
for (int i=x;i<x+y;i++){
common = false;
for (int j=0; j<x; j++){
if (ARR[i].degree == ARR[j].degree){
common = true;
}
}
if (common == false){
ARR[last] = ARR[i];
last ++;
}
}
addLast = last;
}
int multLast;
void multiply (int x, int y){
for (int i=addLast;i<250;i++){
ARR[i].coeff=0;
ARR[i].degree=0;
}
for (int i=0;i<x;i++){
for(int j=x;j<x+y;j++){
ARR[last].coeff+=ARR[i].coeff*ARR[j].coeff;
ARR[last].degree=ARR[i].degree+ARR[j].degree;
last++;

}
}
multLast=last;
}

int main(){

int x; int y;

cin >> x ;
takeInput (x, 0);

cin >> y ;
takeInput (y,x);

add (x,y);

multiply (x,y);

// Output Code

for (int i = x+y; i<addLast; i++){


cout << ARR[i].degree << " ";
} cout << "\n";
for (int i = x+y; i<addLast; i++){
cout << ARR[i].coeff << " ";
} cout << "\n";
for (int i = addLast; i<multLast; i++){
cout << ARR[i].degree << " ";
} cout << "\n";
for (int i = addLast; i<multLast; i++){
cout << ARR[i].coeff << " ";
} cout << "\n";

You might also like