Dsa Lab Practical File
Dsa Lab Practical File
( Lectt. )
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
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
Rema rks
2 3 4 5 6 7
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; } } 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 23 76 98 21 66 Enter the number to be search 11 item does not exist
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++) { 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 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
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++) { 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 97 49 96 95 53 33 Addition of two matrix is 33 153 184 68 116 144 110 174 56
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; } }
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()
{ 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) {
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)
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>
#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; } }
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:
#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(); }