School-Coding-Cpp/sfusi/RicercaDiUnValInUnArr.cpp

32 lines
713 B
C++

//Patriche Robert Cosmin
#include <iostream>
using namespace std;
int ricercaVettore(int arr[], int n, int valore) {
// Ricerca in entrambi i versi
for (int i = 0; i < n; i++) {
if (arr[i] == valore) return i;
}
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == valore) return i;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int valore = 30;
int indice = ricercaVettore(arr, n, valore);
if (indice != -1) {
cout << "Valore trovato all'indice: " << indice << endl;
} else {
cout << "Valore non trovato." << endl;
}
return 0;
}