Friday, March 4, 2011

User-Defined Functions

Write a function that can be called to compute the product of two matrices of size m by n and n by m. The main function provides the values for m and n and two matrices.

#include <stdio.h>
void product(int A[20][20],int B[20][20]);
int m,n;
void main()
{
int i,j,Arr[20][20],Brr[20][20];
clrscr();
printf("Enter the value of row(m) and column(n):");
scanf("%d %d",&m,&n);
printf("Enter the elements of first %d x %d matrix\n",m,n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&Arr[i][j]);
printf("Enter the elemts of second %d x %d matrix\n",n,m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&Brr[i][j]);
product(Arr,Brr);
getch();
}
void product(int A[20][20],int B[20][20])
{
int i,j,k,C[40][40]={0};
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
for(k=1;k<=n;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
printf("Product of 1st and 2nd Matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
printf("%4d",C[i][j]);
putchar('\n');
}
}

No comments:

Post a Comment