4

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

3
  • 1
    Note: in melhor_class(), all else uses can be deleted. Commented Dec 30, 2021 at 12:47
  • oh ok, so for instance if the function returns 1 in the first condition it doesn't run the other "ifs"? Commented Dec 30, 2021 at 17:08
  • It needs if()s. It does not need the else if()s. Commented Dec 30, 2021 at 17:09

3 Answers 3

2

The function trocar_equipas receives a pointer as an argument, so you can just pass it like this:

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);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

"t_equipa temp" must be changed to t_equipa* temp
1

The first argument expression of this call

trocar_equipas(&e,j,j+1);

is incorrect.

It has the type t_equipa ** while the function trocar_equipas expects an argument of the type t_equipa *.

void trocar_equipas(t_equipa *e, int p1, int p2)
{
    t_equipa temp = e[p1];
    e[p1] = e[p2];
    e[p2] = temp;

}

So call the function like

trocar_equipas(e,j,j+1);

Another approach is to declare and define the function trocar_equipas the following way

void trocar_equipas( t_equipa *e1, t_equipa *e2 )
{
    t_equipa temp = *e1;
    *e1 = *e2;
    *e2 = temp;
}

and call it like

trocar_equipas( &e[j], &e[j+1] );

or

trocar_equipas( e + j, e + j + 1 );

1 Comment

Thank you so much for your detailed explanation! I understand now!
1

Aside: a simplified compare function.

int melhor_class(const t_equipa *eqA, const t_equipa *eqB) {
  if (eqA->pontos != eqB->pontos) {
    return eqA->pontos > eqB->pontos;
  }
  if (eqA->golosM != eqB->golosM) {
    return eqA->golosM > eqB->golosM;
  }
  return eqA->golosS < eqB->golosS;
}

A more common design idiom would return 1 on greater, -1 on less and 0 on equal.

  if (eqA->pontos != eqB->pontos) {
    return eqA->pontos > eqB->pontos ? 1 : -1;
  }
  ...

Comments

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.