DSA ASSIGNMENT 1
KARAN PRATAP SINGH RATHORE
B22CH013
Q1) MATRIX MULTIPLICATION :
PART 1 : Row To Column Multiplication
● The code performs row to column matrix multiplication for square matrices of size N x N.
● Memory is dynamically allocated for three matrices A,B and C using the malloc function.
● C programming language is used and special function as clock() , and time.h header file is
used for time measurement.
● Matrices A and B are initialized with random values between o and 299, while matrix C is
initialized with zeros.
● Code has time complexity of O(N^3) .
● The code includes a section to free the dynamically allocated memory for matrices A, B and
C to prevent memory leaks.
Average time taken by code is 1063.34
PART 2 : Row To Row Multiplication
● The code defines a function mat_transpose a matrix in place . The transposition is performed
on matrix B , Swapping elements below the main diagonal with their corresponding elements
above the main diagonal.
● Code has the time complexity of O(N^3).
● The code includes a section to free the dynamically allocated memory for matrices A,b and C
to prevent memory leaks .
Average time taken is 1061.78
PART 3 : 1-D Multiplication
● Matrices A, B, and C are represented as 1D arrays, reducing the need for multiple
allocations and potentially improving cache locality.
● The code efficiently performs matrix multiplication using a single-dimensional array
representation, showcasing a different approach to matrix storage.
● Code has a time complexity of O(N^3) as the matrix multiplication is performed using
three nested loops to iterate through the rows and columns of the matrices.
Average time taken by code is 1027.529
Comparsion:
Q2) Fibonacci Series Recurrence:
PART 1 : Recursive Algorithm Method
1. User Interaction:
● The program prompts the user to input a value for n representing the Fibonacci term.
2. Recursive Fibonacci Function:
● A recursive function, recursiveFibonacci, calculates the Fibonacci term for the given input
n.
● Base cases handle scenarios where n is 1 or 2, returning 0 and 1, respectively.
● For n greater than 2, the function recursively calls itself for n-1 and n-2 and sums the results.
3. Calculation and Output:
● The program calculates the Fibonacci term for the user-provided value of n using the
recursive function.
● The result is printed to the console using printf.
4. Time Measurement:
● The program records the start time using clock() before performing the calculations.
● After the calculations, it records the end time and calculates the elapsed time in
milliseconds.
● The total time taken to execute the recursive Fibonacci algorithm is then printed.
5. Execution Efficiency:
● The recursive approach is conceptually simple but has an exponential time complexity.
● For larger values of n, the recursive approach may result in slower execution due to
redundant calculations.
Time complexity of code is O(2^N).
PART 2 : For Loop Method
● The code uses an iterative approach to calculate the Fibonacci number at position N. It uses
a loop to iteratively compute Fibonacci numbers up to the desired position.
● The variables fib and temp are used to store the current Fibonacci number and the previous
Fibonacci number, respectively. They are initialized appropriately for the first two iterations.
● The for loop iterates N times, calculating Fibonacci numbers and updating the variables fib
and temp.
● The code handles the base cases of Fibonacci (0 and 1) explicitly and starts the iteration
from index 2.
● The program uses the C standard library's time.h to measure the time it takes to calculate
the Fibonacci number at position N.
● Time Complexity is equals to O(N).