vuoi
o PayPal
tutte le volte che vuoi
Formattazione del testo
struct list{double value;struct list * nextPtr;};
void createLists(struct list * srcPtr, struct list ** dest1PtrPtr,struct list ** dest2PtrPtr) {init(&dest1PtrPtr);init(&dest2PtrPtr);while(srcPtr!=NULL) {if (srcPtr >value > T) {pre_insert(dest1PtrPtr, srcPtr >value);dest1Ptr = &((*dest1Ptr) >nextPtr);} else {pre_insert(dest2PtrPtr, srcPtr >value);dest2Ptr = &((*dest2Ptr) >nextPtr);}srcPtr = srcPtr >nextPtr;}}
Si definisca una funzione c che riceve in ingresso due array di int e verifica se tali arraysono uguali (due array sono uguali se contengono lo stesso numero di elementi, con glistessi valori e nello stesso ordine).
typedef unsigned short int Boolean;
#define TRUE 1
#define FALSE 0
Boolean is_equal(int * U, int * V, int sizeU, int sizeV) {int count=0;if (sizeU != sizeV)return FALSE;while(count < sizeU) {if(U[count] != V[count])return FALSE;elsecount++;}return TRUE;}
Si definisca la struttura c che rappresenta un elemento di una lista di
int in formacollegata con puntatori. Si definisca una funzione c che riceve in ingresso una tale lista eè minore del valore che lo segue nella lista
stampa a video ciascun elemento se e solo se(l'ultimo elemento della lista non ha un sucessore e non viene mai stampato). Se la lista èvuota o contiene un solo elemento, la funzione non stampa a video alcun valore.
Soluzione:
<pre>
<code>
struct list{
int value;
struct list* next_ptr;
};
void visit (struct list* ptr) {
while (ptr!=NULL && ptr->next_ptr!=NULL) {
if (ptr->value < ptr->next_ptr->value)
printf("%d\n", ptr->value);
ptr = ptr->next_ptr;
}
}
</code>
</pre>
Si definisca una funzione c che riceve in ingresso un array di int e stampa a video il
elementi uguali al minimo elemento dell'array e il suo indice (se l'array contiene più minimo, la funzione stampa a video l'indice del primo elemento uguale al valore minimo).
void printMinIndex(int * V, int size) {
int count=0;
int minIndex = 0;
for(count=1; count<size; count++) {
if(V[count] < V[minIndex])
minIndex = count;
}
for(count=0; count<size; count++) {
if(V[count] == V[minIndex])
printf("Elemento: %d, Indice: %d\n", V[count], count);
}
}
```html
count<size; count++) {
if(V[count] < V[minIndex])
minIndex = count;
}
printf("Minimo elemento: V[%d]=%d", minIndex, V[minIndex]);
}
Si definisca una funzione c che riceve in ingresso due matrici X e Y di valori float di
dimensione AxB e BxC, rispettivamente, alloca e costruisce la matrice prodotto Z di
dimensione AxC, e restituisce il puntatore alla prima locazione della matrice prodotto.
Si assuma che le tre matrici siano rappresentate su array monodimensionali.
float * product(float* X, float* Y, int A, int B, int C){
int a,b,c;
float * zPtr;
zPtr = (float*)malloc(A*C*sizeof(float));
for(a=0; a<A; a++)
for(c=0; c<C; c++)
for(b=0,zPtr[a*C+c]=0; b<B; b++)
zPtr[a*C+c]+ = X[a*B+b]*Y[b*C+c];
return zPtr;
}
Si definisca la struttura c che rappresenta un elemento di una lista di float in forma
collegata con puntatori. Si definiscano due funzioni c che calcolano quanti elementi di una
tale lista sono minori di un valore assegnato, in forma iterativa e in forma
```<struct list>
<unsigned int value>
<struct list * next_ptr>
</struct list>
<int visit_i(struct list* ptr, float target)>
<int count>
count=0;
<while(ptr!=NULL)>
<if (ptr >value < target)>
count++;
ptr=ptr >next_ptr;
</while>
return count;
</int visit_i>
<int visit_r(struct list* ptr, float target)>
<if(ptr!=NULL)>
<if (ptr >value < target)>
return 1 + visit_r(ptr >next_ptr, target);
<else>
return visit_r(ptr >next_ptr, target);
<else>
return 0;
</int visit_r>
<struct list>
<unsigned int value>
<struct list * next_ptr>
</struct list>
<struct list * createList(unsigned int N)>
<struct list * headPtr>
<int count>
init(&headPtr);
<for>
(count=N; count>=1; count )pre_insert(&headPtr, count*count);
return headPtr;
}
Si definiscano le strutture dati che rappresentano una lista di interi in forma collegata con array e indici. Si definisca una funzione c che riceve in ingresso una lista in forma collegata con array e indici e restituisce l’elemento massimo. Si assuma che la lista contenga almeno un elemento.
<pre>
<code>
struct record {
int value;
int next;
};
struct list {
int first, free, size;
struct record* buffer;
};
</code>
</pre>
int max(struct list* ptr) {
int max, position;
max = ptr->buffer[ptr->first].value;
position = ptr->buffer[ptr->first].next;
while(position != ptr->size) {
if (max < ptr->buffer[position].value)
max = ptr->buffer[position].value;
position = ptr->buffer[position].next;
}
return max;
}
Si definisca la struttura c che rappresenta un elemento di una lista di int in forma collegata con puntatori. Si definisca una funzione c che riceve in ingresso un array di int e crea la lista in forma collegata con puntatori.
<pre>
<code>
struct node {
int data;
struct node* next;
};
</code>
</pre>
void createLinkedList(int arr[], int size, struct node** head) {
struct node* current = NULL;
struct node* temp = NULL;
for (int i = 0; i < size; i++) {
temp = (struct node*)malloc(sizeof(struct node));
temp->data = arr[i];
temp->next = NULL;
if (*head == NULL) {
*head = temp;
current = temp;
} else {
current->next = temp;
current = current->next;
}
}
}
struct list { int value; struct list *next_ptr; }; struct list *arrayToList(int *V, int size) { struct list *headPtr; int count; init(&headPtr); for (count = 0; count < size; count++) preInsert(&headPtr, V[count] * V[count]); return headPtr; } void init(struct list **ptrptr) { *ptrptr = NULL; } void preInsert(struct list **ptrptr, float value) { struct list *tmp_ptr; tmp_ptr = *ptrptr; *ptrptr = (struct list *)malloc(sizeof(struct list)); (*ptrptr)->value = value; (*ptrptr)->next_ptr = tmp_ptr; }Esercizio 4 (9 punti) TRUE e FALSE Si definiscano il tipo di dato Boolean (tramite typedef) e i valori di verità (tramite #define). Si definisca la struttura dati che rappresenta una lista di interi in forma collegata con array e indici. Si definisca la funzione c che realizza l'inserimento in testa su una tale lista (la funzione restituisce TRUE se la
typedef struct node { float value; struct node* next; } Node; Node* createLinkedList(float* array1, float* array2, int length) { Node* head = NULL; Node* current = NULL; for (int i = 0; i < length; i++) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->value = array1[i] + array2[i]; newNode->next = NULL; if (head == NULL) { head = newNode; current = newNode; } else { current->next = newNode; current = newNode; } } return head; }
l’implementazione delle funzioni elementari distruct list{float value;struct list * next_ptr;};struct list * arrayToList(float * U, float * V, int size) {struct list * headPtr;int count;init(&headPtr);for (count=size 1; count>=0; count++)preInsert(&headPtr, U[count]+V[count]);return headPtr;}void init(struct list** ptrptr){*ptrptr = NULL;}void preInsert(struct list** ptrptr, float value){struct list* tmp_ptr;tmp_ptr=*ptrptr;*ptrptr=(struct list*)malloc(sizeof(struct list));(*ptrptr) >value=value;(*ptrptr) >next_ptr=tmp_ptr;} TRUE e FALSESi definiscano il tipo di dato Boolean (tramite typedef) e i valori di verità(tramite #define). Si definisca la struttura dati che rappresenta una lista di interi in formasequenziale. Si definisca la funzione c che realizza l'inserimento in testa su una tale lista(la funzione restituisce TRUE se la lista non è piena e FALSE in caso contrario).typedef unsigned short int Boolean;#define TRUE 1#define FALSE
struct list { int * buffer; int size; int head; int tail; }; Boolean preInsert(struct list* ptr, int value) { if ((ptr->tail + 1) % ptr->size != ptr->head) { ptr->head = (ptr->head + ptr->size - 1) % ptr->size; ptr->buffer[ptr->head] = value; return TRUE; } else { return FALSE; } } struct list { int value; struct list * next_ptr; }; struct list * arrayToList(int * V, int size) { struct list * headPtr; int count; init(&headPtr); for (count = 0; count < size; count++) { preInsert(&headPtr, V[count] * V[count]); } return headPtr; } void init(struct list** ptrptr) { *ptrptr = NULL; } void preInsert(struct list** ptrptr, int value) { struct list * newNode = (struct list*) malloc(sizeof(struct list)); newNode->value = value; newNode->next_ptr = *ptrptr; *ptrptr = newNode; }
float value) {
struct list* tmp_ptr;
tmp_ptr = *ptrptr;
*ptrptr = (struct list*)malloc(sizeof(struct list));
(*ptrptr)->value = value;
(*ptrptr)->next_ptr = tmp_ptr;
}
typedef unsigned short int Boolean;
#define TRUE 1
#define FALSE 0
struct record {
int value;
int next;
};
struct list {
int first, free, size;
struct record* buffer;
};
Boolean preInsert(struct list* ptr, int value) {
int moved;
if(ptr->free != ptr->size) {
moved = ptr->free;
ptr->free = (ptr->buffer)[ptr->free].next;
ptr->buffer[moved].value = value;
ptr->buffer[moved].next = ptr->first;
ptr->first = moved;
return TRUE;
}
else {
return FALSE;
}
}