LAB ASSIGNMENT 1
Name: Manas Patil
PRN: 12211772
Class: TY IT C
Batch: Batch 2
Roll no: 10
Aim: Analysis of Matrix Multiplication.
Step 1: Pseudocode for Matrix Multiplication
procedure MatrixMultiplication(A, B)
input A, B n*n matrix
output C, n*n matrix
begin
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
C[i, j] = 0;
end for
end for
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
C[i, j] = C[i, j] + A[i, k] * B[k, j];
end for
end for
end for
end MatrixMultiplication
Step 2: Code for Matrix Multiplication
#include <stdio.h>
void inputMatrix(int matrix[][10], int rows, int cols) {
printf("Enter elements of the %dx%d matrix:\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
int matrixMultiply(int A[][10], int r1, int c1, int B[][10], int r2, int c2, int C[][10]) {
if (c1 != r2) {
printf("Matrix multiplication not possible. Columns of A (%d) must equal rows of B
(%d).\n", c1, r2);
return 0;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
C[i][j] = 0;
for (int k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
return 1;
void printMatrix(int matrix[][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
int main() {
int r1, c1, r2, c2;
printf("Enter the number of rows and columns for the first matrix (A): ");
scanf("%d %d", &r1, &c1);
printf("Enter the number of rows and columns for the second matrix (B): ");
scanf("%d %d", &r2, &c2);
if (c1 != r2) {
printf("Matrix multiplication not possible. Columns of A (%d) must equal rows of B
(%d).\n", c1, r2);
return 1;
int A[10][10], B[10][10], C[10][10];
printf("Input for Matrix A:\n");
inputMatrix(A, r1, c1);
printf("Input for Matrix B:\n");
inputMatrix(B, r2, c2);
matrixMultiply(A, r1, c1, B, r2, c2, C);
printf("Resultant Matrix (A x B):\n");
printMatrix(C, r1, c2);
return 0;
}
Step 3: Equations for number of arithmetic operations needed in matrix
multiplication. (Considering addition and Multiplication only).
Addition: n3 −n2
For each element in the result matrix, after performing n multiplications, n−1
additions are needed to sum the products. Since there are n^2 elements in the
result matrix, the total number of additions is n2×(n−1)=n3−n^2
Multiplication: n3
In matrix multiplication, each element in the result matrix is obtained by
multiplying n elements from a row of the first matrix and a column of the
second matrix. For an n×n matrix, there are n^2 elements in the result matrix,
and each element requires n multiplications. Thus, the total number of
multiplications is n^3.
Total: 2 n3−n2
The total arithmetic operations required combine both multiplication (n^3)
and addition (n^3 - n^2), resulting in:
Total Operations=n3+(n3−n2)=2n3−n2
Step 4: Prepare a table for at least for 10 values of n.
N A (n3 −n2) M (n3 ) T (2 n3−n2)
1 0 1 1
2 4 8 8
3 18 27 48
4 48 64 112
5 100 125 200
6 180 216 324
7 294 343 490
8 448 512 704
9 648 729 972
10 900 1000 1300
Step 5: Comparing n versus addition, Multiplication and Total operation in
Matrix multiplication.
Output: