Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object I. To write a program to multiply two given matrices.

Background Theory:

Let A and B are two given matrices of dimensions (m ´ n) and (p ´ q) respectively. Also let C be the product matrix. For the multiplication of given two matrices, number of columns of the first matrix must equal to the number of rows of the second ie n=p. The resultant matrix C will be an m´q order.

Let A= and B= The resultant product matrix of A2´3 and B3´3 is C2´3.

Let C= The row elements of the C are obtained as follows:

c11: a11´b11+a12´b21+a13´b31

c12: a11´b12+a12´b22+a13´b32

c13: a11´b13+a12´b23+a13´b33

This suggests that any element of the product matrix C can be found by:

The first subscript of a will be the first subscript of c and the second subscript of b will be the second subscript of c. The second subscript of a and the first subscript of b is same and varies from 0 to n-1. Let this common script be k. Since very element of c is a summation of certain product terms, one has to use a variable like sum in order to have the generated elements summed up and stored it.

All the elements of the products matrix C are generated by repeating the above process for all the values of i and j for i being row and j being column indications. The first subscript of C, i, should vary from 0 to m-1 and j, the second subscript vary from 0 to q-1 as shown in flow chart.

Nested for loops are used. Whenever nested for loops are used, the inside for is taken care of first. In other words, here, j varies faster than i . Since j varies faster than i, the elements of the product matrix are generated row-wise. If by chance the above two loops are interchanged, then the elements of C are generated column-wise.

Code:

//program to multiply two matrices//

#include<stdio.h>

#include<conio.h>

void main()

{

long int m, n, j, i, k, p, q, sum, a, b, c;

// three 2D arrays each of size 10*10 //

clrscr();

printf("\n\nEnter the order of the first matrix A\n");

repeat:

scanf("%ld%ld", &m, &n);

printf("\nEnter the order of the second matrix B\n");

scanf("%ld%ld", &p, &q);

printf("\nOrder of matrix A= %ld*%ld\nOrder of matrix B= %ld*%ld\n", m,n,p,q);

if(n!=p)

{

printf("\n\nMatrices are not multipliable\n");

goto repeat;  //the use of goto statement to repeat the program//

}

printf("\nEnter the elements of matrix A\n");

for(i=0;i<m;i++)

for(j=0;j<n;++j)

scanf("%ld", &a[i][j]);

printf("\nEnter elements of matrix B\n");

for(i=0;i<p;++i)

for(j=0;j<q;++j)

scanf("%ld", &b[i][j]);

printf("\nMatrix A\n");

for(i=0; i<m;++i)

{

for(j=0;j<n;++j)

printf("%ld\t", a[i][j]);

printf("\n");

}

printf("\nMatrix B\n");

for(i=0;i<p;++i)

{

for(j=0;j<q;++j)

printf("%ld\t", b[i][j]);

printf("\n");

}

//finding the product//

for(i=0;i<m;++i) //for each row //

{

for(j=0;j<q;++j) //for each column//

{

sum=0;

for(k=0;k<n;++k) //for each matrix element//

sum+=a[i][k]*b[k][j];

c[i][j]=sum;

}

}

printf("\nProduct matrix\n");

for(i=0;i<m;++i)

{

for(j=0;j<q;++j)

printf("%ld\t", c[i][j]);

printf("\n");

}

getch();

}

Search yourself for more such codes

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12