0% found this document useful (0 votes)
3 views23 pages

dsfile

The document is a lab file for a Data Structure course (BCS-351) submitted by Ayushi Rajput. It includes a list of experiments focusing on C programming for array operations, such as insertion and deletion, as well as operations on 2D arrays like matrix multiplication and diagonal differences. Additionally, it contains LeetCode problems related to the 'Two Sum' and finding the 'Second Largest Element in an Array'.

Uploaded by

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

dsfile

The document is a lab file for a Data Structure course (BCS-351) submitted by Ayushi Rajput. It includes a list of experiments focusing on C programming for array operations, such as insertion and deletion, as well as operations on 2D arrays like matrix multiplication and diagonal differences. Additionally, it contains LeetCode problems related to the 'Two Sum' and finding the 'Second Largest Element in an Array'.

Uploaded by

exotiiiickido
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab File

Data Structure
(BCS -351)

Submitted By: Submitted To:


Name: Ayushi Rajput Ms. Neeraj Mam

University Roll NO.: 2501920100194

Branch: CSE

Section: CSE2D

Group: G1
Experiment List
Subject Name: DS Lab Subject Code:BCS
351

[Link]. Experiment COs Blooms


Level
1. Write a C program to create array and perform various

operations:

a) Insertion at: i) Beg ii) End iii) Any position

b) Deletion from: i) Beg ii) End iii) Any position

Leet Code Problem

Two Sum

[Link]

2. Write a C program to perform various operations 2D


array:

a)Multiplication of 2 matrix

b)Finding difference of sum of two diagonal elements

LeetCode Problem

Second Largest Element in an Array

[Link]
number/
Experiment 1
Write a program in C to create array and perform various operations:

1) Iinsertion at:

1)Beg

#include <stdio.h>

int main() {

int arr[100];

int size, newValue, i;

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

scanf("%d", &arr[i]);

printf("Enter the value to insert at the beginning: ");

scanf("%d", &newValue);

for (i = size; i > 0; i--) {

arr[i] = arr[i - 1];

arr[0] = newValue;

size++;

printf("Array after insertion:\n");

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");
return 0; }

OUTPUT:
2)End

#include <stdio.h>

