To determine whether or not a number is present in an array, you can use a linear search in C. If it is, the question becomes where exactly it is present. A sequential search is another name for this method. It is simple, and the way it operates is as follows: we check each element with the element to find until we either discover it or the list runs out. Searching in a linear fashion for numerous occurrences while utilizing a function
Linear search program in C
#include <stdio.h>int main(){ int array[100], search, c, n; printf(“Enter number of elements in arrayn”); scanf(“%d”, &n); printf(“Enter %d integer(s)n”, n); for (c = 0; c < n; c++) scanf(“%d”, &array[c]); printf(“Enter a number to searchn”); scanf(“%d”, &search); for (c = 0; c < n; c++) { if (array[c] == search) /* If required element is found */ { printf(“%d is present at location %d.n”, search, c+1); break; } } if (c == n) printf(“%d isn’t present in the array.n”, search); return 0;#} OutputEnter the number of elements6Enter 6 numbers734125Enter the number to search11 is present at position 4
Program that does a linear search in C for multiple occurrences
The following line of code will print out the total number of occurrences of the necessary element in the list, as well as all of the locations where it has been found.
#include <stdio.h>int main(){ int array[100], search, c, n, count = 0; printf(“Enter number of elements in arrayn”); scanf(“%d”, &n); printf(“Enter %d numbersn”, n); for (c = 0; c < n; c++) scanf(“%d”, &array[c]); printf(“Enter a number to searchn”); scanf(“%d”, &search); for (c = 0; c < n; c++) { if (array[c] == search) { printf(“%d is present at location %d.n”, search, c+1); count++; } } if (count == 0) printf(“%d isn’t present in the array.n”, search); else printf(“%d is present %d times in the array.n”, search, count); return 0;}
OutputEnter the number of elements8Enter 6 numbers73432536Enter the number to search33 is present at position 23 is present at position 43 is present at position 73 is present 3 times in an array
C program for linear search using a function
#include <stdio.h> long linear_search(long [], long, long); int main(){ long array[100], search, c, n, position; printf(“Input number of elements in arrayn”); scanf(“%ld”, &n); printf(“Input %d numbersn”, n); for (c = 0; c < n; c++) scanf(“%ld”, &array[c]); printf(“Input a number to searchn”); scanf(“%ld”, &search); position = linear_search(array, n, search); if (position == -1) printf(“%d isn’t present in the array.n”, search); else printf(“%d is present at location %d.n”, search, position+1); return 0;} long linear_search(long a[], long n, long find) { long c; for (c = 0 ;c < n ; c++ ) { if (a[c] == find) return c; } return -1;}
Linear search in C using pointers
long linear_search(long *p, long n, long find) { long c; for (c = 0; c < n; c++) { if (*(p+c) == find) return c; } return -1; }
Conclusion
The duration of time necessary to search for an element by utilizing the technique is reliant upon the volume of the list. The best-case scenario has it appearing at the start of the list, while the worst-case scenario has it appearing at the very end of the list. The degree of difficulty in terms of time is O(n).