Browse Source

linear-search.cpp

pull/1339/head
suryakantdubalgunde 3 years ago
committed by GitHub
parent
commit
474398726f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      linear-search.cpp

33
linear-search.cpp

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
using namespace std;
int main()
{
int myarray[10] = {21,43,23,54,75,13,5,8,25,10};
int key,loc;
cout<<"The input array is"<<endl;
for(int i=0;i<10;i++){
cout<<myarray[i]<<" ";
}
cout<<endl;
cout<<"Enter the key to be searched : "; cin>>key;
for (int i = 0; i< 10; i++)
{
if(myarray[i] == key)
{
loc = i+1;
break;
}
else
loc = 0;
}
if(loc != 0)
{
cout<<"Key found at position "<<loc<<" in the array";
}
else
{
cout<<"Could not find given key in the array";
}
}
Loading…
Cancel
Save