Advertisement
Mr_kindle

matrixmultiplication.c

Nov 19th, 2022 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. /*tHIS CODE MULTIPLY TWO MATRICES which are dymamiclly allocated AND PRINT THE OUTPUT*/
  2. /*All matrices are dynamically allocated*/
  3. /*Let there are 2 matrices mat1 of n1 * m1 & mat2 of n2 * m2. then for matrix to be multiplied m1 should be
  4. equal to n2 (m1 == n2). After multiplication the product matrix will be of n1 * m2 type.*/
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. #include<malloc.h>
  9.  
  10. void display(int *mat,int row, int column);
  11. int * multiply_matrix(int *mat1,int row1,int col1,int *mat2,int row2, int col2);
  12.  
  13.  
  14. //taking all these as global variable because each function will use it
  15.  
  16. int main()
  17. {   int *mat1, *mat2, *mat3;
  18.     int n1,n2,m1,m2,i;
  19.     srand(time(0));
  20.     printf("Enter value of 'n' & 'm' for the first matrix (n * m):  ");
  21.     scanf("%d %d",&n1,&m1);
  22.     printf("Enter value of 'n' & 'm' for the second matrix (n * m):  ");
  23.     scanf("%d %d",&n2,&m2);
  24.    
  25.     /*Dyanamically allocating memeory for both matrices*/
  26.     mat1 = calloc(n1*m1, sizeof(int));
  27.     mat2 = calloc(n2*m2,sizeof(int));
  28.     /*Dynamically allocating memory for product matrix*/
  29.    
  30.     /*randomly generating first matrix*/
  31.     for(i=0;i<n1*m1;i++)
  32.         mat1[i] = rand()%9 + 1;//generating number between 1 to 9
  33.     /*Randomly generating the second matrix*/
  34.     for(i=0;i<n2*m2;i++)
  35.         mat2[i] = rand()%9 + 1;
  36.    
  37.     /*multliplying both matrices*/
  38.     mat3 = multiply_matrix(mat1,n1,m1,mat2,n2,m2);
  39.      /*Displaying both the matrices*/
  40.     display(mat1,n1,m1);
  41.     display(mat2,n2,m2);
  42.     /*displaying the product matrix*/
  43.     display(mat3,n1,m2);
  44.    
  45. free(mat1);
  46. free(mat2);
  47. free(mat3);
  48.    
  49.            
  50.            
  51.  
  52.  
  53. return 0;
  54. }//main
  55.  
  56. void display(int *mat,int row, int column)
  57.     {   int i,j;
  58.            
  59.         for(i=0;i<row;i++)
  60.             {
  61.                 for(j=0;j<column;j++)  
  62.                     printf("%-4d ",mat[i*column + j]);
  63.                 printf("\n");
  64.            
  65.             }
  66.         printf("\n\n");
  67.        
  68.     }//display
  69.    
  70. int* multiply_matrix(int *mat1,int row1,int col1,int *mat2,int row2, int col2)
  71. {   int i,j,k;
  72.     int *prod;
  73.     /*dynamically allocating the product matrix*/
  74.     prod = calloc(row1 * col2, sizeof(int));
  75.     /*Checking multiplication constrants*/
  76.     if(col1 != row2)
  77.        {
  78.            printf("\nThese matrix can not be multiplied: ");
  79.            exit(1);
  80.        }
  81.     for(i=0;i<row1;i++)
  82.         {   for(j=0;j<col2;j++)
  83.                 {  
  84.                     for(k=0;k<row2;k++)
  85.                         {   prod[i * col2 + j] = prod[i * col2 + j] + mat1[i * col1 + k] * mat2[k * col2 + j];/*quite tricky pay attention to it*/
  86.                               //printf("%d ",prod[i * col2 + j]);
  87.                         }
  88.                 }
  89.         }
  90. return prod;
  91. }//end of multiply matrix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement