how can i sort in decreasing order?

i did the sort is in increasing order

typedef struct{
        char name[30];
        CARd hand[3];
        CARd biggest_card;
        int nro_victories;
        }PLAYER;

PLAYER h;
int m, i, j, k;
for(i=0; i<n_playeres-1; i++)
              {
                  k = j;
                  m = h[i].nro_victories;
                  for(j=i+1; j<n_players; j++)
                  {
                      if(h[j].nro_victories < m)
                      {
                          m = h[j].nro_victories;
                          k = j;
                      }
                  }
              h[k].nro_victories = h[i].nro_victories;
              h[i].nro_victories = m;
              }

Recommended Answers

All 5 Replies

just change line 16 to use > instead of < comparison operators.

(if there is any undeclare variable just ignore.. it's because i forgot here, but it's all in my program)

when i try to print the data of the player, i have a problem with the number of victories
line 79: print number of victories
the first time of the loop it's correct, shows the number of victories(int)
but in the second until the end shows the address (i dont know of what) and dont know why

in line 91 i'm trying to print the players in decreasing order of victories
but how i print the names in the correct order too?

typedef enum{diamonds, spades, hearts, clubs}SUIT;

typedef struct{
        SUIT suit;
        int value;
        }CARD;

typedef struct{
        CARD *cards[52];
        int atual; /*indicates the next card to be withdrawal of the deck*/
        }DECK;

typedef struct{
        char name[30];
        CARD hand[3];
        CARD biggest_card;
        int num_victories;
        }PLAYER;

void initiated_player(PALYER *players, char *name, int i_player)
int Compare_Card(CARD *card, CARD *card2);
void Increase_Victories(PLAYER *players, int i_player)
PLAYER Recovery_Data_Player(PLAYER *players, int i_player);
void sort(PLAYER *players, int n_players);

main()
{
     int i, j, k, n_players, i_player, J;
     PLAYER *players, playersR;
     CARD *Biggest_card;
     .
     .
     .
     printf("Name of the players:\n\n");
     for(i=0; i<n_players; i++)
     {
          printf("player %d: ", i+1);
          scanf("%s", name);
          initiated_player(players, name, i);
     }
     .
     .
     .
// each player recive 3 cards
// this 3 cards are compare to see which card are the biggest (of the hand of the player)
// the biggest card of each player is compare to see which player have the biggest card
// then the number of victories of the player who has the biggest card are increaded
// like this...
    k=0;
    *Biggest_card = players[0].biggest_card;
    for(i_player=0; i_player<n_players; i_player++)
    {
         J = Compare_Card(Biggest_card, &(players[i_palyer].biggest_card));
         if(J == -1)
        {
           *Biggest_card = players[i_player].biggest_card;
             k = i_palyer;
        }
    }
                       
    printf("player %d:\n", k+1);
    printf("biggest card: %d of %d\n\n", Biggest_card->value, Biggest_card->suit);
                       
    Increase_Victories(players, k);
     .
     .
     .
     printf("results of the play 1: \n");

     for(i_player=0; i_player<n_players; i_player++)
     {
          playersR = Recovery_Data_Player(players, i_player);
          printf("players %d:\n\n", i_player+1);
          printf("name: %s\n\n", playersR.name);
          printf("hand:\n");
          for(k=0; k<3; k++)
               printf("card %d: %d of %d\n", k+1, playersR.hand[k].value, playersR.hand[k].suit);
          printf("biggest card: %d of %d \n\n", playersR.biggest_card.value, playersR.biggest_card.suit);
          printf("victories until now: %d\n\n", playersR.num_victories);
     }
     .
     .
     .
     printf("end of game!\n");

     sort(players, n_players);

     printf("classification:\n\n");
     
     for(i=0; i<n_players; i++)// print sort
          printf("player %s: %d\n\n", players.name, players[i].num_victories);
                            
     printf("player %s wins the game\n\n", players[0].name);
     .
     .
     .
}

/*this function put each player in the condition of started the game*/
void initiated_player(PLAYER *players, char *name, int i_player)
{
     strcpy(players[i_player].name, name);
     players->num_victories = 0;
}

/*This function compares card1 with card2 and returns 0 if the cards are equals,-1 if card1 < cadr2 and else 1. The comparison is done considerating the valueabsolute of the card. The bigger value is the biggest card. In case of tie (2 or most cardswith the same value), the suit must be verificate, considerating the follow order:Diamonds < Spades < Hearts < Clubs. Example: 10 of Diamonds < 10 of Spades < 10 of Hearts < 10 of Clubs            king of Diamonds >  10 of Clubs */
int Compare_Card(CARD *card, CARD *card2)
{
    PLAYER *players;
    if(card1->value == card2->value)
    {
        if(card1->suit < card2->suit)
            return -1;
        else
            return 1;
    }
    else
    {
        if(card1->value < card2->value)
            return -1;
        else
            return 1;
    }
}

/*this function increments the number of victories of the player i_player*/
void Increase_Victories(PLAYER *players, int i_player)
{
     players[i_player].num_victories++;    
}


/*this function recovery all data of the player i_player*/
PLAYER Recovery_Data_Player(PLAYER *players, int i_player)
{
        PLAYER data;
        data = players[i_player];
        return data;
}

/*selection sort - decreasing*/
void sort(PLAYER *players, int n_players)
{
     int i, j, k, victories;
     for(i=0; i<n_players-1; i++)
     {
         k = j;
         victories = players[i].num_victories;
         for(j=i+1; j<n_players; j++)
         {
             if(players[j].num_victories > victories)
             {
                victories = players[j].num_victories;
                k = j;
             }
         }
         players[k].num_victories = players[i].num_victories;
         players[i].num_victories = victories;
     }
}

somebody help me! pleeeease
i cant figure out why prints the address in number of victories
it only prints right in the first time of the loop

=S

You didn't post the program - you posted "something like it".

So it's not just a snippet of code which shows the problem, and it's also not the whole program.

I'm not going to waste time with it. Your quote:

(if there is any undeclare variable just ignore.. it's because i forgot here, but it's all in my program)

thanks, Adak
you're very polite

i didnt post my entire code because its gigant and its all in portuguese

you didnt notice that i post all about the error?!
if you dont wanna help, please dont disturb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.