Untitled
unknown
plain_text
2 years ago
560 B
11
Indexable
//cau1
#include <iostream>
using namespace std;
int linear_search(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 4;
int result = linear_search(arr, n, x);
if (result == -1) {
cout << "Khong tim thay " << x << " trong mang." << endl;
} else {
cout << "Tim thay " << x << " tai vi tri " << result << " trong mang." << endl;
}
return 0;
}Editor is loading...
Leave a Comment