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
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
( Lectt. )
ECE 3rd Sem. Roll No:12ECEL26
NARNAUL (HARYANA)
CERTIFICATE
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 Engineering
(Session 2012-2015)
Submitted to : Submitted by:
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Mrs. Mamta Yadav Subhash Kumar Yadav ( Lectt. ) ECE 3rd Sem. Roll No:12ECEL26
DSA LAB
INDEX
Sr. No
Program Name Program to search an element in a two dimensional array using Linear Search Program perform Multiplication of two matrix Program perform Addition of two matrix Program to sort an array using Bubble Sort technique Program to sort an array using Quick Sort technique Program to sort an array using Selection Sort technique Program to find factorial of a Number
Pa ge Da No. te
Lec tt. Sig n
Rema rks
1-2 3-4 5-6 7-8 910 1112 13
2 3 4 5 6 7
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");
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
scanf("%d",&item); for(i=0;i<=n-1;i++) { if(item==a[i]) { loc=1; break; } } 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
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
96 Enter the number to be search 96 96is found in position2
Output: Enter no of element 5 Enter the number 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]);
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
} } 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++) { 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();
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
} Output: Enter the element of first matrix 12 23 34 45 56 78 89 90 91 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
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Result is 16643
Result is 12371 Result is 10080 Result is 12285
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++)
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
{ 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++) { for(j=0;j<=2;j++) { s[i][j]=a[i][j]+b[i][j]; printf("%d\t",s[i][j]); } printf("\n"); getch(); } }
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
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 97 49 96 95 53 33 Addition of two matrix is 33 153 184 68 116 144 110 174 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");
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
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; } }
} printf("Sorted array is\n"); for(i=0;i<=n-1;i++) printf("%d\n",a[i]); getch(); } Output:
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
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
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()
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
{ 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; quick_sort(a,l,h); printf("Sorted array is:\n"); output (a,n); } void quick_sort(int a[],int l,int h) {
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
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; } } while(low<=high); if(i<high) quick_sort(a,l,high); if(low<h)
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
quick_sort(a,low,h); } void output(int a[],int n) { for(i=0;i<=n-1;i++) { printf("%d\n",a[i]); } }
Output: How many element in the array: 5
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
Enter the elements of array: 99 101 302 234 324 Sorted array is: 99 101 234 302 324
Program No:5 Aim: Program to sort an array using Selection Sort technique #include<stdio.h>
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
#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 54
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana
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
Department of Electronics & Communication Engineering, YCET,Narnaul,Haryana