Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- int A[100][100], B[100][100], C[100][100],n1,n2,n3=0;
- int arr_len(int array[][100])
- {
- int len=0;
- for(int i=0;array[i][0]!='\0';i++)
- len++;
- return len;
- }
- void sort_polynomial(char array)//used to sort the polynomial, such that highest degree term always stays on the left
- {
- int coeff,deg,i,j;
- switch (array)
- {
- case 'A':
- {
- for(i=0;i<arr_len(A);i++)
- {
- for(j=0;j<arr_len(A)-1;j++)
- {
- if(A[j][1]<A[j+1][1])
- {
- coeff=A[j][0];
- deg=A[j][1];
- A[j][0]=A[j+1][0];
- A[j][1]=A[j+1][1];
- A[j+1][0]=coeff;
- A[j+1][1]=deg;
- }
- }
- }
- }
- break;
- case 'B':
- {
- for(i=0;i<arr_len(B);i++)
- {
- for(j=0;j<arr_len(B)-1;j++)
- {
- if(B[j][1]<B[j+1][1])
- {
- coeff=B[j][0]; //swapping both coefficient and degree
- deg=B[j][1];
- B[j][0]=B[j+1][0];
- B[j][1]=B[j+1][1];
- B[j+1][0]=coeff;
- B[j+1][1]=deg;
- }
- }
- }
- }
- break;
- default:
- {
- for(i=0;i<arr_len(C);i++)
- {
- for(j=0;j<arr_len(C);j++)
- {
- if(C[j][1]<C[j+1][1])
- {
- coeff=C[j][0];
- deg=C[j][1];
- C[j][0]=C[j+1][0];
- C[j][1]=C[j+1][1];
- C[j+1][0]=coeff;
- C[j+1][1]=deg;
- }
- }
- }
- }
- }
- }
- void padd()
- {
- int i=0,k=0,found=0;
- while(i<n1)
- {
- for(int j=0;j<n2;j++)
- {
- if(A[i][1]==B[j][1]) //checking if degree matches. if yes then add the coeff
- {
- found=1;
- C[k][0]=A[i][0]+B[j][0];
- C[k++][1]=A[i][1];
- }
- }
- if(!found)//if the degree present in polynomial A has no match with B, then just include the term in c
- {
- C[k][0]=A[i][0];
- C[k++][1]=A[i][1];
- }
- else
- found=0;
- i++;
- }
- i=0;
- while(i<n2)//to add the terms from polynomial B to C, where degree of B is not present in A
- {
- for(int j=0;j<arr_len(C);j++)
- {
- if(C[j][1]==B[i][1])
- found=1;
- }
- if(!found)//if any term whose degree is not found in B, then it is added to C directly
- {
- C[k][0]=B[i][0];
- C[k++][1]=B[i][1];
- }
- else
- found=0;
- i++;
- }
- sort_polynomial('C');
- printf("\n\nThe result after adding the the polynomial A and B is: ");
- for(int i=0;C[i][0]!='\0';i++)
- n3++;
- for(int i=0;i<n3;i++)
- {
- if(C[i][1]==0)
- {
- printf("%d",C[i][0]);
- break;
- }
- printf("%dX^%d",C[i][0],C[i][1]);
- if(i<n3-1)
- printf(" + ");
- }
- }
- int main()
- {
- printf("\nEnter the number of terms in Plynomial A: ");
- scanf("%d",&n1);
- for(int i=0;i<n1;i++)
- {
- printf("Enter the Co-efficient and Power of term %d: ",i+1);
- scanf("%d%d",&A[i][0],&A[i][1]);
- }
- printf("\nEnter the number of terms in Plynomial B: ");
- scanf("%d",&n2);
- for(int i=0;i<n2;i++)
- {
- printf("Enter the Co-efficient and Power of term %d: ",i+1);
- scanf("%d%d",&B[i][0],&B[i][1]);
- }
- sort_polynomial('A');
- printf("\n\nEntered polynomial A is: ");
- for(int i=0;i<n1;i++)
- {
- if(A[i][1]==0)//if the degree is 0, then this statement makes sure that x is not displayed and only the number(coeff) is displayed
- {
- printf("%d",A[i][0]);
- break;
- }
- printf("%dX^%d",A[i][0],A[i][1]);
- if(i<n1-1)
- printf(" + ");//this is to make sure that + is not displayed at the end of the polynomial
- }
- sort_polynomial('B');
- printf("\n\nEntered polynomial B is: ");
- for(int i=0;i<n2;i++)
- {
- if(B[i][1]==0)
- {
- printf("%d",B[i][0]);
- break;
- }
- printf("%dX^%d",B[i][0],B[i][1]);
- if(i<n2-1)
- printf(" + ");
- }
- padd();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement