(C++) CPP Programs - 26

Friday, December 14, 2007

26. Develop functions
a) To read a given matrix
b) To output a matrix
c) To compute the product of two matrices.
Use the above-developed functions to read in two matrices A(M*N) and (P*Q), to compute the product of the input matrices, to output the given matrices and the computed product matrix in a function.

/* Product of a matrix*/

#include
#include

int a[5][5],b[5][5],c[5][5],m,n,p,q;

void main()
{
clrscr();

read_mat();

printf("\nMatrix a is\n");
output(a,m,n);

printf("\nMatrix b is\n");
output(b,p,q);

mat_mul();

printf("\nMatrix c is\n");
output(c,m,q);

getch();
}

read_mat()
{
int i,j;
printf("\nEnter order of a\n");
scanf("%d%d",&m,&n);

printf("\nEnter the matrix b\n");
scanf("%d%d",&p,&q);

if(p!=n)
{
printf("\nNot multiplicable\n");
getch();
exit(0);
}

printf("\nMultiplicable\n");
printf("\nEnter the matrix a\n");
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf("%d",&a[i][j]);

printf("\nEnter matrix b\n");
for(i=0;i < p;i++)
for(j=0;j < q;j++)
scanf("%d",&b[i][j]);
}

output(int x[5][5],int ro,int col)
{
int i,j;
for(i=0;i < ro;i++)
{
for(j=0;j < col;j++)
printf("%4d",x[i][j]);
printf("\n");
}
}

mat_mul()
{
int i,j,k;
for(i=0;i < m;i++)
for(j=0;j < q;j++)
{
c[i][j]=0;
for(k=0;k < p;k++)
c[i][j]+=a[i][k]*b[k][i];
}
}

0 comments:

Post a Comment

Chitika

About This Blog

Followers

Blog Archive

  © Blogger template The Professional Template II by Ourblogtemplates.com 2009

Back to TOP