C Program to search for an element in an array – In this article, we will detail in on the various methods to search for an element in an array in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this specific piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
As we all know, an array is a collection or sequential arrangement of elements in any given order. Arrays form the basics of the C programming language.
As you can see in the photo uploaded above, the elements are first entered in no particular order.
The elements entered here are 4, 6, 2, 1 and 3.
The target element is mentioned here as well. The target element here is 2.
Once the element is present in the array, it will say “element found”.
Thus, the several methods to find an element in an array are as follows:
Using Standard Method
- Read the array size and store that value into the variable n.
2) Read the entered elements and store those elements into an array a[] using scanf statement and the for loop. scanf(“%d”,&a[i]) indicates scanf statement reads the entered element and assigned that element to a[i].
3) Read the key which we want to search in an array.
4) Compare the key with each element of the array as a[i]==key and print element found, if the key matches with any element of the array otherwise print element not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <conio.h> int main() { int a[10000],i,n,key; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("Enter the key : "); scanf("%d", &key); for(i=0; i<n; i++) { if(a[i]==key) { printf("element found "); return 0; } } printf("element not found"); } |
1 2 3 4 5 6 7 8 |
Enter size of the array: 5 Enter elements in array: 4 6 2 1 3 Enter the key: 2 element found |
Using Function – Search An Element In An Array
- The search() function compares each element of an array with the key which we want to search using if condition and the for loop.If any element matches with that key then this function returns 1, otherwise, it returns 0 to main() function at ‘if” condition.
2) The search() function will be called by passing an array, array size, key as arguments.
3) If the function search() returns 1 to if condition,then if(1) is true and prints “element found”.If it returns 0 then if(0) is false and prints “element not found”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <stdio.h> #include <conio.h> int search(int *a,int n,int key) { int i; for(i=0; i<n; i++) { if(a[i]==key) { return 1; } } return 0; } int main() { int a[10000],i,n,key; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("Enter the key : "); scanf("%d", &key); if(search(a,n,key)) printf("element found "); else printf("element not found "); } |
1 2 3 4 5 6 7 8 |
Enter size of the array: 5 Enter elements in array: 1 2 3 4 5 Enter the key: 2 element found |
Using Recursion – Search An Element In An Array
- In the main() search() function will be called by passing an array,0,array size,key as arguments in if condition.
2) The search() function checks the if condition i<n.If this condition is true then
a) It checks the a[i]==key,if key matches with a[i] then this function returns 1 to main() at if condition.
b) If key not matches with a[i] then it returns the same function by increasing i value as search(a,i+1,n,key) to main(). Again the search() will be called.Repeats until the key matches with any element of the array.
c) If the function returns 1 then main() prints “element found”.Otherwise, it prints “element not found”.
3) In this program search() function is recursive function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> #include <conio.h> int search(int *a,int i,int n,int key) { if(i<n) { if(a[i]==key) return 1; return search(a,i+1,n,key); } return 0; } int main() { int a[10000],i,n,key; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } printf("Enter the key : "); scanf("%d", &key); if(search(a,0,n,key)) printf("element found "); else printf("element not found "); } |
1 2 3 4 5 6 7 8 |
Enter size of the array: 5 Enter elements in array: 4 5 6 1 2 Enter the key : 0 element not found |