-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
52 lines (44 loc) · 1012 Bytes
/
search.cpp
File metadata and controls
52 lines (44 loc) · 1012 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
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
const int MAX_SIZE = 20;
int myData[MAX_SIZE];
int idx[MAX_SIZE];
int count = 0;
void search(int x, int n);
int main(){
int n;
cout << "Jumlah dta maksimal " << MAX_SIZE << ":";
cin >> n;
//validate input size
if(n <= 0 || n > MAX_SIZE){
cout << "Jumlah data tidak valid\n";
return 1;
}
for(int i = 0; i < n; i++){
cout << "Data ke-"<< i << ":";
cin >> myData[i];
}
int x;
cout << "Cari:";
cin >> x;
search(x, n);
if(cout > 0){
cout << "Ditemukan pada index: ";
for(int i = 0; i < cout; i++){
cout << idx[i];
if(i < cout-1) cout << ", ";
}
cout << endl;
}
else{
cout << "Data tidak ditemukan\n";
}
return 0;
}
void search(int x, int n){
for(int i = 0; i < n; i++){
if(x == myData[i]){
idx[count++] = i;
}
}
}