Академический Документы
Профессиональный Документы
Культура Документы
COMPILER DESIGN
Submitted to:
Mrs.Navjot
Submitted By:
Bharti 10901124 (C4)
10901124(C4)
INDEX
S.No
1. 2.
Remarks
3.
4.
5.
6.
7.
10901124(C4)
PRACTICAL 1 WAP to implement linear and binary search using switch statement.
#include<iostream.h> #include<conio.h> #include<stlib.h> void main() { int a[60], i=0, j=0, p ,q, item, beg, end, mid, temp; clrscr(); cout<<Enter the number of elements in array\n; cin>>p; cout<<Enter th elements:\n; for(i=0;i<n;i++) cin>>a[i]; cout<<Enter the item to be found\n; cin>>item; cout<<Select the type of search:\n 1.Linear Search\n 2.Binary Search\n; cin>>q; switch(s) { Case1: //Linear Search for(i=0;i<n;i++) { if(a[i]==item) { cout<<position of element=<<i+1;
10901124(C4) getch(); exit(1); } } Case2: //Bubble Sort in order to arrange the elements for(i=0;i<n;i++) for(j=0;j<n-1;j++) if(a[j]>a[j+1]) { temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; } Case3: //Binary Search beg=1; end=p; mid=(beg + end)/2; while(item!=a[mid]&&(beg<=end)) { if(item<a[mid]) end=mid-1; else beg=mid+1; mid=(beg+end)/2; } if(item==a[mid])
10901124(C4) { cout<<position of the element=<<mid+1; break; } default: cout<<element not found; } getch(); }
10901124(C4)
10901124(C4)
PRACTICAL 2 WAP to find the number of white spaces and characters in a given string.
#include<iostream.h> #include<conio.h> #include<stdlib.h> void main() { char c; int a=0, ch=0; cout<<Enter the string\n; while(c!=\n) { c=getc(stdin); if(c== ) { a++; } else { ch++; } } cout<<No. of white spaces=<<a; cout<<\n Length of the string=<<ch; getch(); }
10901124(C4)
PRACTICAL 3 WAP to find the no. of vowels in given string as well as the no. of times each vowel repeats.
#include<iostream.h> #include<conio.h> void main() { char str[50]; int a=0, e=0, i=0, o=0, u=0; clrscr(); cout<<Enter the string\n; gets(str); for(j=0;str*j+!=\0;j++) { if (str*j+==a ||str*j+==A) a++; if (str*j+==e ||str*j+==E) e++; if (str*j+==i ||str*j+==I) i++; if (str*j+==o ||str*j+==O) o++; if (str*j+==u ||str*j+==U) u++; } cout<<No. of times each vowel repeats :\na=<<a<<, \ne=<<e<<, \ni=<<i<<, \no=<<o<<, \nu=<<u; getch(); }
10901124(C4)
10901124(C4)