0

I can't sort this array in the right way and i don't understand why. I want to sort the array but keep the original index so that index can match with my index int the array with players and so I can put the name of the player there.

I get something like this:

before sorting 
_________________________________
Players ranked are David Beckham  ENG ---- 0.
Players ranked are Wayne Rooney  ENG ---- 5.
Players ranked are Pirlo  ITA ---- 3.
Players ranked are Del Piero  ITA ---- 2.
Players ranked are Lionel Messi  ARG ---- 5.

after sorting ( which is wrong )

Players ranked are David Beckham  ENG ---- 0.
Players ranked are Wayne Rooney  ENG ---- 0.
Players ranked are Pirlo  ITA ---- 5.
Players ranked are Del Piero  ITA ---- 3.
Players ranked are Lionel Messi  ARG ---- 2.

Can anyone help me with this ?

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements);
int LocationOfLargest(int array[], int n);
print_array (int array[], char name_and_country_code[][LENGTH_NAME], int elements);
void swap (int *a , int *b);

int main (void)
{
    int match1[PLAYERS] = { 0,1,3,2,4};
    int match2[PLAYERS] = { 0,4,0,0,1};
    int goals[PLAYERS] ;

    char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
    char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
    char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
    char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
    char name_and_country_code[PLAYERS][LENGTH_NAME];
    char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
    int i, first =1, second= 2;

    for(i=0; i < PLAYERS; i++)
    {
        strcpy (name_and_country_code[i], name[i]);
        strcat (name_and_country_code[i], "  " );
        strcat (name_and_country_code[i], country_abbreviations[i]);
        goals[i]= match1[i] + match2[i];
        printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
    }

    printf("\n_________________________________\n");
    //before sorting
    print_array ( goals, name_and_country_code, PLAYERS );
    printf("\n");
    sort_func ( goals, name_and_country_code, PLAYERS);
    //after sorting not working right
    print_array ( goals, name_and_country_code, PLAYERS );

    return 0;
}

print_array (int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int i ;
    for ( i = 0 ; i < PLAYERS; i ++)
    {
        printf ("Players ranked are %s ---- %d.\n", name_and_country_code[i], array[i]);
    }
}

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int index ,last = elements-1;
    while (last >0 )
    {
        index = LocationOfLargest(array, last);
        swap (&array[last], &array [index]);
        last--;
    }
}

void swap (int *a , int *b)
{
    int tmp = *a ;
    *a = *b;
    *b = tmp;
}

int LocationOfLargest(int array[], int n)
{
    int j , index =0 ;
    for (j = 0 ; j <= n ; j ++)
        if (array[index] < array[j])
            index = j;
    return j;
}
11
  • which sorting technique are you trying to use ? Commented Jun 26, 2013 at 5:24
  • @AurA: it's a selection sort. Commented Jun 26, 2013 at 5:25
  • @AurA the selection sort technique Commented Jun 26, 2013 at 5:26
  • 2
    Don't store data in different arrays! Put it into struct! Commented Jun 26, 2013 at 5:28
  • 1
    Is it your School Assignment?? Commented Jun 26, 2013 at 5:40

2 Answers 2

2

I believe what you want to do is sort one of your arrays and be able to index into it with the same index as the rest?

Why dont you store your myriad arrays as 1 array of the below struct and sort that alone?

typedef struct{
    int match1, match2, goals;
    char name[LENGTH_NAME];
    //and  so on

} Player;

...

Player players[PLAYERS]; 

//Now sort the array of players according to the goals.

This way you can sort by goals and still have the relevant data for the player together.

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

3 Comments

I am not sure what you are trying to say
@user2512806 the problem you are having is that when you sort the array of goals, your array of names goes out of sync right? i.e they don't match any more?
@user2512806 I have enhanced my example, please see if it helps.
0

In a quick scan, there are two errors that come to mind.


Let's look at LocationOfLargest().

