Search Element in Array in C Language
Program Description:
Let’s write a program to Search Element in Array in C programming language. The Program should ask for the number to search and then search for the given number in the input array. Here is the example input and output of the program.
Excepted Input and Output:
Input:
1 2 3 |
Enter desired array size(1-100): 10 Please enter array elements(10) : 43 67 89 09 23 12 66 77 88 12 Enter a Number to search in Array: 23 |
Output:
FOUND - The 23 number Found at index 4
Prerequisites:
It is good to know the basics of the Arrays and Functions in C language. Please go through the following articles to learn more about these topics.
- Arrays in C
- Functions in C – How to Declare, Define, and call functions?
- How to pass Arrays to the Functions
Search Element in Array in C Program Explanation:
- Start the program by declaring the values array with the max size of 100. You can also use a Macro or Const global variable to hold the 100 value. But we are directly using here.
- Also declare the supporting variables targetNum, i, and size
- Take the desired array size from the user and update the size variable.
- Now, Prompt the user to provide the array elements. Use a For Loop to iterate over the array elements and update them.
- Use a variable i to traverse from i=0 to i=size-1.
- At each iteration, Prompt the user for input and update the values[i] element (Use scanf() function to read the elements)
- Take the targetNum from the user, The targetNum is the number to be searched in the values array.
- To search for the Number in Array
- Iterate from zero(0) to size-1 and compare the each element value with the targetNum value.
- if the values[i] == targetNum condition is true, Then we found the targetNum in the array.
- Display the FOUND message on the console and stop the program using the return statement
- If the above loop is completed successfully, Then we haven’t found the targetNum in the values array. So display the NOT FOUND message on the standard output.
📢 This Program uses Linear Search and the Time Complexity of the Program is Big O(n). Where n is the size of the array.
Algorithm of Search Element in Array in C:
- Start the program
- declare values[100] array and targetNum, i, and size variables.
- Take array size from the user
- Update the array elements with user-provided numbers
- Iterate from Zero(0) to size-1 using for loop
- update the values[i] element
- Prompt the user for number to search and store in targetNum
- Search for the
targetNum in the
values array.
- Traverse the array from Zero(0) to size-1 using for loop
- If the (values[i] == targetNum) is true, Then we found the targetNum. Display the FOUND message and terminate the program.
- If the loop is completed, Then the targetNum is NOT FOUND in the values array. Display the NOT FOUND message.
- stop the program
Program to Search Element in Array in C:
Here is the C Program to search for a number in the array.
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 |
/* Program to Search for an elemenet in array. sillycodes.com */ #include <stdio.h> int main() { // declare the 'values' array int values[100]; int targetNum, i, size; printf("Enter desired array size(1-100): "); scanf("%d", &size); // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("values[%d] : ", i); scanf("%d", &values[i]); } printf("Enter a Number to search in Array: "); scanf("%d", &targetNum); // find 'targetNum' in 'values' array for(i = 0; i < size; i++) { if(values[i] == targetNum) { printf("FOUND - The %d number Found at index %d\n", targetNum, i); return 0; } } // If we reached here, Then targetNum is not found in array. printf("NOT FOUND - The %d number Not Found in Array\n", targetNum); return 0; } |
Program Output:
Let’s compile and run the program. We are using the GCC compiler in the Linux operating system.
Compiling the C program using GCC.
gcc search-element.c
Run the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 43 67 89 09 23 12 66 77 88 12 Enter a Number to search in Array: 23 FOUND - The 23 number Found at index 4 $ ./a.out Enter desired array size(1-100): 5 Please enter array elements(5) : 10 20 30 40 50 Enter a Number to search in Array: 50 FOUND - The 50 number Found at index 4 $ ./a.out Enter desired array size(1-100): 4 Please enter array elements(4) : 88 22 33 44 Enter a Number to search in Array: 9 NOT FOUND - The 9 number Not Found in Array $ |
Related Array Programs:
- Index – C Language Practice Programs
- C Tutorials Index – Step by step tutorials to master C Langauge
- C Program to Read and Print Arrays
- Reverse Print Array Program in C
- C Program to calculate Sum of all array Elements
- C Program to Calculate the Average of all array Elements
- C Program to find minimum element in Array
- C Program to Find Maximum Element in Array
- C Program to Find Second Largest Element in Array
- C Program to Find Second Smallest Element in Array
- C Program to Copy array to Another Array