#sorting problem and using to returns for a void funciton

10 messages · Page 1 of 1 (latest)

fast cobalt
#

Hello, this is my code, the problem is in the first function as it always prints me right the first return ( so 5 if its cofe and if its max age it gives me 65) but i want both while it only prints me either 5, 8 or 65,8 depending on which return i put first.
I guess the 8 is the lenght of the tab ?

Also i can't get the sorting function to swap me everything right as it doesn't switch the 29 with the 27 :(. I need your help thanks in advance.

Execution:
La consommation maximale de cafe: 5 et 8 <--- here i should get 5 et 65 but when i switch it gives me 65 and 8, never both good answers

Contenu de 3 tableaux avant tri :
Indice age nbCafe Poste
0) 19 1 Programmeur

  1.  24          4       Analyste
    
  2.  25          3       Programmeur
    
  3.  29          3       Programmeur
    
  4.  27          5       Operateur <---- here is the problem!!! 
    
  5.  30          0       Analyste
    
  6.  56          0       Operateur
    
  7.  65          3       Programmeur
    

Program is in the txt file.

odd phoenixBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

burnt herald
#

You can't return 2 variables from a single function, what i would suggest is to make a generic function to find the max value in an array an use this for both

#

like that

int findMax(int arr[], int nb) {
  int i;
  int max = arr[0];

  for (i = 1; i < nb; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}
  printf("La consommation maximale de cafe: %d et %d\n",
         findMax(nbCafe, nbPerso), findMax(age, nbPerso));

@fast cobalt

burnt herald
#

the problem i found with you sorting is that after you swap, you peek the minValue as it was before the swap, so let's say minValue is 2 and i is 1 you do the swap but minValue stays 2, even though the values went to index 1,

So what i did is put the swap after the inner for loop, so you only swap once each loop when you find the minimum,
i also added

void trier(char poste[], int nbCafe[], int age[], int nbPerso) {
  int temp1;
  char temp2;
  for (int i = 0; i < nbPerso - 1; i++) {
    int ageMin = i;
    for (int j = i + 1; j < nbPerso; j++) {
      if (age[ageMin] > age[j]) {
        ageMin = j;
      }
    }

    if (ageMin != i) {
      temp1 = age[i];
      age[i] = age[ageMin];
      age[ageMin] = temp1;

      temp1 = nbCafe[i];
      nbCafe[i] = nbCafe[ageMin];
      nbCafe[ageMin] = temp1;

      temp2 = poste[i];
      poste[i] = poste[ageMin];
      poste[ageMin] = temp2;
    }
  }
}

you can also keep what you had but you should add

        ageMin = i;

after the swap like that

void trier(char poste[], int nbCafe[], int age[], int nbPerso) {
  int i, j;
  int ageMin = i, tempo, tempoCafe;
  char tempoPoste;
  for (i = 0; i < nbPerso - 1; i++) {
    ageMin = i;
    for (j = i + 1; j < nbPerso; j++) {
      if (age[ageMin] > age[j]) {
        ageMin = j;
      }

      if (ageMin != i) {
        tempo = age[i];
        age[i] = age[ageMin];
        age[ageMin] = tempo;

        tempoCafe = nbCafe[i];
        nbCafe[i] = nbCafe[ageMin];
        nbCafe[ageMin] = tempoCafe;

        tempoPoste = poste[i];
        poste[i] = poste[ageMin];
        poste[ageMin] = tempoPoste;
        ageMin = i;
      }
    }
  }
}
fast cobalt
#

Thank you so much 🙏

fast cobalt
burnt herald
#

np!

odd phoenixBOT
#

@fast cobalt Has your question been resolved? If so, type !solved :)

fast cobalt
#

!solved