Вы находитесь на странице: 1из 10

10901124(C4)

COMPILER DESIGN

Submitted to:
Mrs.Navjot

Submitted By:
Bharti 10901124 (C4)

10901124(C4)

INDEX
S.No
1. 2.

Name of the program


WAP to implement linear and binary search using switch statement. WAP to find the number of white spaces and characters in a given string. WAP to find the no. of vowels in given string as well as the no. of times each vowel repeats. WAP to determine the factorial of a given number using macros. WAP to determine combination using macros.

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)

PRACTICAL 4 WAP to determine the factorial of a given number using macros.


#include<iostream.h> #include<conio.h> #define fact(b) for(int i=1;i<=b;i++)\ f=i*f long f=1; void main() { int x; clrscr(); cout<<"enter the no.\n"; cin>>x; fact(x); cout<<\n factorial=<<f; getch(); }

10901124(C4)

PRACTICAL 5 WAP to determine combination using macros.


#include<iostream.h> #include<conio.h> #include<math.h> #define comb(b,c) for(int i=1;i<=b;i++)\ f=i*f; for(int k=1;k<=c;k++) g=k*g; for(int l=1;l<=abs(b-c);l++) h=l*h; d=h*g; a=f/d; double b,c; double f=1; double g=1; double h=1; double a,d; void main() { clrscr(); int x,y; cout<<"enter the 2 no.s= "; cin>>x>>y; comb(x,y); cout<<a; getch(); }

Вам также может понравиться