Implement Sequential search algorithms in C++
Code
#include <iostream>
using namespace std;
int main(){
int n, x, i, pos;
int a[100];
cout << "Enter the number of elements in the array: ";
cin >> n;
cout << "Enter the elements of the array" << endl;
for (i = 0; i < n; i++){
cin >> a[i];
}
cout << endl;
cout << "Enter element to search: ";
cin >> x;
for(i = 0; i < n; i++){
if(a[i] == x){
pos = i;
break;
}
}
if(i == n){
cout << "Element not found" << endl;
}
else{
cout << "Element found at position " << pos + 1 << endl;
}
}
Output
0 Comments
Post a Comment