Linear Search Algorithm
an implementation of the linear search algorithm.unknown
c_cpp
2 years ago
813 B
4
Indexable
Never
#include <iostream> using namespace std; void linearSearch(int arr[], int n, int length) { bool isFound = false; for(int i = 0; i < length; i++) { if(arr[i] == n) { cout << "Element was found at position" << " " << i + 1 << endl; isFound = true; } } if(isFound == false) { cout << "Couldn't find the element" << endl; } } int main() { int arrayLength = 2; int arr[arrayLength]; cout << "Enter 5 numbers" << endl; for(int i = 0; i < arrayLength; i++) { cin >> arr[i]; } int num; cout << "Kindly, Enter the number you wanna search for" << endl; cin >> num; linearSearch(arr, num, arrayLength); return 0; }