I am creating a polynomial calculator for 2 polynomials in C++. For different operations, I have created different functions. Below is my approach for the function for multiplication. For multiplying, first I must save all the possible degrees. This should mean a max of highest degree of polynomial1+polynomial2, and least of smallest degree of either. I am first just saving all the possible degree options taking into consideration polynomial1 and polynomial 2. I will then sort the degrees in descending order, perform calculations on coefficients, and save the coefficient according to the degree calculated.
However I am unable to properly save all the possible degree options, and can't spot the error. The values that are actually being saved, I can't seem to understand, why they are being saved, and where is the error. I am writing this program without utilising external libraries like for performing calculations. Kindly spot the error, and how I can successfully save all the possible degree options correctly. The comments are my understanding of what is happening/ should be happening. Thanks.
The ideal output should be: "99, 97, 95, 93, 91, 89, 98, 96, 94, 92, 90, 88...."
The current output is: "99 96 93 90 87 84 81 78 75 72 688904896 32629 119 114 109 104 97 94 89 84 79 74 1377809792 65258 218 210 202 194 184 178"
#include <iostream>
using namespace std;
void multiply(int degree_poly1[], int terms_poly1, int degree_poly2[], int terms_poly2, int degree_result[])
{
int temp=0;
for(int a=0; a<terms_poly1*terms_poly2; a++)
{//this is first term
//degrees
for(int b=0; b<=a; b++)
{//this is 2nd term
if(degree_result[b]==0){
//checks if location is empty or not
//dr[0]=0, so go on
temp=degree_poly1[a]+degree_poly2[b];
for(int c=0; c<=b; c++)
{
if(temp==degree_result[c]){
//checks if there is already a value temp in previous array throughout. yes means stop
break;
}
if(temp!=degree_result[c]){
//checks
degree_result[b]=temp;
}
}
temp=0;
}
}
}
for(int a=0; a<terms_poly1*terms_poly2; a++)
{
cout<<degree_result[a]<<" ";
}
}
int main()
{
int degree_poly11[10]={79,78,77,76,75}, terms_poly11=5, degree_poly22[10]={20,18,16,14,12}, terms_poly22=6, degree_resultt[100]={};
/*
The whole program saves a polynomial 5x^9 + 7x^3 + 2x^2 as coefficients 5,7,2 in coefficient1[0], coefficient1[1], coefficient1[2] AND the degrees 9,3,2 in degree_poly11[0], degree_poly11[1], degree_poly11[2]. terms_poly11 gives the no. of total terms in the equation i.e. 3.
This array is currently not present here but is present in complete program.
Similarly for another polynomial2 in coefficient22[] and degree_poly22[].
*/
multiply(degree_poly11, terms_poly11, degree_poly22, terms_poly22, degree_resultt);
return 0;
}