0% found this document useful (0 votes)
19 views

Saksham Lab

The document contains the source code and outputs for experiments on data structures and algorithms. The experiments include operations on arrays and matrices, dynamic memory allocation, counting words in a text, checking for sparse matrices, and calculating Fibonacci numbers recursively.

Uploaded by

aamirneyazi12
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Saksham Lab

The document contains the source code and outputs for experiments on data structures and algorithms. The experiments include operations on arrays and matrices, dynamic memory allocation, counting words in a text, checking for sparse matrices, and calculating Fibonacci numbers recursively.

Uploaded by

aamirneyazi12
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Laboratory Report

DATA STRUCTURE LAB.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


SCHOOL OF ENGINEERING & TECHNOLOGY

Submitted By
Student Name Saksham Prashar
Student Id 2023379202
Section/Group CS-L
Programme B.Tech (CS&F)
Department Computer Science and Engineering
Session/ 2023-24/2302
Semester
Submitted To
Faculty Name Dr. Krishan Kumar

SHARDA UNIVERSITY
Plot No. 32-34, Knowledge Park III,
Greater Noida, Uttar Pradesh 201310
TABLE OF CONTENT
Sl No Date Aim of the Experiment Signature/date Grade
1.

2023379202 Saksham Prashar


EXPERIMENT NO: 1 DATE:
AIM OF THE EXPERIMENT:
P1: Write a program to compute minimum/maximum of a given array.

P2: Write a menu-based program to perform operations on Linear Array:


i) Insert an element at the Kth Position.
ii) Delete an element from the Kth Position.
iii) Traverse an array element.

P3: Write a program to perform following operations in matrix:


a. Addition
b. Subtraction
c. Multiplication
d. Transpose

SOURCE CODE:

P1:
#include <stdio.h>
void main()
{
int x;
printf("Enter the size of array:");
scanf("%d", &x);
int arr[x];
for (int i = 0; i < x; i++)
{
printf("Enter element%d : ", i + 1);
scanf("%d", &arr[i]);
}
for (int i = 0; i < x - 1; i++)
{
for (int j = i + 1; j < x; j++)
{
if (arr[i] > arr[j])
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
2023379202 Saksham Prashar
printf("Maximum number:%d\n", arr[x-1]);
printf("Minimum number:%d", arr[0]);

P2:

#include <stdio.h>

#define MAX_SIZE 100


int arr[MAX_SIZE];int size = 0;

void insert(int index, int element) {


if (index < 0 || index > size) {
printf("Invalid index.\n");
return;
}
arr[index] = element;
size++;
printf("Element %d inserted at index %d.\n", element, index);
}

void delete(int index) {


if (index < 0 || index >= size) {
printf("Invalid index.\n");
return;
}
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
printf("Element at index %d deleted.\n", index);
}

void traverse() {
printf("Elements in the array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
2023379202 Saksham Prashar
printf("\n");
}

void menu() {
printf("\nMenu:\n");
printf("1. Insert an element at the Kth Position.\n");
printf("2. Delete an element from the Kth Position.\n");
printf("3. Traverse an array element\n");
printf("4. Exit\n");
}

int main() {
int choice, index, element;
while (1) {
menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the index: ");
scanf("%d", &index);
printf("Enter the element: ");
scanf("%d", &element);
insert(index, element);
break;
case 2:
printf("Enter the index: ");
scanf("%d", &index);
delete(index);
break;
case 3:
traverse();
break;
case 4:
return 0;
default:
printf("Invalid choice. Please enter again.\n");
}
}
return 0;
}
2023379202 Saksham Prashar
P3:

#include <stdio.h>
#include <stdbool.h>
int main()
{
int n;
printf("Enter the order of square matrix:");
scanf("%d", &n);
int matrix1[n][n];
int matrix2[n][n];
int r[n][n];
int t_a[n][n];
int t_b[n][n];
printf("\nMATRIX 1\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("Enter element at [%d],[%d] ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
printf("\nMATRIX 2\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("Enter element at [%d],[%d] ", i, j);
scanf("%d", &matrix2[i][j]);
}
}

while (true)
{
printf("\n\nPress 1 for addition of two matrix:");
printf("\nPress 2 for subtraction of two matrix:");

2023379202 Saksham Prashar


printf("\nPress 3 for multiplication of two matrix:");
printf("\nPress 4 for transpose of two matrix:");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:

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


{
for (int j = 0; j < n; j++)
{
r[i][j] = (matrix1[i][j] + matrix2[i][j]);
}
}
printf("\nAddtion of Matrix 1 and Matrix 2 is : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

printf("Element at [%d,%d] is %d\t", i, j, r[i][j]);


}
}
break;
case 2:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
r[i][j] = (matrix1[i][j] - matrix2[i][j]);
}
}
printf("\nSubtraction of Matrix 1 and Matrix 2 is : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

printf("Element at [%d,%d] is %d\t", i, j, r[i][j]);


}
2023379202 Saksham Prashar
}
break;
case 3:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int count = 0;
for (int k = 0; k < n; k++)
{
count += matrix1[i][k] * matrix2[k][j];
}
r[i][j] = count;
}
}
printf("\nMultiplication of Matrix 1 and Matrix 2 is : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

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

break;
case 4:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
t_a[i][j] = matrix1[j][i];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
t_b[i][j] = matrix2[j][i];
}

