C Program to Search an Element in an Array

This is a C Program to read an array and search for an element.

Problem Description

This program will implement a one-dimentional array, take a number form users to search for in the array using Binary Search.

Problem Solution

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.

Program/Source Code

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.

  2.     /*
  3.      * C program accept an array of N elements and a key to search.
  4.      * If the search is successful, it displays "SUCCESSFUL SEARCH".
  5.      * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.
  6.      */
  8.     #include <stdio.h>
  9.     void main()
  10.     {
  12.         int array[20];
  13.         int i, low, mid, high, key, size;
  15.         printf("Enter the size of an array\n");
  16.         scanf("%d", &size);
  18.         printf("Enter the array elements\n");
  19.         for (i = 0; i < size; i++) 
  20.         {
  21.             scanf("%d", &array[i]);
  22.         }
  24.         printf("Enter the key\n");
  25.         scanf("%d", &key);
  27.         /*  search begins */
  29.         low = 0;
  30.         high = (size - 1);
  32.         while (low <= high) 
  33.         {
  34.             mid = (low + high) / 2;
  36.             if (key == array[mid]) 
  37.             {
  38.                 printf("SUCCESSFUL SEARCH\n");
  39.                 return;
  40.             }
  42.             if (key < array[mid])
  43.                 high = mid - 1;
  45.             else
  46.                 low = mid + 1;
  48.         }
  50.         printf("UNSUCCESSFUL SEARCH\n");
  52.     }
Program Explanation

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.

Runtime Test Cases
Enter the size of an array
Enter the array elements
Enter the key
$ a.out
Enter the size of an array
Enter the array elements
Enter the key

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.