0% found this document useful (0 votes)
24 views5 pages

Daa Lab 1

The document outlines a lab assignment focused on matrix multiplication, including pseudocode, C code implementation, and analysis of arithmetic operations involved. It details the number of additions and multiplications required for square matrices, providing a table with calculations for values of n from 1 to 10. The total operations are summarized as 2n^3 - n^2, combining both types of operations.

Uploaded by

manasdp271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Daa Lab 1

The document outlines a lab assignment focused on matrix multiplication, including pseudocode, C code implementation, and analysis of arithmetic operations involved. It details the number of additions and multiplications required for square matrices, providing a table with calculations for values of n from 1 to 10. The total operations are summarized as 2n^3 - n^2, combining both types of operations.

Uploaded by

manasdp271
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB ASSIGNMENT 1

Name: Manas Patil


PRN: 12211772
Class: TY IT C
Batch: 2

Aim: Analysis of Matrix Multiplication.

Step 1: Pseudocode for Matrix Multiplication

Input: Two matrices A (n x m) and B (m x p)


Output: Resultant matrix C (n x p)

1. Initialize C as an n x p matrix with all elements set to 0


2. For i = 1 to n:
For j = 1 to p:
For k = 1 to m:
C[i][j] = C[i][j] + A[i][k] * B[k][j]
3. Return C

Step 2: Code for Matrix Multiplication


#include <stdio.h>

void multiplyMatrices(int n, int matrixA[n][n], int matrixB[n][n], int


result[n][n]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = 0; // Initialize the result matrix element
for (int k = 0; k < n; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}
void printMatrix(int n, int matrix[n][n]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int n;

printf("Enter the size of the square matrices (n x n): ");


scanf("%d", &n);

int matrixA[n][n], matrixB[n][n], result[n][n];

printf("Enter elements of Matrix A (%d x %d):\n", n, n);


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrixA[i][j]);
}
}

printf("Enter elements of Matrix B (%d x %d):\n", n, n);


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrixB[i][j]);
}
}

multiplyMatrices(n, matrixA, matrixB, result);

printf("Resultant Matrix:\n");
printMatrix(n, result);

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.
 Additions (A)
 Multiplications (M)
 Total Operations (T=A+M)

n A (n3 −n2) M (n3 ) T (2 n3−n2)


1 0 1 1
2 8 8 16
3 18 2 45
4 32 64 96
5 50 125 175
6 72 216 288
7 98 343 441
8 128 512 640
9 162 729 891
10 200 1000 1200
For square matrix, the formulas are-
 Additions (A): n×n×(n−1)
 Multiplications (M): n×n×n
 Total (T): A+M
Step 5: Comparing n versus addition, Multiplication and Total operation in
Matrix multiplication.

Output:

You might also like