Advertisement
Sathvikks8

PolynomialAddition.c

Oct 1st, 2020 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4. int A[100][100], B[100][100], C[100][100],n1,n2,n3=0;
  5. int arr_len(int array[][100])
  6. {
  7.   int len=0;
  8.   for(int i=0;array[i][0]!='\0';i++)
  9.     len++;
  10.   return len;
  11. }
  12. void sort_polynomial(char array)//used to sort the polynomial, such that highest degree term always stays on the left
  13. {
  14.   int coeff,deg,i,j;
  15.   switch (array)
  16.   {
  17.     case 'A':
  18.     {
  19.       for(i=0;i<arr_len(A);i++)
  20.       {
  21.         for(j=0;j<arr_len(A)-1;j++)
  22.         {
  23.           if(A[j][1]<A[j+1][1])
  24.           {
  25.             coeff=A[j][0];
  26.             deg=A[j][1];
  27.             A[j][0]=A[j+1][0];
  28.             A[j][1]=A[j+1][1];
  29.             A[j+1][0]=coeff;
  30.             A[j+1][1]=deg;
  31.           }
  32.         }
  33.       }
  34.     }
  35.     break;
  36.     case 'B':
  37.     {
  38.       for(i=0;i<arr_len(B);i++)
  39.       {
  40.         for(j=0;j<arr_len(B)-1;j++)
  41.         {
  42.           if(B[j][1]<B[j+1][1])
  43.           {
  44.             coeff=B[j][0]; //swapping both coefficient and degree
  45.             deg=B[j][1];
  46.             B[j][0]=B[j+1][0];
  47.             B[j][1]=B[j+1][1];
  48.             B[j+1][0]=coeff;
  49.             B[j+1][1]=deg;
  50.           }
  51.         }
  52.       }
  53.     }
  54.     break;
  55.     default:
  56.     {
  57.       for(i=0;i<arr_len(C);i++)
  58.       {
  59.         for(j=0;j<arr_len(C);j++)
  60.         {
  61.           if(C[j][1]<C[j+1][1])
  62.           {
  63.             coeff=C[j][0];
  64.             deg=C[j][1];
  65.             C[j][0]=C[j+1][0];
  66.             C[j][1]=C[j+1][1];
  67.             C[j+1][0]=coeff;
  68.             C[j+1][1]=deg;
  69.           }
  70.         }
  71.       }
  72.     }
  73.   }
  74. }
  75. void padd()
  76. {
  77.   int i=0,k=0,found=0;
  78.   while(i<n1)
  79.   {
  80.     for(int j=0;j<n2;j++)
  81.     {
  82.       if(A[i][1]==B[j][1]) //checking if degree matches. if yes then add the coeff
  83.       {
  84.         found=1;
  85.         C[k][0]=A[i][0]+B[j][0];
  86.         C[k++][1]=A[i][1];
  87.       }
  88.     }
  89.     if(!found)//if the degree present in polynomial A has no match with B, then just include the term in c
  90.     {
  91.       C[k][0]=A[i][0];
  92.       C[k++][1]=A[i][1];
  93.     }
  94.     else
  95.       found=0;
  96.     i++;
  97.   }
  98.   i=0;
  99.   while(i<n2)//to add the terms from polynomial B to C, where degree of B is not present in A
  100.   {
  101.     for(int j=0;j<arr_len(C);j++)
  102.     {
  103.       if(C[j][1]==B[i][1])
  104.         found=1;
  105.     }
  106.     if(!found)//if any term whose degree is not found in B, then it is added to C directly
  107.     {
  108.       C[k][0]=B[i][0];
  109.       C[k++][1]=B[i][1];
  110.     }
  111.     else
  112.       found=0;
  113.     i++;
  114.   }
  115.   sort_polynomial('C');
  116.   printf("\n\nThe result after adding the the polynomial A and B is: ");
  117.   for(int i=0;C[i][0]!='\0';i++)
  118.     n3++;
  119.   for(int i=0;i<n3;i++)
  120.   {
  121.     if(C[i][1]==0)
  122.     {
  123.       printf("%d",C[i][0]);
  124.       break;
  125.     }
  126.     printf("%dX^%d",C[i][0],C[i][1]);
  127.     if(i<n3-1)
  128.       printf(" + ");
  129.   }
  130. }
  131. int main()
  132. {
  133.   printf("\nEnter the number of terms in Plynomial A: ");
  134.   scanf("%d",&n1);
  135.   for(int i=0;i<n1;i++)
  136.   {
  137.     printf("Enter the Co-efficient and Power of term %d: ",i+1);
  138.     scanf("%d%d",&A[i][0],&A[i][1]);
  139.   }
  140.   printf("\nEnter the number of terms in Plynomial B: ");
  141.   scanf("%d",&n2);
  142.   for(int i=0;i<n2;i++)
  143.   {
  144.     printf("Enter the Co-efficient and Power of term %d: ",i+1);
  145.     scanf("%d%d",&B[i][0],&B[i][1]);
  146.   }
  147.   sort_polynomial('A');
  148.   printf("\n\nEntered polynomial A is: ");
  149.   for(int i=0;i<n1;i++)
  150.   {
  151.     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
  152.     {
  153.       printf("%d",A[i][0]);
  154.       break;
  155.     }
  156.     printf("%dX^%d",A[i][0],A[i][1]);
  157.     if(i<n1-1)
  158.       printf(" + ");//this is to make sure that + is not displayed at the end of the polynomial
  159.   }
  160.   sort_polynomial('B');
  161.   printf("\n\nEntered polynomial B is: ");
  162.   for(int i=0;i<n2;i++)
  163.   {
  164.     if(B[i][1]==0)
  165.     {
  166.       printf("%d",B[i][0]);
  167.       break;
  168.     }
  169.     printf("%dX^%d",B[i][0],B[i][1]);
  170.     if(i<n2-1)
  171.       printf(" + ");
  172.   }
  173.   padd();
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement