Rishav_hitk_cse

sparse matrix addition

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