Skip to content
Prev Previous commit
Next Next commit
Add busca sequencial C++
  • Loading branch information
Poskvansk committed Feb 1, 2022
commit 126be6877607e6f78b7d9a4e58fc0770cdbcec13
36 changes: 36 additions & 0 deletions src/cpp/BuscaSequencial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <vector>

using namespace std;

int busca_sequencial(vector<int> &nums, int target) {

for (size_t i = 0; i < nums.size(); i++) {

if(nums[i] == target)
return i;
}

return -1;
}

int main() {

vector<int> nums = {1, 2, 3, 4, 5, 27, -1, 12, 999};
int target;

cout << "Digite o numero que deseja busca no vetor: ";
cin >> target;
cout << "\n";

int pos = busca_sequencial(nums, target);

if(pos > -1) {
cout << "Numero encontrado no vetor na posicao: " << pos << endl;
}
else {
cout << "Numero nao encontrado no vetor." << endl;
}

return 0;
}