100% C program for matrix multiplication

// matrix multiplication
#include <stdio.h>
#include<stdlib.h>
void main()
{
  int a[5][5],b[5][5],c[5][5];
  int i,j,k,m,n,p,q;
  /*m,n= no of rows & columns of  matrix a respectively
    p,q = no of rows & columns of  matrix b respectively*/
    printf("\n enter the no of rows and columns of matrix a:");
    scanf("%d%d",&m,&n);
    printf("\n enter the no of rows and columns of matrix b:");
    scanf("%d%d",&p,&q);
    /*lets check if the dimensions of matrices are compatible or not for multiplication.  columns of 1st matrix must equal rows of 2nd matrix for multiplication to exist*/
   if(n!=p)
   {
       printf("\n multiplication not possible");
       exit(0);
   }
   else
   {
       /*entering values in matrix a begins*/
       for(i=0;i<m;i++)
       {
           for(j=0;j<n;j++)
           {
               printf("\n enter a[%d][%d]:",i,j);
               scanf("%d",&a[i][j]);
           }
       } //entering data of matrix a is over
       
       /*entering values in matrix a begins*/
       for(i=0;i<p;i++)
       {
           for(j=0;j<q;j++)
           {
               printf("\n enter b[%d][%d]:",i,j);
               scanf("%d",&b[i][j]);
           }
       } //entering data of matrix b is over
       /*calculation of elements of c begins*/
       
       for(i=0;i<m;i++)
       {
           for(j=0;j<q;j++)
           {
               c[i][j]=0;
               for(k=0;k<n;k++)
               {
                   c[i][j]=c[i][j]+a[i][k]*b[k][j];
               }    
           }
       } //computation of elements in matrix C is over
       printf("\n\n");
      // printing output begins
       for(i=0;i<m;i++)
       {
        for(j=0;j<q;j++)
        {
            printf("%5d",c[i][j]);
        }
        printf("\n");
       }
   }//end of else
}// end of main
   
ఔట్పుట్ తెరమీద
   enter the no of rows and columns of matrix a:2
2
enter the no of rows and columns of matrix b:2
2
enter a[0][0]:1
enter a[0][1]:0
enter a[1][0]:0
enter a[1][1]:1
enter b[0][0]:1
enter b[0][1]:2
enter b[1][0]:3
enter b[1][1]:-1
    1    2
    3   -1 
గమనిక  the 1st row of the matrix may get printed with some shift on the screen..plz take care of it by adjusting the program accordingly

కామెంట్‌లు

ఈ బ్లాగ్ నుండి ప్రసిద్ధ పోస్ట్‌లు

100% C prog to write content into a file and then read it back onto the console

Spic macay

శ్రీరస్తు.. రామలింగ 18-1-24 (సూరసాని వారి ఆహ్వానము)