0

Currently i got this:

    struct employe{
    char nomE[25];
    char posteE;
    float nbHeureE;
    float tauxE;
} employes[16];
int nbPers = 0;

void readFile(){
    int i=0;

    FILE * entree;
    if(entree = fopen("employes.dat", "r"))
    {
        fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
        nbPers++;
        while(!feof(entree)) 
        {
            i++;
            fscanf(entree, "%24c %c %f %f", &employes[i].nomE, &employes[i].posteE, &employes[i].nbHeureE, &employes[i].tauxE);
            nbPers++;
        }
        fclose(entree);
    }
    else printf("Impossible d'ouvrir le fichier!\n");
}

void trier(struct employe employes[], int nbPers){
    int j,i,k;
    struct employe temp;
    for(i=0;i<16;i++)
    {
        for(j=1;j<15;j++)
        {
            if(strcmp(employes[i].nomE, employes[j].nomE) < 0)
            {
                temp = employes[i];
                employes[i] =employes[j];
                employes[j] = temp;
            }
        }
    }
}

int main() {
    int p=0;

    readFile();
    trier(employes, nbPers);
    for(p=0; p<nbPers; p++)
    printf("%s", employes[p].nomE);

    return 0;
}

employes.dat looks like this:

Tremblay Alain           A 35.0 35.5
Vachon Jean              P 40.0 22.75
Lapalme Justin           O 40.0 15.75
Deschenes Sylvie         P 35.0 25.0
Lachance Carl            O 37.5 18.0
Labonte Chantal          P 40.0 20.0
Doucet Michel            A 40.0 33.75
Desjardins Alex          P 35.0 25.0
Tardif Guy               A 40.0 28.5
Clinclin Stephane        O 40.0 20.75
Lafleur Marie            A 37.5 32.75
Desbiens Robert          P 35.0 25.0
Desautels Maryse         P 35.0 26.0
St-germain guy           O 37.5 15.0
Bourgeois Louis          A 37.5 29.0
St-amour Flavie          P 40.0 25.0

I'm trying to sort my employe structure in alphabetical order from the name (char nomE[25];). But, for some reason, it dosnt sort the first name and outputs this:

Tremblay Alain
Bourgeois Louis
Clinclin Stephane
Desautels Maryse
Desbiens Robert
Deschenes Sylvie
Desjardins Alex
Doucet Michel
Labonte Chantal
Lachance Carl
Lafleur Marie
Lapalme Justin
St-amour Flavie
St-germain guy
Tardif Guy
Vachon Jean

If someone have any idea why, I would REALLY appreciate the answer. Thank you in advance.

3
  • I think this link should help you stackoverflow.com/questions/28071593/… Commented Feb 6, 2016 at 5:59
  • I haven't tried your code but from what I remember about BubbleSort (gasp!) the inner loop should be something like for(j=i;j<16;j++) ... and when you get everything figured out, you might want to remove the hardcoded 16 (just saying). Cheers. Commented Feb 6, 2016 at 6:01
  • I tried what you suggested but it only reverse the alphabetic order and still skips the first name. Commented Feb 6, 2016 at 6:19

1 Answer 1

0

Change:

for(i=0;i<16;i++) {
    for(j=1;j<15;j++){ 

to:

for(i=0;i<16;i++) {
    for(j=0;j<15;j++){

EDIT: You have confused SelectionSort with BubbleSort. SelectionSort goes with i = 0 and j = i + 1 but also with i < 15 and j < 16, whereas BubbleSort is from i = 1 to i < 16 and j = 0 and j < 15. The above given version has the disadvantage that it does little more swaps than needed, but still produces the correct result at the end. See my comment for its implementation in ideone.com environment.

Because you skip to check the first element after the first iteration.

Sign up to request clarification or add additional context in comments.

3 Comments

It now goes at the end when its supposed to go between Tardif Guy and Vachon Jean...
No, I get it correct here. It may be because you run it up to the 15th element with <15 instead of <16 if there is no Vachon Jean at the end. Normally Tremblay Alain is the last but one when sorted.
There is no difference, you just access the elements (names) through each structure in the structure array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.