C program to search an element in an array
Learn how to write a C program to search for an element in an array. This article provides a detailed explanation and sample code for searching an element using a simple linear search approach.
Searching for an element in an array is a fundamental operation in C programming, commonly used in various applications such as data retrieval, validation, and problem-solving. This article will guide you through writing a C program to search for an element in an array, providing a detailed explanation and sample code.
Steps to Search for an Element in an Array
To search for an element in an array, we can follow these steps:
- Input the Array: Read the array elements from the user.
- Input the Element to Search: Read the element to be searched by the user.
- Search the Array: Iterate through the array to find the element.
- Print the Result: Output the result indicating whether the element was found and its position.
Write a C program to search an element in an array
#include <stdio.h>
int main() {
int size, element, found = 0;
// Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the element to search
printf("\nEnter the element to search: ");
scanf("%d", &element);
// Search the array
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
found = 1;
printf("Element %d found at position %d", element, i + 1);
break;
}
}
if (!found) {
printf("Element %d not found in the array", element);
}
return 0;
}
Output
Enter the size of the array: 5
Enter 5 elements -
10
90
50
20
30
Enter the element to search: 50
Element 50 found at position 3
Explanation
- Input the Array: The user inputs the array size and its elements.
- Input the Element to Search: The user inputs the element they want to search for in the array.
- Search the Array: The program uses a loop to iterate through the array. If the element is found, a message indicates the element's position and the loop is terminated.
- Print the Result: If the element is found, its position is printed. If the element is not found after the loop completes, a message indicating that the element is not found is printed.
Detailed Steps
- Step 1: Input the Array
- The program prompts the user to enter the size of the array.
- The user inputs the elements of the array.
- Step 2: Input the Element to Search
- The program prompts the user to enter the element to be searched in the array.
- Step 3: Search the Array
- A loop runs from the start to the end of the array.
- During each iteration of the loop, the current element is compared with the search element.
- If a match is found, the position of the element is printed, and the loop is terminated.
- Step 4: Print the Result
- If the element is found, its position in the array is printed (position starts from 1).
- If the element is not found after the loop completes, a message indicating that the element is not found is printed.
Advantages of This Approach
- Simplicity: The method is straightforward to implement.
- Efficiency: The search process has a linear time complexity of O(n), where n is the number of elements in the array.
Disadvantages of This Approach
- Efficiency for Large Arrays: For very large arrays, the linear search may not be the most efficient approach. In such cases, more advanced searching algorithms like binary search (for sorted arrays) might be preferred.