Saturday, August 20, 2011

Program to find the multiplication of two matrixs using functions.

//Program to find the multiplication of two matrixs using functions.
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void create(int ***t,int r,int c);
void display(int **t,int r,int c);
void mul(int ***t,int **a,int **b,int m,int n,int q);
main()
{
 int **a,**b,**c,m,n,p,q,i,j;
 printf("\n Enter the number of rows in 1st  matrix:");
 scanf("%d",&m);
 printf("\n Enter the number of columns in  1st matrix:");
 scanf("%d",&n);
 create(&a,m,n);
 printf("\n Enter the number of rows in 2nd  matrix:");
 scanf("%d",&p);
 printf("\n Enter the number of columns in  2nd matrix:");
 scanf("%d",&q);
 create(&b,p,q);

 if(n==p)
 {
  mul(&c,a,b,m,n,q);
  printf("\n1st matrix is \n");
  display(a,m,n);
  printf("\n2nd matrix is \n");
  display(b,p,q);
  printf("\nResult of multiplication \n");
  display(c,m,q);
 }
 else
 {
 printf("multiplication not possible");
 printf("\n 1st matrix is \n");
 display(a,m,n);
 printf("\n2nd matrix is\n");
 display(b,p,q);
 }

 getch();
}
void create(int ***te,int r,int c)
{
     int **t,i,j;
     t=(int**)malloc(r*sizeof(int*));
     for(i=0;i<r;i++)
     t[i]=(int*)malloc(c*sizeof(int));
     printf("Enter the elements of matrix");


     for(i=0;i<r;i++)
       for(j=0;j<c;j++)
         scanf("%d",&t[i][j]);
       
     *te=t;
}
void display(int **t,int r,int c)
{
     int i,j;
     for(i=0;i<r;i++)
     {
      for(j=0;j<c;j++)
        printf(" %d ",t[i][j]);
      printf("\n");
     }
   
}

void mul(int ***c,int **a,int **b,int m,int n,int q)
{
   
     int i,j,k;
     int **t;
     t=(int**)malloc(m*sizeof(int*));
     for(i=0;i<m;i++)
     t[i]=(int*)malloc(q*sizeof(int));

     for(i=0;i<m;i++)
     {
       for(j=0;j<q;j++)
        {
          t[i][j]=0;
          for(k=0;k<n;k++)
           {
             t[i][j]+=a[i][k]*b[k][j];
           }
        }
    }
 *c=t;
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More