Sie sind auf Seite 1von 7

5

ASSIGNMENT 2
MATRIX MULTIPLICATION BY USING TWO DIMENSIONAL ARRAY

STATEMENT:

In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or


expressions, arranged in rows and columns. The individual items in a matrix are called its
elements or entries. An example of a matrix with 2 rows and 3 columns is

The rule for matrix multiplication is more complicated, and two matrices can be multiplied
only when the number of columns in the first equals the number of rows in the second. A
major application of matrices is to represent linear transformations, that is, generalizations
of linear functions such as f(x) = 4x. A major branch of numerical analysis is devoted to the
development of efficient algorithms for matrix computations, a subject that is centuries old
and is today an expanding area of research.

MATRIX MULTIPLICATION TECHNIQUE AND AN EXAMPLE:


6

ALGORITHM:
INPUT: Two matrices

OUTPUT: A matrix showing the product of two square matrices of which the number of
columns in the first equals the number of rows in the second.

PROCESS:

Step 1: Create the function “void main()”

Step 1.1: Take the number of rows and number of columns of the 1st matrix in the integer
type variables r1 and c1 respectively.

Step 1.2: Take the number of rows and number of columns of the 2nd matrix in the integer
type variables r2 and c2 respectively.

Step 1.3: If(c1≠r2) then Print "The matrix multiplication is not possible and return.

[End of ‘If’]

Step 1.4: Take the elements of the 1st matrix in an array ‘a’ 2nd matrix in an array ‘b’.

Sep 1.6: Print the 1st matrix ‘a’ and the 2nd matrix ‘b’.

Step 1.7: For i=0 to i<r1 repeat Step 1.8 to Step 1.11

Step 1.8: For j=0 to j<c2 repeat Step 1.9 to Step 1.11

Step 1.9: Set c[i][j] 0

Step 1.10: For k=0 to k<c1 repeat Step 1.11

Step 1.11: Set c[i][j]  c[i][j] + a[i][k] × b[k][j]

[End of Step 1.10 ‘For’ loop]

[End of Step 1.8 ‘For’ loop]

[End of Step 1.7‘For’ loop]

Step 1.12: Print the 3rd matrix by a two dimensional array ‘c’ which represents the product
of the 1st and 2nd matrix.

[End of the function “void main()”]


7

Step 2: Stop.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

//* Multiplication of two matrices*//

void main()

int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,c2,r2;

//* Taking the no. of rows and columns of the 1st matrix*//

printf("enter no. of rows and column of 1st matrix: ");

scanf("%d%d",&r1,&c1);

//* Taking the no. of rows and columns of the 2nd matrix*//

printf("enter no. of rows and column of 2nd matrix: ");

scanf("%d%d",&r2,&c2);

//* Checking the no. of columns of the 1st matrix and no. of rows of the 2nd matrix are equal or
not*//

if(c1!=r2)

printf("The matrix multiplication is not possible");

return;

//* Taking the elements of the 1st matrix*//

printf("\n Enter first matrix......\n");

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

{
8

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

printf("a[%d][%d]=",i,j);

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

//* Taking the elements of the 2nd matrix*//

printf("\nEnter second matrix.......\n");

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

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

printf("b[%d][%d]=",i,j);

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

//* Printing the 1st matrix*//

printf("\n\nFirst matrix is......\n\n");

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

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

printf("%d ",a[i][j]);

printf("\n");

}
9

//* Printing the 2nd matrix*//

printf("\n\nSecond matrix is.......\n\n");

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

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

printf("%d ",b[i][j]);

printf("\n");

//* Matrix multiplication*//

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

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

c[i][j]=0;

for(k=0;k<c1;k++)

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

//* Printing the 3rd matrix after multiplication*//

printf("\n\n After matrix multiplication.....\n\n");

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

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

printf("%d ",c[i][j]);

printf("\n");

}
10

getch( );

OUTPUT:
11

DISCUSSION:
The product of a square matrix multiplied by a column matrix arises naturally in linear algebra; for
solving linear equations and representing linear transformations . In general AB≠BA because AB
and BA may not be simultaneously defined, and even if they are they may still not be equal. This is
contrary to ordinary multiplication of numbers.

Das könnte Ihnen auch gefallen