2023379202 Saksham Prashar


}
printf("\nTranspose of Matrix1 is : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

printf("element at [%d,%d] = %d\t", i, j, t_a[i][j]);


}
}
printf("\nTranspose of matrix 2 is : \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

printf("element at [%d,%d] = %d\t", i, j, t_b[i][j]);


}
}

break;

default:
printf("\nExit!");
return 0;
}
}
}

OUTPUT:

P1:

2023379202 Saksham Prashar


P2:

2023379202 Saksham Prashar


P3:

2023379202 Saksham Prashar


EXPERIMENT NO: 2 DATE:
AIM OF THE EXPERIMENT:
P4: Write a program to implement the following dynamic memory allocation functions:
i) malloc()
ii) calloc()
iii) realloc()
iv) free()
P5: Write a program which counts number of words, lines and characters in a given text.

P6: Write a program to check whether the matrix is a sparse matrix or not.

P7: Write a program to print Fibonacci series using Recursion.

SOURCE CODE:

P1:

#include <stdio.h>
#include <stdlib.h>
int main() {

int *arr1 = (int *) malloc(5 * sizeof(int));


if (arr1 == NULL) {
printf("Memory allocation using malloc failed.\n");
return 1;
}

float *arr2 = (float *) calloc(10, sizeof(float));


if (arr2 == NULL) {
printf("Memory allocation using calloc failed.\n");
free(arr1);
return 1;
}

arr2 = (float *) realloc(arr2, 15 * sizeof(float));


if (arr2 == NULL) {
printf("Memory reallocation failed.\n");
free(arr1);
free(arr2);
return 1;
}

2023379202 Saksham Prashar


arr1[0] = 10;
arr1[1] = 20;
arr1[2] = 30;
arr1[3] = 40;
arr1[4] = 50;
arr2[0] = 1.1;
arr2[1] = 2.2;
arr2[2] = 3.3;
arr2[3] = 4.4;
arr2[4] = 5.5;
arr2[5] = 6.6;
arr2[6] = 7.7;
arr2[7] = 8.8;
arr2[8] = 9.9;
arr2[9] = 10.10;
arr2[10] = 11.11;

printf("Array allocated using malloc: ");


for (int i = 0; i < 5; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
printf("Array allocated using calloc: ");
for (int i = 0; i < 10; i++) {
printf("%.2f ", arr2[i]);
}
printf("\n");

free(arr1);
free(arr2);
return 0;
}

P2:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
int character_count = 0;
2023379202 Saksham Prashar
int word_count = 0;
int line_count = 0;
char str[] = "This is a sample string.\nIt contains multiple lines.\nAnd some words.";
for(int i = 0; i < strlen(str); i++) {
character_count++;
if(str[i] == '\n') {
line_count++;
}
if(isspace(str[i]) && isalpha(str[i+1])) {
word_count++;
}
}
if(character_count != 0) {
line_count++;
}
if(character_count != 0) {
word_count++;
}
printf("Number of Characters: %d\n", character_count);
printf("Number of Words: %d\n", word_count);
printf("Number of Lines: %d\n", line_count);
return 0;
}

P3:

#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void checkSparse(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {
int count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
count++;
}
}
}
if (count > (rows * cols) / 2) {

2023379202 Saksham Prashar


printf("The given matrix is a sparse matrix.\n");
} else {
printf("The given matrix is not a sparse matrix.\n");
}
}
int main() {
int matrix[MAX_ROWS][MAX_COLS];
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
checkSparse(matrix, rows, cols);
return 0;
}

P4:

#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
void printFibonacci(int n) {
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
}
int main() {
int n;
printf("Enter the number of Fibonacci numbers to print: ");
2023379202 Saksham Prashar
scanf("%d", &n);
printFibonacci(n);
return 0;
}

Output:
P1:

P2:

P3:

P4:

2023379202 Saksham Prashar

You might also like