0% found this document useful (0 votes)
44 views14 pages

DS Lab Manual

The document contains 9 programs related to data structures. The programs cover linear search, 2D array search, merging sorted arrays, matrix operations, call by value vs reference, string operations, binary search iterative and recursive, bubble sort and selection sort.

Uploaded by

enord.ros
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)
44 views14 pages

DS Lab Manual

The document contains 9 programs related to data structures. The programs cover linear search, 2D array search, merging sorted arrays, matrix operations, call by value vs reference, string operations, binary search iterative and recursive, bubble sort and selection sort.

Uploaded by

enord.ros
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
You are on page 1/ 14

DELHI INSTITUTE OF ENGINEERING

AND TECHNOLOGY, MEERUT

LAB MANUAL
FOR

Subject :- DATA STRUCTURE (BCS351)


PROGRAM NO.1

Aim: - To search an element in the array using Linear Search.

#include<stdio.h>

#include<conio.h>

void main()
{
int a[10],i,item,flag=0;
clrscr();
printf("Enter the data in the array");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d",&item);
for(i=0;i<10;i++)
{
if(item==a[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("Element Not Found");
else
printf("Element Found at Position =%d",i);
getch();
}

PROGRAM NO.2

Aim: - To search an element in the 2-dimensional array using Linear Search.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,item,flag=0;
clrscr();
printf("Enter the data in the array");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element to be searched");
scanf("%d",&item);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(item==a[i][j])
{
flag=1;
printf("Element found at position =%d,%d",i,j);
}
}
}
if(flag==0)
printf("Element Not Found");

getch();
}

PROGRAM NO.3
Aim: - To merge two sorted array into one sorted array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i,j,k,n,m,t;
clrscr();
printf("Enter size of Array A\n");
scanf("%d",&n);
printf("Enter the data in Array A\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter size of Array B\n");
scanf("%d",&m);
printf("Enter the data in Array B\n");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
i=j=k=0;
while(i<n&&j<m)
{
if(a[i]<b[j])
c[k++]=a[i++];
else
if(a[i]>=b[j])
c[k++]=b[j++];
}

PROGRAM NO.4

Aim: - To perform the following operation in Matrix


1. Addition 2. Subtraction 3. Multiplication 4. Transpose
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],i,j,k;
clrscr();
printf("Enter the data in Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the data in Martix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition of two Matrix A and B is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
printf("Subtraction of two Matrix A and B is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("Transpose of Matrix C is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
d[j][i]=c[i][j];
}
}
for(i=0;i<3;i++)
{

for(j=0;j<3;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
printf("Multiplication of Matrix A and B is\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\n"); for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");

getch();

}
PROGRAM NO.5

Aim: - To perform the swapping of two numbers using call by value and call byreference.

#include<stdio.h>
#include<conio.h>
void
swapbyvalue(int,int); void
swapbyref(int*,int*); void
main()
{
int a,b;
clrscr();
printf("Enter the two numbers");
scanf("%d%d",&a,&b);
swapbyvalue(a,b);
swapbyref(&a,&b);
printf("\nNumber after swapping by Reference\n");
printf("\na=%d\nb=%d",a,b);
getch();
}
void swapbyvalue(int x, int y)
{
int
temp;
temp=x;
x=y;
y=temp;
printf("\nNumbers after swapping by value are\n");
printf("a=%d",x);
printf("\nb=%d",y);
}
void swapbyref(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

PROGRAM NO.6

Aim: - To perform following operation on strings using string functions


1. Addition 2. Copying 3. Reverse 4. Length of String.

#include<conio.h
>
#include<stdio.h>
#include<string.h
>void main()
{
char a[20],b[20],c[20];
int l;
clrscr();
printf("Enter the First String");
scanf("%s",&a);
printf("Enter the Second String");
scanf("%s",&b);
strcat(a,b);
printf("\nConcatenation of String a and b is:%s",a);
l=strlen(a);
printf("\nLength of String is %d",l);
strcpy(c,a);
printf("\nthe Copied String is %s",c);
strrev(a);
printf("\nreverse of String is %s",a);

getch();
}
PROGRAM NO.7 (a)

Aim: - To search an element in the array using Iterative Binary Search.

#include<stdio.h>
#include<conio.h
>void main()
{
int a[20],n,mid,beg,i,end,item,loc=-1;
clrscr();
printf("Enter the number of elements to be entered\n");
scanf("%d",&n);
printf("Enter the elements in ascending order");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d",&item);
beg=0; end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc=mid;
break;
}
else if(a[mid]<item)
beg=mid+1;
else
end=mid-
1;
}
if(loc==-1)
printf("Element Not Present");
else
printf("Element found at =%d",loc);
getch();
}
PROGRAM NO.7 (b)

Aim: - To search an element in the array using Recursive Binary Search.

#include<stdio.h>
#include<conio.h>
void binary(int [],int,int);
void main()
{
int a[20],i,n,item;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("enter the data in array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d",&item);

binary(a,n,item);
getch();
}

void binary(int a[],int n,int item)


{
int beg,end,mid,loc=-1;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc=mid;
break;
}

else if(item>a[mid])
beg=mid+1
;else
end=mid-1;
}
if(loc==-1)
printf("Element not Found");
else
printf("Element Found at position = %d",loc);
}
PROGRAM NO.8

Aim: - To implement Bubble Sort.

#include<stdio.h>
#include<conio.h>
void bubble(int [],int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
getch();
}
void bubble(int a[],int n)
{
int i,temp,j,p;
for(i=1;i<n;i++)
{
for(p=0;p<n-i;p++)
{
if(a[p]>a[p+1])
{
temp=a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
PROGRAM NO.9

Aim: - To implement Selection Sort.

#include<stdio.h>
#include<conio.h>
void select(int [],int);
void bubble(int [],int);
int min(int [],int,int);

void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
select(a,n);
getch();
}
void bubble(int a[],int n)
{
int i,temp,p;
for(i=1;i<n;i++)
{
for(p=0;p<n-i;p++)
{
if(a[p]>a[p+1])
{
temp=a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
printf("\nData After Bubble Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}

void select(int a[],int n)


{
int i,loc,temp;
loc=0;
temp=0;
for(i=0;i<n;i++)
{
loc=min(a,i,n);temp=a[loc];
a[loc]=a[i];
a[i]=temp;
}
printf("\nData After Selection Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}

PROGRAM NO.10

Aim: - To implement Insertion Sort.

#include<stdio.h>
#include<conio.h>

void insert(int [],int);


void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insert(a,n);
getch();
}

void insert(int a[],int n)


{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=temp;
}
printf("Data After Insertion Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);

You might also like