logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Read an Array and Search for an Element

Posted on November 7, 2012 by Manish

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.

advertisement
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.

  1.  
  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.      */
  7.  
  8.     #include <stdio.h>
  9.     void main()
  10.     {
  11.  
  12.         int array[20];
  13.         int i, low, mid, high, key, size;
  14.  
  15.         printf("Enter the size of an array\n");
  16.         scanf("%d", &size);
  17.  
  18.         printf("Enter the array elements\n");
  19.         for (i = 0; i < size; i++) 
  20.         {
  21.             scanf("%d", &array[i]);
  22.         }
  23.  
  24.         printf("Enter the key\n");
  25.         scanf("%d", &key);
  26.  
  27.         /*  search begins */
  28.  
  29.         low = 0;
  30.         high = (size - 1);
  31.  
  32.         while (low <= high) 
  33.         {
  34.             mid = (low + high) / 2;
  35.  
  36.             if (key == array[mid]) 
  37.             {
  38.                 printf("SUCCESSFUL SEARCH\n");
  39.                 return;
  40.             }
  41.  
  42.             if (key < array[mid])
  43.                 high = mid - 1;
  44.  
  45.             else
  46.                 low = mid + 1;
  47.  
  48.         }
  49.  
  50.         printf("UNSUCCESSFUL SEARCH\n");
  51.  
  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.

advertisement
Runtime Test Cases
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 Reference Books in C Programming, Data Structures and Algorithms.

« Prev Page - C Program to Sort the Array in Descending Order
» Next Page - C Program to Implement a Stack

« C Program to Sort the Array in Descending Order
C Program to Implement a Stack »

Deep Dive @ Sanfoundry:

  1. Java Programming Examples on Combinatorial Problems & Algorithms
  2. C Programming Examples on Linked List
  3. Python Programming Examples on Linked Lists
  4. C Programming Examples on Combinatorial Problems & Algorithms
  5. C# Programming Examples on Sorting
  6. Python Programming Examples on Searching and Sorting
  7. C Programming Examples on Searching and Sorting
  8. Java Programming Examples on Arrays
  9. C Programming Examples on Arrays
  10. C# Programming Examples on Arrays
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.