-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst program.cpp
More file actions
43 lines (39 loc) · 837 Bytes
/
first program.cpp
File metadata and controls
43 lines (39 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
using namespace std;
int main(){
cout<<"Enter your number";
int key,n;
int list[20];
int linear_search(int[],int,int);
cout<<"enter the size of the array\n";
cin>>n;
for(int i=0;i<n;i++)
cin>>list[i];
cout<<"your array elements are\n";
for(int i=0;i<n;i++)
cout<<"["<<list[i]<<"] ";
cout<<endl;
cout<<"enter the key";
cin>>key;
int result=linear_search(list,key,n);
if(result==-1){
cout<<"the item is not found";
}
else
cout<<"the item is found in the index :"<<result<<" the value is "<<list[result];
return 0;
}
int linear_search(int list[],int key,int n){
int index=0;
int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}
while(found==0&&index<n);
if(found==0)
index=-1;
return index;
}