Advertisement
Rishav_hitk_cse

day 2 lab assignment(sort not working)

Jul 16th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct term{
  5.     int exp;
  6.     float coef;
  7. };
  8.  
  9.  
  10. void display(struct term* poly,int,int);
  11. void input(struct term*,int,int);
  12. void sort(struct term*,int,int);
  13. void add(struct term*,int,int);
  14.  
  15. int main(){
  16.     int n1,n2;
  17.    
  18.     printf("Enter the number of term in 1st polynomial:");
  19.     scanf("%d",&n1);
  20.     printf("Enter the number of term in 2nd polynomial:");
  21.     scanf("%d",&n2);
  22.    
  23.     struct term*poly=(struct term *)malloc((n1+n2)*sizeof(struct term));
  24.    
  25.     printf("Enter 1st polynomial:\n");
  26.     input(poly,0,n1-1);
  27.     sort(poly,0,n1-1);
  28.    
  29.     printf("Enter 2nd polynomial:\n");
  30.     input(poly,n1,n1+n2-1);
  31.     sort(poly,n1,n1+n2-1);
  32.    
  33.     printf("Polynomial 1 is:\n");
  34.     display(poly,0,n1-1);
  35.    
  36.     printf("Polynomial 2 is:\n");
  37.     display(poly,n1,n1+n2-1);
  38.    
  39.     add(poly,n1,n2);
  40.     sort(poly,0,n1+n2-1);
  41.    
  42.     printf("Resultant polynomial is:\n");
  43.     display(poly,0,n1+n2-1);
  44.    
  45.     return 0;
  46. }
  47.  
  48. void input(struct term* p,int l,int r){
  49.     int i;
  50.     for(i=l;i<=r;i++){
  51.         printf("TERM %d:\n",i-l+1);
  52.         printf("Enter exponent:");
  53.         scanf("%d",&p[i].exp);
  54.         printf("Enter coefficient:");
  55.         scanf("%f",&p[i].coef);
  56.        
  57.     }
  58. }
  59.  
  60. void display(struct term* poly,int l,int r){
  61.     int i;
  62.     for(i=l;i<=r;i++){
  63.         if(poly[i].coef){
  64.             printf("%.2fx^%d",poly[i].coef,poly[i].exp);
  65.         if(i!=r)
  66.             printf("+");
  67.         }
  68.            
  69.     }
  70.     printf("\n");
  71.    
  72. }
  73.  
  74. void sort(struct term* poly,int l,int r){
  75.     int i,j;
  76.     struct term temp;
  77.    // int  n=r-l+1;
  78.     for(i=l;i<r-1;i++){
  79.         for(j=l;j<r-i+1;j++){
  80.             if(poly[j].exp<poly[j+1].exp){
  81.                 temp=poly[j];
  82.                 poly[j]=poly[j+i];
  83.                 poly[j+i]=temp;
  84.             }
  85.            else  if(poly[j].exp==poly[j+1].exp){
  86.                poly[j].coef+=poly[j+1].coef;
  87.                poly[j+1].coef=0;
  88.            }
  89.         }
  90.     }
  91. }
  92.  
  93. void add(struct term* poly,int m,int n){
  94.     int i,j;
  95.     for(i=0;i<m;i++){
  96.         for(j=m;j<m+n;j++){
  97.             if(poly[i].exp==poly[j].exp){
  98.                poly[i].coef+=poly[j].coef;
  99.                poly[j].coef=0;
  100.             }
  101.         }
  102.     }
  103.    
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement