#include <stdio.h>
#include <string.h>
#define dim 5 //dimensione fisica dell'array
//struttura
typedef struct {
char nome[20];
char cogn[20];
int matr;
}studente;
//prototipi
void printarray(studente v[]);
void scanarray(studente v[]);
int compare(studente *, studente *);
// main
main(){
studente stud[dim];//array di tipo studente di 'dim' elementi
scanarray(stud);
printarray(stud);
qsort(stud,dim,sizeof(studente),compare);// ordina l'array
printarray(stud);//stampa array ordinato
system("PAUSE");
}//fine main
//procedura per stamapare gli elementi dell'array
void printarray(studente v[]){
int j;
for(j=0;j<dim;j++){
printf("%s\t%s\t%d\n\n",v
[j
].
cogn,v
[j
].
nome,v
[j
].
matr);
}
}
//procedura inserire elementi nell'array
void scanarray(studente v[]){
int i;
for(i=0;i<dim;i++){
system("cls");
scanf("%s",v[i].nome);
printf("\n\nInserire il cognome: ");
scanf("%s",v[i].cogn);
printf("\n\nInserire il matricola: ");
scanf("%d",&v[i].matr);
}
}
//funzione compare elementi della struttura
int compare(studente *e1, studente *e2){
if (strcmp(e1->cogn,e2->cogn)==0)
if (strcmp(e1->nome,e2->nome)==0)
if (e1->matr < e2->matr)
return -1;
else if (e1->matr>e2->matr)
return +1;
else return 0;
else return strcmp(e1->nome,e2->nome);
else return strcmp(e1->cogn,e2->cogn);
}