-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.0 (C++) Implement linear search by using 1D Array.cpp
More file actions
63 lines (48 loc) · 1.3 KB
/
16.0 (C++) Implement linear search by using 1D Array.cpp
File metadata and controls
63 lines (48 loc) · 1.3 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// Implement linear search by using 1D Array
/*
let, int num[] = { 10, 2, 30, 15, 28, 5 }
Search the value = 15
1. Is the item found
2. If yes then what's the position?
*/
#include <iostream>
using namespace std;
int main()
{
int num[100];
int n, i, value, position;
cout<<" Enter the total number of values: ";
cin>>n;
// Insert the values (to array)
cout<<" Enter the "<<n<<" values: ";
for(i=0; i<n; i++){
cin>>num[i];
}
position=-1;
cout<<" Enter the value you want to search: ";
cin>>value;
// Search the values from array
for(i=0; i<n; i++){
if(value==num[i]){
position=i+1;
break;
}
}
// Showing result from calculation
cout<<"\n";
if(position==-1){
cout<<" Item isn't found";
}
else{
cout<<" The value is found at position: "<<position<<endl;
}
return 0;
}
/* ===== Output / Result:
Input:
Enter the total number of values: 5
Enter the 5 values: 10 12 25 13 29
Enter the value you want to search: 13
Output:
The value is found at position: 4
*/