Rishav_hitk_cse

Untitled

Jul 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX 20
  3.  
  4. void input(int [][MAX],int,int);
  5. void convert_to_triplet(int [][MAX],int,int,int [][3]);
  6. void display(int [][3]);
  7. void transpose(int [][3],int [][3]);
  8. void sort(int [][3]);
  9. void multiply_sparse(int [][3],int [][3],int [][3]);
  10.  
  11.  
  12. int main()
  13. {
  14.     int r1,c1,r2,c2;
  15.    
  16.     int matrix1[MAX][MAX];
  17.     int matrix2[MAX][MAX];
  18.     int sparse1[MAX*MAX+1][3];
  19.     int sparse2[MAX*MAX+1][3];
  20.     int result[MAX*MAX+1][3];
  21.    
  22.    
  23.     printf("Enter no. of rows and column in first matrix:");
  24.     scanf("%d %d",&r1,&c1);
  25.     do{
  26.                 printf("Enter no. of rows and column in second matrix:");
  27.                 scanf("%d %d",&r2,&c2);
  28.                
  29.         }while(r2!=c1);
  30.        
  31.    
  32.    
  33.     printf("Enter first matrix:\n");
  34.     input(matrix1,r1,c1);
  35.    
  36.     printf("Enter second matrix:\n");
  37.     input(matrix2,r2,c2);
  38.    
  39.     convert_to_triplet(matrix1,r1,c1,sparse1);
  40.     convert_to_triplet(matrix2,r2,c2,sparse2);
  41.    
  42.     printf("Sparse representation of first matrix:\n");
  43.     display(sparse1);
  44.    
  45.     printf("Sparse representation of second matrix:\n\n\n");
  46.     display(sparse2);
  47.      
  48.     multiply_sparse(sparse1,sparse2,result);
  49.     printf("Resultant matrix is:\n");
  50.     display(result);
  51.    
  52.  
  53.     return 0;
  54. }
  55.  
  56. void multiply_sparse(int a[][3],int b[][3],int cf[][3]){
  57.     int i,j,k=1,l=0,n=0;
  58.     int bt[MAX*MAX+1][3];
  59.     int c[MAX*MAX+1][3];
  60.    
  61.     c[0][0]=a[0][0];
  62.     c[0][1]=b[0][1];
  63.    
  64.      transpose(b,bt);
  65.      for(i=1;i<=a[0][2];i++){
  66.          for(j=1;j<=bt[0][2];j++){
  67.              if(a[i][1]==bt[j][1]){
  68.                  c[k][0]=a[i][0];
  69.                  c[k][1]=bt[j][0];
  70.                  c[k][2]=a[i][2]*bt[j][2];
  71.                  k++;
  72.              }
  73.          }
  74.      }
  75.   c[0][2]=k-1;
  76.    for(i=1;i<=k-1;i++){
  77.        for(j=i+1;j<=k-1;j++){
  78.            if(c[i][0]==c[j][0]&&c[i][1]==c[j][1]){
  79.                c[i][2]+=c[j][2];
  80.                c[j][0]=-1;
  81.             }
  82.        }
  83.    }
  84.    for(i=0;i<=k-1;i++){
  85.        if(c[i][0]!=-1){
  86.            cf[l][0]=c[i][0];
  87.            cf[l][1]=c[i][1];
  88.            cf[l][2]=c[i][2];
  89.            l++;
  90.        }
  91.    }
  92.    cf[0][2]=l-1;
  93. }
  94.  
  95. void input(int a[MAX][MAX],int r,int c){
  96.     int i,j;
  97.     for(i=0;i<r;i++){
  98.         for(j=0;j<c;j++){
  99.             printf("Enter matrix[%d][%d]:",i,j);
  100.             scanf("%d",&a[i][j]);
  101.         }
  102.     }
  103. }
  104.  
  105. void convert_to_triplet(int a[][MAX],int r,int c,int  sparse[][3]){
  106.     int i,j,k=1;
  107.    
  108.     sparse[0][0]=r;
  109.     sparse[0][1]=c;
  110.    
  111.     for(i=0;i<r;i++){
  112.         for(j=0;j<c;j++){
  113.             if(a[i][j]){
  114.                 sparse[k][0]=i;
  115.                 sparse[k][1]=j;
  116.                 sparse[k][2]=a[i][j];
  117.                 k++;
  118.             }
  119.         }
  120.     }
  121.    
  122.     sparse[0][2]=k-1;
  123. }
  124.  
  125. void display(int sparse[][3]){
  126.     int i;
  127.     for(i=0;i<=sparse[0][2];i++){
  128.         printf("%d\t%d\t%d\n",sparse[i][0],sparse[i][1],sparse[i][2]);
  129.     }
  130. }
  131.  
  132. void transpose(int b[][3],int bt[][3]){
  133.     int i;
  134.     for(i=0;i<=b[0][2];i++){
  135.         bt[i][0]=b[i][1];
  136.         bt[i][1]=b[i][0];
  137.         bt[i][2]=b[i][2];
  138.     }
  139.     sort(bt);
  140. }
  141.  
  142. void sort(int a[][3]){
  143.     int i,j,n,temp;
  144.     n=a[0][2];
  145.     for(i=1;i<n;i++){
  146.         for(j=1;j<n-i;j++){
  147.             if(a[j][0]>a[j+1][0]||(a[j][0]==a[j+1][0]&&a[j][1]>a[j+1][1])){
  148.                
  149.                 temp=a[j][0];
  150.                 a[j][0]=a[j+1][0];
  151.                 a[j+1][0]=temp;
  152.                
  153.                 temp=a[j][1];
  154.                 a[j][1]=a[j+1][1];
  155.                 a[j+1][1]=temp;
  156.                
  157.                 temp=a[j][2];
  158.                 a[j][2]=a[j+1][2];
  159.                 a[j+1][2]=temp;
  160.             }
  161.         }
  162.     }
  163. }
Add Comment
Please, Sign In to add comment