This is a C Program to read an array and search for an element.
This program will implement a one-dimentional array, take a number form users to search for in the array using Binary Search.
1. Create an array of some certain size and define its elements in sorted fashion.
2. Now take an input from the users which you want to search for.
3. Take two variables pointing to the first and last index of the array (namely low and high)
4. Run start a while and run it until low equals high.
5. Now, take a mid of low and high value, check whether value at mid equals the user input.
6. In case it matches the user input, it means we found the number, after which we must break out from the loop.
7. And if user input is greater than value at mid, then low is assigned the value of mid. Similarly if user input is smaller than value at mid, then high is assigned the value of mid.
8. In this way, the region of finding the user input becomes half.
Here is source code of the C program to read an array and search for an element. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C program accept an array of N elements and a key to search.
* If the search is successful, it displays "SUCCESSFUL SEARCH".
* Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.
*/
#include <stdio.h>
void main()
{
int array[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", &array[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 == array[mid])
{
printf("SUCCESSFUL SEARCH\n");
return;
}
if (key < array[mid])
high = mid - 1;
else
low = mid + 1;
}
printf("UNSUCCESSFUL SEARCH\n");
}
1. Declare an array of capacity 20, taking size from users, define all the element of the array but in sorted fashion.
2. Now, take three variables, low pointing to the first index of array i.e 0, last index of array i.e size-1, and mid.
3. Run a loop till the low become equal to high.
4. Inside this loop, first calculate mid using (high + low) / 2 = mid.
5. Check if the value at mid matches the user input, if it does, that means we found the element and now we can return by breaking out from the loop.
6. If user input is greater than value at mid, then low is shifted to mid, similarly if user input is smaller than mid, then high id shifted to mid.
7. In this way, each time, we halved the region where we are finding the element.
Enter the size of an array 4 Enter the array elements 90 560 300 390 Enter the key 90 SUCCESSFUL SEARCH $ a.out Enter the size of an array 4 Enter the array elements 100 500 580 470 Enter the key 300 UNSUCCESSFUL SEARCH
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Practice BCA MCQs
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Check C Books