Tech

Matrix Multiplication with and without Functions

I’m starting off the coding Category. I’ll try to put the coding as simple and so that it is understandable.

I’m Beginning with Matrix multiplication because this was the code which we were trying to crack during our school days ūüôā (thinking it to be so difficult). Now it seems to be so simple.

Matrix Multiplication

/* PROGRAM TO READ TWO MATRICES MULTIPLY THEM AND PRINT ALL THE    THREE MATRICES  */
 
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,o,p;
printf("Enter the size of A  metrix\n");
scanf("%d%d",&m,&n);
 
printf("Enter the size of B  metrix\n");
scanf("%d%d",&o,&p);
if (n==o)
{
printf("Enter the elements of A metrix\n");
for(i=0;i<m;i++)
     for(j=0;j<n;j++)
  scanf("%d",&a[i][j]);
printf("Enter the elements of B metrix\n");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
  scanf("%d",&b[i][j]);
 
for(i=0;i<m;i++)
     for(j=0;j<p;j++)
     {
  c[i][j] =0;
  for (k=0;k<n;k++)
     c[i][j] = c[i][j]+a[i][k]*b[k][j];
     }
 
clrscr();
printf("\nA matrix is :\n\n");
for(i=0;i<m;i++)
{
     for(j=0;j<n;j++)
printf("%4d", a[i][j]);
     printf("\n");
}
printf("\nB matrix is :\n\n");
 
for(i=0;i<o;i++)
{
     for(j=0;j<p;j++)
printf("%4d", b[i][j]);
     printf("\n");
}
 
printf("\nC matrix is :\n\n");
for(i=0;i<m;i++)
{
     for(j=0;j<p;j++)
printf("%4d", c[i][j]);
     printf("\n");
}
 
    }
    else
printf("Multiplication not possible \n Please Re-enter Correct Size\n");
getch();
}
Matrix Multiplication using Functions
/* Matrix Multiplication using function */
#include<stdio.h >
#include<conio.h>
int i,j,k;
void main()
{
  int a[10][10],b[10][10],c[10][10],m,n,p,q;
  void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int p,int q);
  void read(int x[10][10],int m,int n);
  void display(int x[10][10], int m,int n);
  clrscr();
  printf("Enter the size of A Mtrix (Row and Col): \n");
  scanf("%d%d",&m,&n);
  printf("Enter the size of B Mtrix (Row and Col): \n");
  scanf("%d%d",&p,&q);
  if(n!=p)
  {
      printf("Multiplication Not Possible\n Please re-enter\n");
      printf("correct size and try again .....\n");
  }
  else
  {
     read(a,m,n);
     read(b,m,n);
     mul(a,b,c,m,n,p,q);
     printf("A Matrix is :\n");
     display(a,m,n);
     printf("B Matrix is :\n");
 
     display(b,m,n);
     printf("C Matrix is :\n");
     display(c,m,n);
  }
  getch();
}
  void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int p,int q)
  {
    for (i=0;i<m;i++)
       for(j=0;j<q;j++)
       {
 z[i][j]=0;
 for(k=0;k<n;k++)
     z[i][j]+= x[i][k]*y[k][j];
       }
 
  }
 
  void read(int x[10][10], int m,int n)
  {
    printf("Enter Matrix Value Row by Row\n");
    for (i=0;i<m;i++)
       for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
 
  }
 
  void display(int x[10][10], int m,int n)
  {
     for (i=0;i<m;i++)
      {
for(j=0;j<n;j++)
  printf("%5d",x[i][j]);
printf("\n");
      }
     printf("\n");
 
  }
Advertisements

About Sri

An Electronics Engineer by profession. But as a hobbyist I'm drawn towards various topics which is what this blog is all about. :)

Discussion

One thought on “Matrix Multiplication with and without Functions

  1. Hi,
    I want to create a function that receives two 2d integer array and returns a 2d integer array.
    how to do that?

    Posted by Arpan Das | April 26, 2015, 4:26 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 527 other followers

Blog Stats

  • 352,040 views

Blog categories..



On My twitter now !

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

This is me :)

%d bloggers like this: