Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Stefano Berretti
Fondamenti di Informatica
SECONDA PROVA IN ITINERE – testo B – 10 gennaio 2020
COGNOME________________________ NOME________________________
Soluzione
Esercizio 1.
#define epsilon 0.00001
#define TRUE 1
#define FALSE 0
typedef unsigned short int Boolean;
struct record {
float value;
int next;
};
struct list {
struct record * buffer;
int free;
int first;
int size;
};
Esercizio 2.
Università degli Studi di Firenze, Scuola di Ingegneria – Corso di Laurea in Ingegneria Informatica – Prof. Stefano Berretti
struct list {
struct int * buffer;
int head;
int tail;
int free;
};
pivot = ptr->buffer[ptr->head];
l = ptr->head;
r = ptr->tail;
while( l != r) {
do{ r=(r-1+ptr>size)%ptr->size; } while( r != l && ptr->buffer[r] > pivot );
if( r != l ) {
do{ l=(l+1)%ptr->size; } while( r != l && ptr->buffer[l] <= pivot );
swap(ptr->buffer,l,r);
}
}
swap(ptr->buffer,l,ptr->head);
return l;
}
void swao(int * V, l, r) {
int tmp;
tmp = ptr->buffer[l];
ptr->buffer[l] = ptr->buffer[r];
ptr->buffer[r] = tmp;
}
Indicando con N il numero di elementi presenti nella lista, l’equazione di costo può essere scritta come:
c1 N N>0
partition(N) =
c2 N == 0
Esercizio 3.
struct list {
int value;
struct list * next_ptr;
};
N = 0;
tmp_ptr = ptr;
while( ptr != NULL ) {
N++;
ptr = ptr->next_ptr;
}
*V = (int *)malloc(sizeof(int)*(N-1));
*n_removed = 0;
ptr = tmp_ptr;
while( ptr != NULL && ptr->next_ptr != NULL ) {
if( ptr->value < ptr->next_ptr->value ) {
// complete the list with elements with values [value+1, value+diff-1]
diff = ptr->next_ptr->value - ptr->value;
for( count=1; count<diff; count++ ) {
pre_insert(&(ptr->next_ptr), ptr->value+count);
ptr = ptr->next_ptr;
}
ptr = ptr->next_ptr;
}
else {
// remove the next element
pre_remove(&(ptr->next_ptr),value);
V[(*n_removed)++] = value;
}
}
}
Indicando con N il numero di elementi della lista, l’equazione di costo può essere scritta come:
c1 + complete_list(N-1) N>0
complete_list(N) =
c2 N == 0