I need to read a file (and store the information in arrays) containing the student number, name and first name and grade of different students, then display the all this information and then use quicksort to display the this information now sorted from highest to lowest grades. The output displays only the information before the quicksort. Anyone knows why my quicksort is wrong? Disclaimer : some words in my code are in french.
Here is a sample of the file :
1000 Docteur Albert 65.5
1001 Solo Hanz 23.4
1002 Caillou Frederic 78.7
1003 Viky Bryan 98.6
1004 Encas Christian 67.7
Here is my code :
#include <stdio.h>
#include <ctype.h>
#define MAX_PERS 200
#define MAX_LONG_NP 15
int numero[MAX_PERS];
char nom[MAX_PERS][MAX_LONG_NP];
char prenom[MAX_PERS][MAX_LONG_NP];
float note[MAX_PERS];
void afficher(float note[], int nbPers, char *quand) /* a effacer lorsque tout fonctionnera bien */
{
int i;
printf("Contenu du tableau %s le tri\n\n", quand);
for(i=0;i<nbPers;i++)
printf("%3.2f\n", note[i]);
}
void echanger (float *P1, float *P2)
{
float tempo;
tempo = *P1 ;
*P1 = *P2;
*P2 = tempo;
}
void partitionner ( float note[], int debut, int fin, int *P )
{
int G = debut , D = fin ;
float Val_Pivot = note[debut];
while ( G <= D && note[G] >= Val_Pivot)
G++;
while (note[D] < Val_Pivot)
D--;
if ( G < D )
echanger(¬e[G], ¬e[D]);
while ( G <= D )
echanger (¬e[debut], ¬e[D]);
*P = D ;
}
void quickSort ( float note[], int gauche, int droite )
{
int indPivot ;
if (gauche < droite)
{
partitionner ( note, gauche, droite, &indPivot);
quickSort ( note, gauche, indPivot - 1 );
quickSort ( note, indPivot + 1, droite);
}
}
int main()
{
FILE *aLire = fopen("notes.txt", "r");
int nbPers=0;
while(!feof(aLire))
{
fscanf(aLire, "%d%s%s%f\n", &numero[nbPers], &nom[nbPers], &prenom[nbPers], ¬e[nbPers]);
nbPers++;
}
fclose(aLire);
afficher(note, nbPers, "avant");
quickSort(note, 0, nbPers-1);
afficher(note, nbPers, "apres");
return 0;
}