3.program to implement sequential search:
#include<iostream>
using namespace std;
int main() {
int arr[10];
int no_of_elements; // Correct the variable name
int key;
bool found = false;
cout << "enter the number of elements:"; // Correct the prompt
cin >> no_of_elements;
for (int i = 0; i < no_of_elements; i++) {
cout << "arr[" << i << "]:";
cin >> arr[i];
}
cout << "enter the value to search:";
cin >> key;
for (int i = 0; i < no_of_elements; i++) {
if (key == arr[i]) {
found = true;
cout << "the value is found at index arr[" << i << "]";
break; // Exit the loop once the key is found
}
}
if (!found) {
cout << "key not found!";
}
return 0;
}
--------------------------------------------------
/tmp/nnypIrDI2Y.o
enter the number of elements:3
arr[0]:2
arr[1]:3
arr[2]:4
enter the value to search:4
the value is found at index arr[2]
No comments