Linear Search using function
In this article we will dicuss the LInear Search Program in C with function. This article will be helpful to students for their academic purpose as they can easily understand how linear search program works with function.
As we will disucss Linear Search with function, so you should have an idea about Function.
Let's understand
Before moving directly to its definition let us know how does function arises?
When a program becomes large, it may be difficult to organise everything with a single main function.
Now if the program could be broken down into several seprate logical compartment based on the type of job carried out by each section of code, then it would have been easier to handle the program.The feature that C provides to do this type of coding is called function.
So coming to the definition
What is a function ?
A Function is a block of instruction that together can perform a certain specific task.
Moving to the code ->
Linear Search with function
This Program can also be done without using function click here for Linear Search Program without using Function
Output: Element Found
Output: Element not Found
Explanation of Code:
In Linear Search, we look for an element or value in an array by traversing it from the beginning to the end until the required element or value is found.
The array is searched sequentially by checking each element of the list until the required element is found. If the element is present in the list then it returns the location of that element.
In this C program, we have written a function called linear_search() that takes three input arguments and returns the position of an element in an array that the user is searching.
We traverse the array starting from the 0th index and increasing in order of index, if key element matches with the array index then we simply display "Element is found along with its index."
If the element is not present in the array then we will display "Element not found".
Explanation of Output:
In case of Element found
In fig 1 the user entered a list of elements in the array and giving the size of array 7.
Element which is needed to search is stored in a variable key.
Searchin of key elements starts by traversing the array sequentially each element one by one
Then key element is entered as 18. so we have to search whether 18 is present or not.
If key element matches with the array index then we simply display "Element 18 found along with its index."
As we can see 18 is present at array index 5. so we will display "Element 18 found at a[5].(a is the variable here)".
In case of Element not found
In fig 2 we have taken the same array to show the case when element is not found.
Just like the previous explanation Searching of key elements will starts by traversing the array sequentially each element one by one.
If key element matches with the array index then we simply display "Element 30 found along with its index."
Here in the entire list key element doesn't matches with the array index as 30 is not present in the array list, so we simply display "Element 30 not found."
Conclusion
So far our Explanation of Linear Search Program With function in C article comes to an end but before ending we just conclude few things.
Linear Search Algorithm Sequentially checks each element of the array until the key element is found or the entire list has been traversed.
Hence for this reason Linear Search is also knowns as sequential search.
If the element is present in the list then it returns the location of that element.
If the element is not present then we simply display "Element not found"
Main Banner Image Credit: peakpx.com