Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Esercizio 1
- Scrivere un metodo che prenda come parametri formali un array di interi A e restituisca in output un array di interi B in cui la posizione i-esima contiene l’indice della prima occorrenza di A[i] nel sottovettore A[i+1, ..., A.length –1 ], oppure –1 se questa non esiste.
- */
- /* IN QUESTO ESERCIZIO NON HO USATO METODI */
- #include <iostream>
- using namespace std;
- int main() {
- //creo un vettore
- int dim;
- cout << "Inserire dimensione array: ";
- cin >> dim;
- int a[dim];
- for (int i=0; i<dim; i++) {
- { cout << "Inserire Valore " << i+1 <<" di " << dim <<": ";
- cin >> a[i];
- }
- }
- // creo il vettore B
- int b[dim];
- for (int i=0; i<dim; i++) {
- bool trovato=false;
- for (int j=i+1; trovato==false && j<dim; j++ ) {
- if (a[i]==a[j]) {
- trovato = true;
- b[i]=j;}
- }
- if (trovato==false) b[i]=-1;
- }
- // mostro il vettore B
- cout << "B = {";
- for (int i=0; i<dim; i++)
- {
- if (i==dim-1) cout << b[i];
- else cout << b[i] << ",";
- }
- cout << "}";
- return 0;}
Advertisement
Add Comment
Please, Sign In to add comment