int LocationOfLargest(int array[], int n)
{
    int j , index =0 ;
        for (j = 0 ; j <= n ; j ++)
            if (array[index] < array[j])
                index = j;
    return j;
}

Likely you meant for this to return index and not j.


Secondly, let's look at what you do to swap the data around when things are out of order.

void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements)
{
    int index ,last = elements-1;
    while (last >0 )
    {
        index = LocationOfLargest(array, last);
        swap (&array[last], &array [index]);
        last--;
    }
}

This function only swaps the order of the score. It does not also swap the strings that represent the names. You'll need to swap the contents of name_and_country_code as well.


And just for the sake of things, here's an example of a solution to this problem using struct as mentioned in the top level comments. It does clean things up quite a bit by making the associations more explicit.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>

#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20

static const int match1[PLAYERS] = {0,1,3,2,4};
static const int match2[PLAYERS] = {0,4,0,0,1};
static const char *name[PLAYERS] = {"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
static const char *countries[PLAYERS] = {"ENG","ENG","ITA","ITA","ARG"};

struct Player {
    char name[LENGTH_NAME];
    int score;
};

void swap(struct Player *a , struct Player *b) {
    struct Player tmp = *a;
    *a = *b;
    *b = tmp;
}

int LocationOfLargest(struct Player *array, int n) {
    int i, largest = 0 ;
    for (i = 0 ; i <= n ; ++i)
        if (array[largest].score < array[i].score)
            largest = i;
    return largest;
}

void sort_func(struct Player *array, int size) {
    int last = size - 1;
    while (last > 0) {
        int index = LocationOfLargest(array, last);
        swap(&array[last], &array[index]);
        last--;
    }
}

int main (void) {
    struct Player players[PLAYERS];
    int i;

    // Construct each player object
    for (i=0; i < PLAYERS; ++i) {
        snprintf(players[i].name, LENGTH_NAME, "%s   %s", name[i], countries[i]);
        players[i].score = match1[i] + match2[i];
    }

    // Print before sorting
    printf("Before sorting:\n");
    for (i=0; i < PLAYERS; ++i)
        printf("Player %s ---- score: %d\n", players[i].name, players[i].score);
    printf("\n");

    // Sort the data
    sort_func(players, PLAYERS);

    // Print after sorting
    printf("After sorting:\n");
    for (i=0; i < PLAYERS; ++i)
        printf("Player %s ---- score: %d\n", players[i].name, players[i].score);
    printf("\n");
}

6 Comments

it worked it sorted the values but it messed up the names ' Players ranked are David Beckham ENG ---- 0. Players ranked are Del e Rooney ENG ---- 2. Players ranked are Pirlo ITA ---- 3. Players ranked are LionPiero ITA ---- 5. Players ranked are Waynel Messi ARG ---- 5. '
@user2512806: You'll need to show me what you did to fix the second bug before I can tell you what remains wrong.
void sort_func(int array[], char name_and_country_code[][LENGTH_NAME], int elements) { int index ,last = elements-1; while (last >0 ) { index = LocationOfLargest(array, last); swap (&array[last], &array [index]); swap (&name_and_country_code[last],&name_and_country_code[index]); last--; } } void swap (int *a , int *b) { int tmp = *a ; *a = *b; *b = tmp; } int LocationOfLargest(int array[], int n) { int j , index =0 ; for (j = 0 ; j <= n ; j ++) if (array[index] < array[j]) index = j; return index;
@user2512806: swap(&name_and_country_code[last], &name_and_country_code[index]); is the correct idea. However, the call is not doing what you think it is. Your implementation of swap() works when it is passed 2 integers. You are passing it 2 strings. You'll need to create a different swap function that swaps 2 strings instead. I'd also like to note that your compiler should have reported a number of warnings (if not errors) about this. If it didn't you should increase the level of warnings being reported.
@user2512806: For example, with the change you proposed in your comments, I get the following warning from my compiler: foo.c:65:8: warning: incompatible pointer types passing 'char (*)[40]' to parameter of type 'int *' [-Wincompatible-pointer-types]
|

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.