This program accepts an array of N elements and a key. Then it searches for the desired element. If the search is successful, it displays “SUCCESSFUL SEARCH”. Otherwise, a message “UNSUCCESSFUL SEARCH” is displayed.

by Nideesh C on April 13, 2011 · 0 comments

in C Programming




/* This program accepts an array of N elements and a key. Then it searches for the desired element. If the search is successful, it displays “SUCCESSFUL SEARCH”. Otherwise, a message “UNSUCCESSFUL SEARCH” is displayed. */

#include <stdio.h>
void main()
{
int table[20];
int i, low, mid, high, key, size;

printf(“Enter the size of an array\n”);
scanf(“%d”, &size);

printf(“Enter the array elements\n”);
for(i = 0; i < size; i++)
{
scanf(“%d”, &table[i]);
}

printf(“Enter the key\n”);
scanf(“%d”, &key);

/* search begins */

low = 0;
high = (size – 1);

while(low <= high)
{
mid = (low + high)/2;
if(key == table[mid])
{
printf(“SUCCESSFUL SEARCH\n”);
return;
}
if(key < table[mid])
high = mid – 1;
else
low = mid + 1;
}

printf(“UNSUCCESSFUL SEARCH\n”);
}                    /* End of main() */
/*———————————-
Output
Output
Enter the size of an array
5
Enter the array elements
12
36
45
78
99
Enter the key
45
SUCCESSFUL SEARCH
———————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept N numbers sorted in ascending order and to search for a given number using binary search. Report success or failure in the form of suitable messages
  2. C program to accept a list of data items and find the second largest and second smallest elements in it. And also computer the average of both. And search for the average value whether it is present in the array or not. Display appropriate message on successful search.
  3. C program to input N numbers (integers or reals) and store them in an array. Conduct a linear search for a given key number and report success or failure in the form of a suitable message
  4. C program to accept an array of 10 elements and swap 3rd element with 4th element using pointers. And display the results
  5. C program to insert a particular element in a specified position in a given array

Leave a Comment

Previous post:

Next post: