To compile and execute the program, copy the code into a text file. Save the text file with the extinction .c (ex. program.c). Save your file in turboc2 or borland (it may your othe c-compiler). Now open you IDE (tc.exe) , goto file->pick file. Your source file will be loaded into IDE. Now you can compile and run the file easily.
#include <stdio.h>
#define TRUE 0
#define FALSE 1
int main(void) {
int array[100],n,i;
clrscr();
gotoxy(31,1);
printf("Binary Search");
gotoxy(26,3);
printf("http:\\techaravind.blogsopt.com");
gotoxy(1,5);
printf("Enter the size of an array");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0;i<=n-1;i++)
{
scanf("%d",&array[i]);
}
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;
printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
printf("\nSearch for Number: ");
scanf("%d", &number);
while(bsearch == FALSE && left <= right) {
middle = (left + right) / 2;
if(number == array[middle]) {
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}
if(bsearch == FALSE)
printf("-- Number Not found --\n");
return 0;
}
0 comments:
Post a Comment