Concetti Chiave
- The function `ricBin` performs a recursive binary search on an array of `quadro` structures, targeting the `nome` field.
- The `quadro` structure includes four fields: `nome`, `autore`, `prezzo`, and `data`, with `nome` being the primary key used for the search.
- The binary search begins with the `inizio` set to 0 and `fine` set to `numQuadri-1`, where `numQuadri` is the number of paintings.
- The function returns the index of the `quadro` if the `nome` matches `dipinto`; otherwise, it returns -1 if the item is not found.
- The call to `ricBin` is made with the array `pinacoteca`, containing up to `MAX_QUADRI` elements, searching for a `dipinto` by name.
Scrivi una funzione C/C++ che esegue in modo ricorsivo la ricerca binaria in un vettore di record. Scrivi l’istruzione di chiamata. Dichiara, definisci la struttura dati e tutte le variabili coinvolte nella chiamata (con eventuali inizializzazioni (di variabili) che ritieni utili nella comprensione della funzione).
#define MAX_QUADRI 20
struct quadro {
char nome[25]; //chiave primaria
char autore[30];
double prezzo;
char data[9];
};
struct quadro pinacoteca[MAX_QUADRI];
int inizio=0;
int fine=numQuadri-1;
char dipinto[25];
int posiz=ricBin(pinacoteca,dipinto,inizio,fine);
int ricBin(const struct quadro *galleria, const char *dipinto, int inizio, int fine)
{
if(inizio > fine)
return -1;
else
{
int centro;
centro = (inizio + fine)/2;
if(!strcmp(galleria[centro].nome,dipinto))
return centro;
else if(strcmp(galleria[centro].nome,dipinto) > 0)
return ricBin(galleria,dipinto,inizio,centro-1);
else
return ricBin(galleria,dipinto,centro+1,fine);
}
}