Week 6 (C)
Week 6 (C)
SPECIFICATION:
( 6) (C). C Program for the multiplication of two matrices and checking their compatibility
ALGORITHM:
STEP-1: Start.
STEP-2: Declare m,n,p,q,c,d,k,sum=0,multiply[10][10],f[10][10],s[10][10].
STEP-3: Read m,n,p,q.
STEP-4:
4.1: Check the compatibility by using the condition(m==p&&n==q) and print the
statements.
4.2: Enter the elements into the first matrix using loop and read those values.
4.3: Enter the elements into the second matrix using another loop and read those values .
4.4: Compute:
sum
sum+f[c][k]*s[k][d]
multiply[c][d]
sum
sum
START
DECLARE m,n,p,q,c,d,k,sum,multiply[10][10],f[10][10],s[10][10]
READ m,n,p,q
(m==p&&n=
=q)
FALSE
CANNOT MULTIPLY
TRUE
CAN MULTIPLY
FALSE
FALSE
READ f[c][d]
FALSE
For c in steps of 1 do where c<m
TRUE
READ s[c][d]
FALSE
FALSE
FALSE
FALSE
Sum=sum+f[c][k]+s[k][d]
Multiply[c][d]=sum
Sum=0
FALSE
Product is:
STOP
FALSE
PROGRAM
/* C Program for Multiplication of Matrices and checking their compatibility */
Program name:
/* Done By : C-Faculty
// wk6c.c
Dated: 15/10/2013*/
#include<stdio.h>
#include<curses.h>
int main()
{
int m,n,p,q,c,d,k,sum=0,multiply[10][10],f[10][10],s[10][10];
clear();
printf(enter number of rows and columns for first matrix);
scanf(%d%d,&m,&n);
printf(enter number of rows and columns for second matrix);
scanf(%d%d,&p,&q);
if(m==p&&n==q)
printf(can multiply);
else
Printf(cannot multiply);
printf(enter the elements into the first matrix);
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
scanf(%d,&f[c][d]);
}
}
printf(enter the elements into the second matrix);
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
scanf(%d,&s[c][d]);
}
}
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
for(k=0;k<m;k++)
{
sum=sum+f[c][k]*s[k][d];
}
multiply[c][d]=sum;
sum=0;
}
}
printf(product is :);
for(c=0;c<m;c++)
{
Department of Computer Science & Engg
for(d=0;d<n;d++)
{
printf(%d\t\n,multiply[c][d]);
printf(\n);
}
}
return(0);
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk6c.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out
4253
ORIGINAL OUTPUT :
Output (1)
enter number of rows and columns for first matrix 3 3
enter number of rows and columns for second matrix 3 3
Can Multiply
enter the elements into the first matrix 1 2 3 4 5 6 7 8 9
enter the elements into the second matrix 9 8 7 6 5 4 3 2 1
product is
30
24
18
84
69
54
138 114
Output (2)
enter number of rows and columns for first matrix 2 2
enter number of rows and columns for second matrix 2 2
Can Multiply
enter the elements into the first matrix 1 2 3 4
enter the elements into the second matrix 5 6 7 8
product is
19
22
43
50
--xXx--
90