int main() {

int arr[100];

int size, i, newElement;

printf("Enter the initial number of elements (max 99): ");

scanf("%d", &size);

if (size >= 100 || size < 0) {

printf("Invalid size!\n");

return 1;

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

scanf("%d", &arr[i]);

printf("\nOriginal array: ");

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

printf("\nEnter the new element to insert at the end: ");

scanf("%d", &newElement);

arr[size] = newElement;
size++;

printf("Array after insertion: ");

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:
3)Any Position

#include <stdio.h>

int main() {

int arr[100];

int size, i, element, position;

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

printf("\nEnter the element to insert: ");

scanf("%d", &element);

printf("Enter the position (1 to %d) to insert it: ", size + 1);

scanf("%d", &position);

if (position < 1 || position > size + 1) {

printf("Invalid position! Insertion is not possible.\n");

} else {

for (i = size - 1; i >= position - 1; i--) {

arr[i + 1] = arr[i];

arr[position - 1] = element;

size++;

printf("\nArray after insertion:\n");

for (i = 0; i < size; i++) {


printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:
2)Deletion at

1)Beginning

#include <stdio.h>

int main() {

int n, i;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

if (n <= 0) {

printf("Array is empty. Deletion not possible.\n");

return 0;

int arr[n];

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

printf("\nOriginal array: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

for (i = 0; i < n - 1; i++) {

arr[i] = arr[i + 1];

n--;
printf("Array after deletion at beginning: ");

for (i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:
2) End

#include <stdio.h>

int main() {

int arr[100];

int size, i;

printf("Enter the number of elements in the array: ");

scanf("%d", &size);

if (size <= 0 || size > 100) {

printf("Invalid size! Please enter a size between 1 and 100.\n");

return 1;

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);

printf("\nOriginal array: ");

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

if (size > 0) {

size--;

printf("\nSuccessfully deleted the last element.\n");

} else {

printf("\nArray is empty! Deletion not possible.\n");


}

printf("Array after deletion at end: ");

if (size == 0) {

printf("Array is now empty.");

} else {

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:
3) Any Position

#include <stdio.h>

#define MAX_SIZE 100

void displayArray(int arr[], int size);

int deleteAtPosition(int arr[], int size, int position);

int main() {

int arr[MAX_SIZE];

int size, i, position;

printf("Enter the number of elements in the array (max %d): ", MAX_SIZE);

scanf("%d", &size);

if (size <= 0 || size > MAX_SIZE) {

printf("Invalid size entry.\n");

return 1;}

printf("Enter %d elements:\n", size);

for (i = 0; i < size; i++) {

printf("Element %d: ", i + 1);

scanf("%d", &arr[i]);}

printf("\nInitial ");

displayArray(arr, size);

printf("\nEnter the position of the element to delete (1 to %d): ", size);

scanf("%d", &position);

size = deleteAtPosition(arr, size, position);

printf("\nResulting ");

displayArray(arr, size);

return 0;}
int deleteAtPosition(int arr[], int size, int position) {

if (size <= 0) {

printf("Error: Array underflow. No elements to delete.\n");

return size; }

if (position < 1 || position > size) {

printf("Error: Invalid position. Deletion not possible.\n");

return size; }

for (int i = position - 1; i < size - 1; i++) {

arr[i] = arr[i + 1]; }

printf("Element at position %d deleted successfully.\n", position);

return size – 1;}

void displayArray(int arr[], int size) {

if (size == 0) {

printf("Array is empty.\n");

return; }

printf("Array elements: ");

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]); }

printf("\n");

OUTPUT:
LeetCode Problem :

TWOSUM

class Solution {

public int[] twoSum(int[] nums, int target) {

int len=[Link];

for(int i=0;i<len;i++){

for(int j=i+1;j<len;j++){

if(nums[i]+nums[j]==target){

return new int[]{i,j};

return null;}}
Experiment 2

Write a program in C to perform various operations on 2D array:

a)Multiplication of 2 matrices

#include <stdio.h>

#define MAX 10

int main() {

int mat1[MAX][MAX], mat2[MAX][MAX], result[MAX][MAX];

int r1, c1, r2, c2;

int i, j, k;

printf("Enter rows and columns for the first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and columns for the second matrix: ");

scanf("%d %d", &r2, &c2);

if (c1 != r2) {

printf("\nError! Multiplication is not possible.\n");

printf("Columns of the first matrix must match rows of the second matrix.\n");

return 0;

printf("\nEnter elements of the first matrix (%dx%d):\n", r1, c1);

for (i = 0; i < r1; i++) {

for (j = 0; j < c1; j++) {

printf("Enter element [%d][%d]: ", i, j);

scanf("%d", &mat1[i][j]);

}
printf("\nEnter elements of the second matrix (%dx%d):\n", r2, c2);

for (i = 0; i < r2; i++) {

for (j = 0; j < c2; j++) {

printf("Enter element [%d][%d]: ", i, j);

scanf("%d", &mat2[i][j]); } }

for (i = 0; i < r1; i++) {

for (j = 0; j < c2; j++) {

result[i][j] = 0; } }

for (i = 0; i < r1; i++) {

for (j = 0; j < c2; j++) {

for (k = 0; k < c1; k++) {

result[i][j] += mat1[i][k] * mat2[k][j];

} } }

printf("\nResultant Product Matrix (%dx%d):\n", r1, c2);

for (i = 0; i < r1; i++) {

for (j = 0; j < c2; j++) {

printf("%d\t", result[i][j]);

printf("\n"); }

return 0;}

OUTPUT:
b) Finding difference of sum of two diagonal elements

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

void readMatrix(int mat[MAX][MAX], int size);

void displayMatrix(int mat[MAX][MAX], int size);

void calculateDiagonalDifference(int mat[MAX][MAX], int size);

int main() {

int mat[MAX][MAX];

int size;

printf("Enter the size of the square matrix (e.g., 3 for 3x3): ");

if (scanf("%d", &size) != 1 || size <= 0 || size > MAX) {

printf("Invalid matrix size.\n");

return 1;

readMatrix(mat, size);

displayMatrix(mat, size);

calculateDiagonalDifference(mat, size);

return 0;

void readMatrix(int mat[MAX][MAX], int size) {

printf("\nEnter the elements of the %dx%d matrix:\n", size, size);

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

printf("Element [%d][%d]: ", i, j);


scanf("%d", &mat[i][ j]);

}}}

void displayMatrix(int mat[MAX][MAX], int size) {

printf("\nThe entered matrix is:\n");

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

printf("%d\t", mat[i][ j]);

printf("\n"); }}

void calculateDiagonalDifference(int mat[MAX][MAX], int size) {

int principal_sum = 0;

int secondary_sum = 0;

for (int i = 0; i < size; i++) {

principal_sum += mat[i][i];

secondary_sum += mat[i][size - 1 - i]; }

int difference = abs(principal_sum - secondary_sum);

printf("\n--- Diagonal Operations Results ---\n");

printf("Sum of Principal Diagonal: %d\n", principal_sum);

printf("Sum of Secondary Diagonal: %d\n", secondary_sum);

printf("Absolute Difference: |%d - %d| = %d\n", principal_sum, secondary_sum,


difference);

}
OUTPUT:

LeetCode Problem:

Second Largest Element in Array:

#include <stdio.h>

#include <limits.h>

int findSecondLargest(int arr[], int size) {

if (size < 2) {

return -1;

int largest = INT_MIN;

int second_largest = INT_MIN;

for (int i = 0; i < size; i++) {

if (arr[i] > largest) {

second_largest = largest;

largest = arr[i];

else if (arr[i] > second_largest && arr[i] != largest) {

second_largest = arr[i];

}
if (second_largest == INT_MIN) {

return -1;

return second_largest;

int main() {

int arr[] = {12, 35, 1, 10, 34, 1};

int size = sizeof(arr) / sizeof(arr[0]);

int result = findSecondLargest(arr, size);

if (result != -1) {

printf("The second largest element is %d\n", result);

} else {

printf("There is no distinct second largest element.\n");

return 0;

}
OUTPUT:

You might also like