Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Standard view
Full view
of .
×
0 of .
Results for:
P. 1
Lab 14

# Lab 14

Ratings: 0|Views: 188|Likes:
Data Structures Program 14, VTU
Data Structures Program 14, VTU

### Availability:

See More
See less

11/12/2010

pdf

text

original

14.Write recursive C Program for a. Searching an element on a given list of integers using theBinary Search methos.b. Solving the Tower of Hanoi problemPROGRAM
#include<stdio.h>#include<conio.h>#include<process.h>int search(int,int [],int,int);void towers(int,char,char,char);static int count=0;int n1;void main(){int choice;int a[25],n,key,low,high,i,ans;clrscr();printf("Enter the choice:1.binary search 2.tower of hanoi\n");scanf("%d",&choice);switch(choice){case 1:printf("Enter the array size:\n");scanf("%d",&n);printf("Enter the array elements:\n");for(i=0;i<=(n-1);i++){scanf("%d",&a[i]);}printf("Enter the key to be searched:\n");scanf("%d",&key);ans=search(key,a,0,n-1);if(ans==1){printf("Key found\n");}else{printf("Key not found\n");}break;case 2:printf("enter n:\n");scanf("%d",&n1);towers(n1,'A','C','B');break;default:printf("Invalid choice\n");break;}}int search(int key,int a[],int low,int high){int mid;if(low<=high){

mid=(low+high)/2;if(key==a[mid]){return(1);}if(key<a[mid]){return(search(key,a,low,mid-1));}else{return(search(key,a,mid+1,high));}}return(-1);}void towers(int n1,char source,char dest,char aux){if(n1==1){printf("step%d:move%d from %c to %c\n",++count,n1,source,dest);}else{towers(n1-1,source,aux,dest);printf("step%d:move%d from %c to %c\n",++count,n1,source,dest);towers(n1-1,aux,dest,source);}}
OUTPUT
Enter the choice:1.binary search 2.tower of hanoi1Enter the array size:410203040Enter the key to be searched:25Key not foundEnter the choice:1.binary search 2.tower of hanoi1Enter the array size:41020

## Activity (1)

### Showing

AllMost RecentReviewsAll NotesLikes