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.

Download this full program | Lab 7 | in pdf format

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 mq order.  

Let A=  and B=

 The resultant product matrix of A23 and B33 is C23.

Let C=

The row elements of the C are obtained as follows:

c11: a11b11+a12b21+a13b31

c12: a11b12+a12b22+a13b32

c13: a11b13+a12b23+a13b33

 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[10][10], b[10][10], c[10][10];

            // 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();

}

To Download this full program | Lab 7 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

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

Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com