Beruflich Dokumente
Kultur Dokumente
ASSIGNMENT 2
MATRIX MULTIPLICATION BY USING TWO DIMENSIONAL ARRAY
STATEMENT:
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.
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.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.12: Print the 3rd matrix by a two dimensional array ‘c’ which represents the product
of the 1st and 2nd matrix.
Step 2: Stop.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
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*//
scanf("%d%d",&r1,&c1);
//* Taking the no. of rows and columns of the 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)
return;
for(i=0;i<r1;i++)
{
8
for(j=0;j<c1;j++)
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
for(i=0;i<c1;i++)
for(j=0;j<c2;j++)
printf("b[%d][%d]=",i,j);
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
printf("%d ",a[i][j]);
printf("\n");
}
9
for(i=0;i<c1;i++)
for(j=0;j<c2;j++)
printf("%d ",b[i][j]);
printf("\n");
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
c[i][j]=0;
for(k=0;k<c1;k++)
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.