You are on page 1of 8

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION :
( 6) (b) C Program to input two matrices , check the compatibility and perform addition and
multiplication.
ALGORITHM:
STEP-1: Start.
STEP-2: Declare m,n,p,q,c,d,f[10][10],s[10][10],sum[10][10].
STEP-3: Read p,q,f[c][d].
STEP-4: Check the conditions .
4.1:Check the compatibility through the condition(m==p&&n==q) and print the
statements .
4.2: Enter the matrices through the loops and read the values.
4.3: Compute:
sum[c][d]

f[c][d]+s[c][d]

STEP-5: Display the added matrices.


STEP-6: Stop.

FLOWCHART:

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

START

DECLARE m,n,p,q,f[10][10],s[10][10],cum[10][10]

READ m,n,p,q

FALSE

(m==p&
&n==q)
TRUE
CAN ADD MATRIX

CANNOT ADD MATRIX

FALSE
for c in steps of 1 do where c<m
TRUE

STOP
FALSE

for d in steps of 1 do where d<n


TRUE
READ f[c][d]

FALSE

for c in steps of 1 do where c<m


TRUE

for d in steps of 1 do where d<n


FALSE
READ s[c][d]

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

FALSE
for c in steps of 1 do where c<m
TRUE

FALSE
for d in steps of 1 do where d<n
TRUE

sum[c][d]=f[c][d]+s[c][d]

FALSE
for c in steps of 1 do where c <m
TRUE

for d in steps of 1 do where d<n


FALSE
TRUE
SUM OF MATRICES IS:

STOP

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program for Addition of two Matrices and to check their compatibility */

Program name:
/* Done By : C-Faculty

// wk6b.c
Dated: 15/10/2013*/

#include<stdio.h>
#include<curses.h>
int main()
{
int m,n,p,q,c,d,f[10][10],s[10][10];
clear();
printf(enter rows and columns for both the matrices );
scanf(%d%d%d%d,&m,&n,&p,&q);
if(m==p&&n==q)
{
printf(can add matrices);
}
else
{
printf(cannot add matrices);
}
printf(enter elements into first matrix);
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

{
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++)
{
sum[c][d]=f[c][d]+s[c][d];
}
}
printf(sum of matrices is:);
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
printf(%d,sum[c][d]);
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

}
}
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 wk6b.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk6b.c -curses o matrixadd

EXPECTED I/P AND O/P:


Output (1)
enter rows and columns for both the matrices
2222
can add matrices
enter elements into first matrix
12
34
enter the elements into the second matrix
12
34
sum of matrices is:
24
68
Output (2)
enter rows and columns for both the matrices
2332
cannot add matrices

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

Original Output :
Output (1)
enter rows and columns for both the matrices
2222
can add matrices
enter elements into first matrix
12
34
enter the elements into the second matrix
12
34
sum of matrices is:
24
68
Output (2)
enter rows and columns for both the matrices
2222
can add matrices
enter elements into first matrix
72
51
enter the elements into the second matrix
12
34
sum of matrices is:
84
85
VIVA VOCE QUESATIONS:
1) What is condition that is required to perform matrix addition?
Ans: To perform matrix addition, the two matrices must have the equal size i.e. equal number of
rows and columns.
2) What is condition that is required to perform matrix multiplication?
Ans: To perform matrix multiplication we have to check for compatibility condition i.e. the
number of columns in the first matrix must be equal with the number of rows in the second
matrix. Otherwise matrix multiplication cant be performed.
3) What is a function?
A function is a self-contained block of code that performs a particular task.

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

4) What is a main ()?


Main is a specially recognized function in C. Every program must have a main function to
indicate where the program has to begin its execution.
--xXx--

Department of Computer Science & Engg