Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1) WAP to implement linear search
- #include <iostream>
- using namespace std;
- int main(){
- cout<<"Enter the size of the array: ";
- int n;
- cin>>n;
- int arr[n];
- cout<<"Enter the array elements: ";
- for(int i=0;i<n;i++) {
- cin>>arr[i];
- }
- cout<<"Enter the element to be searched: ";
- int element;
- cin>>element;
- int index=-1;
- for(int i=0;i<n;i++){
- if(arr[i]==element){
- index=i;
- break;
- }
- }
- if(index!=-1){
- cout<<"Element found at index: "<<index<<endl;
- }
- else cout<<"Element not found!!!"<<endl;
- return 0;
- }
- /*
- OUTPUT:
- Enter the size of the array: 5
- Enter the array elements: 8 9 1 2 19
- Enter the element to be searched: 2
- Element found at index: 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement