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

Data Structure Lab File BCS 351

The document is a lab file for the Data Structure course at Raj Kumar Goel Institute of Technology and Management, detailing various programming tasks related to arrays. It includes objectives and code implementations for inserting and printing elements in 1D and 2D arrays, performing arithmetic operations on 2D arrays, and searching and sorting algorithms. The lab file is submitted by a student named Gaurang Parashar for the 2025-26 academic session.

Uploaded by

Robin
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 views19 pages

Data Structure Lab File BCS 351

The document is a lab file for the Data Structure course at Raj Kumar Goel Institute of Technology and Management, detailing various programming tasks related to arrays. It includes objectives and code implementations for inserting and printing elements in 1D and 2D arrays, performing arithmetic operations on 2D arrays, and searching and sorting algorithms. The lab file is submitted by a student named Gaurang Parashar for the 2025-26 academic session.

Uploaded by

Robin
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

RAJ KUMAR GOEL INSTITUTE OF TECHNOLOGY AND

MANAGEMENT, GHAZIABAD
(College Code - 333)
(Affiliated to Dr. A.P.J Abdul Kalam Technical University, Lucknow)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SESSION 2025-26
Odd Semester

DATA STRUCTURE
LAB FILE (BCS 351)

SUBMITTED TO: SUBMITTED BY:

Ms. Neha Tyagi NAME: GAURANG PARASHAR


Assistant Professor ROLL NO. : 2403330100073
Department of CSE B. Tech (CSE) 2nd Year
INDEX
[Link]. Objective Date Sign
1. Write a Program to insert and print Elements in 1D Array.
2. Write a Program to insert and print Elements in 2D Array.
3. Write a Program to Add, Subtract and Multiply 2D Array.
4. Write a program for Search an element in an array using Linear search.

5. Write a program to search an element in an array using Binary search.

6. Write a program to sort an array using Bubble sort.


7. Write a program to sort an array using Insertion sort.
8. Write a program to sort an array using selection sort.
1. Write a Program to insert and print Elements in 1D Array.

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


int n;
printf("Enter the Number of elements you want to insert in Array: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter Element %d: ",i+1); scanf("%d",&arr[i]);
}
for (int j = 0; j < n; j++)
{
printf("Element at index %d is %d\n",j,arr[j]);
}
return 0;
}
2. Write a Program to insert and print Elements in 2D Array.

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


int row,col;
printf("Enter Number of rows: "); scanf("%d",&row);
printf("Enter Number of Columns: "); scanf("%d",&col);
int arr[row][col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf("Enter Element [%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
for (int k = 0; k < row; k++)
{
for (int l = 0; l < col; l++)
{
printf("The Element at index [%d][%d] is %d\n",k,l,arr[k][l]);
}
}
}
3. Write a Program to Add, Subtract and Multiply 2D Array.

#include<stdio.h>
void printarray(int row1,int col1,int row2,int col2,int result[10][10]);
int multiplication(int arr1[10][10],int arr2[10][10],int result[10][10],int row1,int col1 ,int
row2,int col2){
if (col1!=row2)
{
printf("Multiplication of Matrix is not possible!!\n"); return 0;
}
else{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
result[i][j]=0;
}
}
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
for (int k = 0; k < col1; k++)
{
result[i][j]+=arr1[i][k]*arr2[k][j];
}
}
}
printf("Array after Multiplication\n"); printarray(row1,col1,row2,col2,result);
}
}
void addition(int arr1[10][10],int arr2[10][10],int sum[10][10],int row,int col){
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
sum[i][j]=arr1[i][j]+arr2[i][j];
}
}
}
void subtraction(int arr1[10][10],int arr2[10][10],int sub[10][10],int row,int col){ for (int i
= 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
sub[i][j]=arr1[i][j]-arr2[i][j];
}

}
}
void printarray(int row1,int col1,int row2,int col2,int result[10][10]){
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
printf("Element at index [%d][%d] is %d\n",i,j,result[i][j]);
}
}
}
int main(){
int row1, col1, row2, col2;
int arr1[10][10],arr2[10][10],result[10][10]; printf("Enter Number
of Rows of matrix 1: "); scanf("%d",&row1);
printf("Enter Number of Columns of matrix 1: ");
scanf("%d",&col1);
printf("Enter Number of Rows of matrix 2: "); scanf("%d",&row2);
printf("Enter Number of Columns of matrix 2: ");
scanf("%d",&col2);
printf("Enter Elements in Matrix 1!!\n"); for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
printf("Enter Element [%d][%d]: ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Enter Elements in Matrix 2!!\n"); for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
printf("Enter Element [%d][%d]: ",i,j);
scanf("%d",&arr2[i][j]);
}
}

