0% found this document useful (0 votes)
58 views

Week 6 (C)

The document describes a C program for multiplying two matrices and checking their compatibility. It includes the algorithm, flowchart and full program code. The program declares variables, reads the matrix sizes, checks if the matrices can be multiplied, inputs the matrix elements, calculates the product and outputs the result. It provides sample input and output showing the program checking matrix dimensions and multiplying compatible matrices.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Week 6 (C)

The document describes a C program for multiplying two matrices and checking their compatibility. It includes the algorithm, flowchart and full program code. The program declares variables, reads the matrix sizes, checks if the matrices can be multiplied, inputs the matrix elements, calculates the product and outputs the result. It provides sample input and output showing the program checking matrix dimensions and multiplying compatible matrices.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

RAGHU INSTITUTE OF TECHNOLOGY

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

STEP-5: Display the multiplied matrices .


STEP-6: Stop.
FLOWCHART:

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

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

For c in steps of 1 do where c<m


TRUE
For d in steps of 1 do where d<n
TRUE

FALSE

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


TRUE

READ s[c][d]

Department of Computer Science & Engg

FALSE

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

FALSE

For k in steps of 1 do where k<m


TRUE

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

Multiply[c][d]=sum
Sum=0

FALSE

For c in steps of 1 do where c<m


TRUE

For d insteps of 1 do where d<n


TRUE

Product is:

STOP

Department of Computer Science & Engg

FALSE

RAGHU INSTITUTE OF TECHNOLOGY

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]);

Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

}
}
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

RAGHU INSTITUTE OF TECHNOLOGY

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

Step 4: To create an executing file use the command


cc wk6c.c -curses o matrixmul

EXPECTED I/P AND O/P:


Output (1)
enter number of rows and columns for first matrix 3 3
enter number of rows and columns for second matrix 3 2
Cant Multiply
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 1 2 1
enter the elements into the second matrix 1 1 31
product is
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

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--

Department of Computer Science & Engg

90

You might also like