#include
#define MAX 50
using namespace std;
Funzioni principali
int Dimensionamento();
void CaricaVettore(int v[], int d);
void Ordina(int v[], int d);
void Scambia(int& a, int& b);
int Ricerca(int v[], int d);
int main ()
{
int d, v[MAX], trovato;
d=Dimensionamento();
CaricaVettore(v,d);
Ordina(v,d);
trovato= Ricerca(v,d);
if (trovato) {
cout
}
else {
cout
}
return 0;
}
int Dimensionamento(){
int d;
do{
cout
cin>>d;
} while (dMAX);
return d;
}
void CaricaVettore(int v[], int d){
cout
for (int i=0; i
cin>> v;
}
cout
}
void Ordina (int v[ ], int d){
cout
for (int i=0; i
for (int j=i+1; j
if ( v > v[j]){
Scambia (v, v[j]);
}
}cout
}
}
Scambio di elementi
void Scambia (int& a, int& b){
int c;
c=a;
a=b;
b=c;
}
int Ricerca (int v[], int d) {
int s=0, sx=0, dx=d-1, md, n;
cout
cin>>n;
do{
md=(sx+dx)/2;
if (v[sx]==n || v[dx]==n || v[md]==n) {
s=1;
}
else{
dx= md-1;
}
} while (sx
return s;
}
Domande da interrogazione
- What is the purpose of the `Dimensionamento` function in the code?
- How does the `Ordina` function organize the array elements?
The `Dimensionamento` function is designed to prompt the user to input the size of the array, ensuring it does not exceed the defined maximum size (`MAX`). It repeatedly asks for input until a valid size is entered.
The `Ordina` function sorts the array elements in ascending order using a simple selection sort algorithm. It iterates through the array, comparing elements and swapping them using the `Scambia` function if they are out of order.