int r=multiplication(arr1,arr2,result,row1,col1,row2,col2);
if(row1==row2 &&
col1==col2){ addition(arr1,arr2,result,row1,col1);

printf("Array after Addition\n");


printarray(row1,col1,row2,col2,result);
subtraction(arr1,arr2,result,row1,col1); printf("Array after
Subtraction\n"); printarray(row1,col1,row2,col2,result);
}
else{
printf("Matrix Additon and Subtraction is not Possible!!\n");
return 0;
}

return 0;
}
4. Write a program for Search an element in an array using
Linear search.
#include<stdio.h>
int linear_search(int arr[],int n,int target){ for (int i
= 0; i < n; i++)
{
if (arr[i]==target)
{
return i;
}
}
return -1;
}
int main(){ int n;
printf("Enter the Number of Elements: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
int target;
printf("Enter the element you want to search: ");
scanf("%d",&target);
int result=linear_search(arr,n,target); if(result!=-
1){
printf("Element is found at index %d",result);
}
else{
printf("Element not found!!");
}
return 0;
}
5. Write a program to search an element in an array using
Binary search.
#include<stdio.h>
int binary_search(int arr[],int n,int target){ int
low=0;
int high=n-1; while (low<=high)
{
int mid=low+(high-low)/2; if (arr[mid]==target)
{
return mid;
}
else if(arr[mid]<target){ low=mid+1;
}
else{
high=mid-1;
}
}
return -1;
}
int main(){
int n;
printf("Enter the Number of Elements: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
int target;
printf("Enter the element you want to search: ");
scanf("%d",&target);
int result=binary_search(arr,n,target); if(result!=-
1){
printf("Element is found at index %d",result);
}
else{
printf("Element not found!!");
}
return 0;
}
6. Write a program to sort an array using Bubble sort.

#include<stdio.h>
void bubble_sort(int arr[],int n){ for (int i = 0; i <
n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j]>arr[j+1])
{
int temp=arr[j]; arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main(){ int n;
printf("Enter the Number of Elements: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("Array before Sorting!\n"); for (int i = 0; i
< n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}

bubble_sort(arr,n); printf("Array after


Sorting!\n"); for (int i = 0; i < n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}
return 0;
}
7. Write a program to sort an array using Insertion sort.

#include<stdio.h>
void insertion_sort(int arr[],int n){ for (int i = 1; i <
n; i++)
{
int j=i-1;
int key=arr[i];
while (j>=0 && key>arr[j])
{
arr[j+1]=arr[j]; j=j-1;
}
arr[j+1]=key;
}
}
int main(){ int n;
printf("Enter the Number of Elements: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("Array before Sorting!\n"); for (int i = 0; i
< n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}
insertion_sort(arr,n); printf("Array after
Sorting!\n"); for (int i = 0; i < n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}
return 0;
}
8. Write a program to sort an array using selection sort.

#include<stdio.h>
void selection_sort(int arr[],int n){ int minidx;
for (int i = 0; i < n-1; i++)
{
minidx=i;
for (int j = i+1; j < n; j++)
{
if(arr[j]<arr[minidx]){ minidx=j;
}
}
int temp=arr[minidx]; arr[minidx]=arr[i];
arr[i]=temp;

}
}
int main(){ int n;
printf("Enter the Number of Elements: ");
scanf("%d",&n);
int arr[n];
for (int i = 0; i < n; i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("Array before Sorting!\n"); for (int i = 0; i
< n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}
selection_sort(arr,n); printf("Array after
Sorting!\n"); for (int i = 0; i < n; i++)
{
printf("Element at index %d is %d\n",i,arr[i]);
}
return 0;
}

You might also like