I have this code to order a group of teams based on their scores, just like a soccer ranking and the code works fine when implemented like this (btw I defined "NEQS" to 18):
int melhor_class(t_equipa *eqA, t_equipa *eqB)
{
if(eqA->pontos > eqB->pontos){
return 1;
} else if(eqA->pontos < eqB->pontos){
return 0;
} else if(eqA->golosM > eqB->golosM){
return 1;
} else if(eqA->golosM < eqB->golosM){
return 0;
} else if(eqA->golosS < eqB->golosS){
return 1;
} else if(eqA->golosS > eqB->golosS){
return 0;
} else {
return 1;
}
}
void ordenar_equipas(t_equipa *e)
{
for(int i = 0; i < NEQS - 1; i++)
{
for(int j = 0; j < NEQS - i - 1; j++)
{
if (melhor_class(&e[j],&e[j+1]) == 0)
{
//swaping part
t_equipa temp = e[j];
e[j] = e[j+1];
e[j+1] = temp;
}
}
}
}
And the output works fine, its sorted out:
P V E D M S
Gil Vicente 33 0 0 0 18 7
Benfica 32 0 0 0 10 10
Sporting 31 0 0 0 10 7
Porto 24 0 0 0 20 8
Arouca 0 0 0 0 0 0
Belenenses 0 0 0 0 0 0
Boavista 0 0 0 0 0 0
Braga 0 0 0 0 0 0
Estoril 0 0 0 0 0 0
Famalicao 0 0 0 0 0 0
Maritimo 0 0 0 0 0 0
Moreirense 0 0 0 0 0 0
Pacos Ferreira 0 0 0 0 0 0
Portimonense 0 0 0 0 0 0
Santa Clara 0 0 0 0 0 0
Tondela 0 0 0 0 0 0
Vitoria 0 0 0 0 0 0
Vizela 0 0 0 0 0 0
But when I put the swaping part of the code in an function it just doesn't work:
void trocar_equipas(t_equipa *e, int p1, int p2)
{
t_equipa temp = e[p1];
e[p1] = e[p2];
e[p2] = temp;
}
void ordenar_equipas(t_equipa *e)
{
for(int i = 0; i < NEQS - 1; i++)
{
for(int j = 0; j < NEQS - i - 1; j++)
{
if (melhor_class(&e[j],&e[j+1]) == 0)
{
trocar_equipas(&e,j,j+1);
}
}
}
}
Output:
P V E D M S
Arouca 0 0 0 0 0 0
Belenenses 0 0 0 0 0 0
Benfica 32 0 0 0 10 10
Boavista 0 0 0 0 0 0
Braga 0 0 0 0 0 0
Estoril 0 0 0 0 0 0
Famalicao 0 0 0 0 0 0
Gil Vicente 33 0 0 0 18 7
Maritimo 0 0 0 0 0 0
Moreirense 0 0 0 0 0 0
Pacos Ferreira 0 0 0 0 0 0
Porto 24 0 0 0 20 8
Portimonense 0 0 0 0 0 0
Santa Clara 0 0 0 0 0 0
Sporting 31 0 0 0 10 7
Tondela 0 0 0 0 0 0
Vitoria 0 0 0 0 0 0
Vizela 0 0 0 0 0 0
I really need to put that swaping part in another function! I appreciate any type of help! Thanks