Dsa Lab Practical File
-
Upload
subhash-kumar-yadav -
Category
Documents
-
view
170 -
download
4
description
Transcript of Dsa Lab Practical File
NARNAUL (HARYANA)NARNAUL (HARYANA)
PRACTICAL FILE
OF
DSA LAB
USING ‘C’ LAB
Submitted in Partial Fullfillment of the Requirment in
Electronics & Communication Engineering
(Session 2012-2015)
Submitted to : Submitted by:
Mrs.Mamta Yadav Subhash Kumar Yadav
( Lectt. ) ECE 3rd Sem.
Roll No:12ECEL26
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
NARNAUL (HARYANA)NARNAUL (HARYANA)
CERTIFICATECERTIFICATE
This to certify that Subhash Kumar Yadav has completed her practical lab file of DATA STRUCTURE LAB. OF COMPUTATIONAL PROGRAMMING LAB under my guidance and completed it to my total satisfaction submitted in partial fullfillment of the requirment in
Electronics & Communication EngineeringElectronics & Communication Engineering
(Session 2012-2015)
Submitted to : Submitted by:
Mrs. Mamta Yadav Subhash Kumar Yadav
( Lectt. ) ECE 3rd Sem.
Roll No:12ECEL26
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
DSA LAB
INDEX
Sr.No
Program NamePag
e No.
Date
Lectt. Sign
Remarks
1Program to search an element in a two dimensional array using Linear Search
1-2
2Program perform Multiplication of two matrix
3-4
3Program perform Addition of two matrix
5-6
4Program to sort an array using Bubble Sort technique
7-8
5Program to sort an array using Quick Sort technique
9-10
6Program to sort an array using Selection Sort technique
11-12
7 Program to find factorial of a Number 13
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No. 1
Aim: Program to search an element in a two dimensional array using Linear Search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,j,i,item,loc=-1;
clrscr();
printf("Enter no of element\n");
scanf("%d",&n);
printf("Enter the number\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search\n");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(item==a[i])
{
loc=1;
break;
}
}
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
if(loc>=0)
printf("%d is found in position%d",item,loc+1);
else
printf("item does not exist");
getch();
}
Output:
Enter no of element
5
Enter the number
45
67
87
46
96
Enter the number to be search
96
96is found in position2
Output:
Enter no of element
5
Enter the number
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
23
76
98
21
66
Enter the number to be search
11
item does not exist
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No. 2
Aim: Program perform Multiplication of two matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],m[2][2],i,j,k;
clrscr();
printf("Enter the element of first matrix\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element of second matrix\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Multiplication of two matrix is\n");
for(i=1;i<=2;i++)
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
{
for(j=0;j<=2;j++)
{
m[i][j]=0;
for(k=0;k<=2;k++)
{
m[i][j]=m[i][j]+a[i][j]*b[k][j];
}
printf("Result is %d\n",m[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter the element of first matrix
12
23
34
45
56
78
89
90
91
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Enter the element of second matrix
21
31
42
52
63
73
87
49
48
Multiplication of two matrix is
Result is 7680
Result is 8008
Result is 16643
Result is 12371
Result is 10080
Result is 12285
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No: 3
Aim: Program perform Addition of two matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],s[3][3],i,j;
clrscr();
printf("Enter the element of first matrix\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element of second matrix\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Addition of two matrix is\n");
for(i=0;i<=2;i++)
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
{
for(j=0;j<=2;j++)
{
s[i][j]=a[i][j]+b[i][j];
printf("%d\t",s[i][j]);
}
printf("\n");
getch();
}
}
Output:
Enter the element of first matrix
12
23
34
56
67
78
89
91
23
Enter the element of second matrix
21
45
76
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
97
49
96
95
53
33
Addition of two matrix is
33 68 110
153 116 174
184 144 56
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No:4
Aim: Program to sort an array using Bubble Sort technique
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,i,j,temp;
clrscr();
printf("How many elements in the array\n");
scanf("%d",&n);
printf("Enter the element of array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
}
printf("Sorted array is\n");
for(i=0;i<=n-1;i++)
printf("%d\n",a[i]);
getch();
}
Output:
How many elements in the array
5
Enter the element of array
23
65
98
29
11
Sorted array is
11
23
29
65
98
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No:5
Aim: Program to sort an array using Quick Sort technique
#include<stdio.h>
#include<conio.h>
#define max 100
int a[max],n,i,l,h;
void main()
{
void input(void);
input();
getch();
}
void input(void)
{
void quick_sort(int a[],int l,int h);
void output(int a[],int n);
printf("How many element in the array:\n");
scanf("%d",&n);
printf("\n");
printf("Enter the elements of array:\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
l=0;
h=n-1;
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
quick_sort(a,l,h);
printf("Sorted array is:\n");
output (a,n);
}
void quick_sort(int a[],int l,int h)
{
int temp,key,low,high;
low=l;
high=h;
key=a[(low+high)/2];
do
{
while(key>a[low])
{
low++;
}
while(key<a[high])
{
high--;
}
if(low<=high)
{
temp=a[low];
a[low++]=a[high];
a[high--]=temp;
}
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
}
while(low<=high);
if(i<high)
quick_sort(a,l,high);
if(low<h)
quick_sort(a,low,h);
}
void output(int a[],int n)
{
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Output:
How many element in the array:
5
Enter the elements of array:
99
101
302
234
324
Sorted array is:
99
101
234
302
324
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No:5
Aim: Program to sort an array using Selection Sort technique
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,temp,loc,min;
clrscr();
printf("\How many elements:\n");
scanf("%d",&n);
printf("Enter the element of array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
for(i=0;i<=n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
}
}
if(loc!=1)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
printf("The number after selection sort are:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();
}
Output:
How many elements:
8
Enter the element of array
12
87
34
91
23
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
54
23
76
The number after selection sort are:
12
23
23
34
54
76
87
91
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Program No:6
AIM: Program to find factorial of a Number
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("Enter the number/n");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("The factorial is %d",f);
getch();
}
Output:
Enter the number/n
6
The factorial is 720
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana