DATA STRUCTURES-Practical List
B.Sc.Physical Science,2nd Semester
Name- garima raman
Roll no – 23582059
Submitted to – Mrs Mamta Gupta
Question 1
Implement matrix addition and multiplication.
CODE
#include <iostream>
#include <vector>
using namespace std;
// Function to add two matrices
vector<vector<int>> matrixAddition(const vector<vector<int>>& mat1, const
vector<vector<int>>& mat2) {
int rows = mat1.size();
int cols = mat1[0].size();
vector<vector<int>> result(rows, vector<int>(cols, 0));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
return result;
// Function to multiply two matrices
vector<vector<int>> matrixMultiplication(const vector<vector<int>>& mat1, const
vector<vector<int>>& mat2) {
int rows1 = mat1.size();
int cols1 = mat1[0].size();
int cols2 = mat2[0].size();
vector<vector<int>> result(rows1, vector<int>(cols2, 0));
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
return result;
// Function to display a matrix
void displayMatrix(const vector<vector<int>>& mat) {
for (const auto& row : mat) {
for (int val : row) {
cout << val << " ";
cout << endl;
int main() {
vector<vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
vector<vector<int>> matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
cout << "Matrix 1:" << endl;
displayMatrix(matrix1);
cout << endl;
cout << "Matrix 2:" << endl;
displayMatrix(matrix2);
cout << endl;
vector<vector<int>> additionResult = matrixAddition(matrix1, matrix2);
cout << "Matrix Addition Result:" << endl;
displayMatrix(additionResult);
cout << endl;
vector<vector<int>> multiplicationResult = matrixMultiplication(matrix1, matrix2);
cout << "Matrix Multiplication Result:" << endl;
displayMatrix(multiplicationResult);
return 0;
Question 2
Implement Insertion sort. Your function should also print the number of comparisons it did to
reach the sorted array. Compare number of comparisons your algorithm did for best case input
and worst case input
Code
#include <iostream>
#include <vector>
using namespace std;
int insertionSort(vector<int>& arr) {
int comparisons = 0;
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
comparisons++;
arr[j + 1] = key;
return comparisons;
int main() {
vector<int> arr = {12, 11, 13, 5, 6};
int comparisons = insertionSort(arr);
cout << "Sorted array is:";
for (int i = 0; i < arr.size(); i++) {
cout << " " << arr[i];
cout << endl;
cout << "Number of comparisons: " << comparisons << endl;
return 0;
Question 3
3. mplement following recursive functions:
a) Factorial of a number
CODE
#include <iostream>
using namespace std;
// Recursive function to calculate the factorial of a number
unsigned long long factorial(int n) {
// Base case: factorial of 0 is 1
if (n == 0) {
return 1;
// Recursive case: multiply n with factorial of (n-1)
return n * factorial(n - 1);
int main() {
int number;
cout << "Enter a non-negative integer: ";
cin >> number;
if (number < 0) {
cout << "Factorial is not defined for negative numbers." << endl;
} else {
unsigned long long result = factorial(number);
cout << "Factorial of " << number << " is: " << result << endl;
return 0;
b)Nth Fibonacci number
#include <iostream>
using namespace std;
// Recursive function to calculate the nth Fibonacci number
unsigned long long fibonacci(int n) {
if (n <= 1) {
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
int main() {
int fibN = 7;
cout << "Fibonacci number at position " << fibN << " is: " << fibonacci(fibN) << endl;
return 0;
c) Exponent x" where x and n are parameters
#include <iostream>
using namespace std;
// Recursive function to calculate exponent x^n
double power(double x, int n) {
if (n == 0) {
return 1;
if (n < 0) {
return 1 / power(x, -n);
}
return x * power(x, n - 1);
int main() {
double base = 2.0;
int exponent = 3;
cout << base << " raised to the power " << exponent << " is: " << power(base, exponent) <<
endl;
return 0;
d)Array functions:
i. Sum of array elements
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to calculate the sum of array elements
int arraySum(const vector<int>& arr, int size) {
if (size <= 0) {
return 0;
return arr[size - 1] + arraySum(arr, size - 1);
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << "Sum of array elements: " << arraySum(arr, arr.size()) << endl;
return 0;
ii)Maximum of an array
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to find the maximum element in an array
int arrayMax(const vector<int>& arr, int size) {
if (size == 1) {
return arr[0];
return max(arr[size - 1], arrayMax(arr, size - 1));
int main() {
vector<int> arr = {1, 7, 3, 9, 5};
cout << "Maximum element in the array: " << arrayMax(arr, arr.size()) << endl;
return 0;
iii)Reverse an array
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to reverse an array
void reverseArray(vector<int>& arr, int start, int end) {
if (start >= end) {
return;
swap(arr[start], arr[end]);
reverseArray(arr, start + 1, end - 1);
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << "Original array: ";
for (int num : arr) {
cout << num << " ";
cout << endl;
reverseArray(arr, 0, arr.size() - 1);
cout << "Reversed array: ";
for (int num : arr) {
cout << num << " ";
cout << endl;
return 0;