-3

I have a C struct declared as below (just a simple example):

typedef struct
{
 float score;
 char* name;
}
person;

So, along the program, I changed the value of the person's score (i gave them an initial value). So, i plan to put all of the scores inside a vector and sort them. Then, I would like to print a list of the name of all the persons, starting from biggest score to smallest score. Any idea on this last part? I'm not sure how to code it. Thanks.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
embedded_dev
  • 195
  • 9
  • 1
    Are you talking about a c++ vector or something else? – Retired Ninja Jan 13 '17 at 03:02
  • Not clear what you are asking. If you have a sorted vector then just traverse that vector and print the name from each element. What's the exact problem? – kaylum Jan 13 '17 at 03:03
  • No. It's a C vector. What i'm asking is this: I've got the person's scores, for example: Person 1, score: 210 Person 2, score: 321 Person 3, score: 124 So, what i tought was: Create a vector with the scores "person[i].score", i going from 0 to 2. Sort the vector. Print the names in order. The problem is that i don't know how to print the names in order, since i only sorted the scores. I don't understand why my question got downvoted. Anyone can explain? – embedded_dev Jan 13 '17 at 03:08
  • If you sort the array of structs correctly then you simply need to iterate through the array and print each name. If you have code that isn't working you should edit that code into your question, but the linked question and answer should get you started. – Retired Ninja Jan 13 '17 at 03:11
  • Downvoted because your question is unclear. You wrote "starting from biggest score to smallest score". That contradicts "how to print the names in order, since i only sorted the scores". The former says to print in score order whereas the latter implies you want name order. So which is it? – kaylum Jan 13 '17 at 03:17
  • Since a C++ answer (which is completely invalid in C) is accepted, this question should be tagged C++, not C. – Jonathan Leffler Jan 13 '17 at 03:48

2 Answers2

1

Similar to the C++ approach above, there is a library function in the C standard library called qsort. It is based upon a comparision function, that creates the ordering on the array members.

For your case a minimal example could look like this:

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

// Definitiion of a person
typedef struct person
{
    float score;
    char *name;
} person;

// Some small epsilon
#define EPSILON 1.0e-3f

// Comaprision function. Returns <0 for a<b =0 for a=b and >0 for a>b
static int compare_people( const void *a, const void *b )
{

    // Cast to the correct pointer type
    const person *p1 = (const person*) a;
    const person *p2 = (const person*) b;

    // There are different ways of comparing floats to each other. In this case we use |a - b| < epsilon for some small epsilon
    float difference = p2->score - p1->score;

    if( difference <= -EPSILON )
    {
        return -1;
    }
    else if( difference >= +EPSILON )
    {
        return +1;
    }

    return 0;

}

int main()
{

    // Initialize list of people
    size_t num_people = 5;
    person people[num_people];

    people[0].score = 0.3f;
    people[0].name = "Homer";

    people[1].score = 1.4f;
    people[1].name = "Marge";

    people[2].score = 0.02f;
    people[2].name = "Bart";

    people[3].score = 13.2f;
    people[3].name = "Lisa";

    people[4].score = 1.0f;
    people[4].name = "Maggie";

    // Print unsorted
    printf( "Unsorted:\n" );
    for( size_t i = 0; i < num_people; ++i )
    {
        printf( "  %s - %2.2f\n", people[i].name, people[i].score );
    }
    printf( "\n" );

    // Sort
    qsort( people, num_people, sizeof(person), &compare_people );

    // Print sorted
    printf( "Sorted:\n" ) ;
    for( size_t i = 0; i < num_people; ++i )
    {
        printf( "  %s - %2.2f\n", people[i].name, people[i].score );
    }
    printf( "\n" );

    // Done
    return EXIT_SUCCESS;

}

Note the comment about comparing floating point values.

If you are using Linux, you can investigate system calls and functions of the C standard library by looking up the corresponding man page, e.g.

man qsort

0

For example you ll have

vector<person> vec;

Now you want sort this using sort from STL

You first must create method for operator " < "

bool operator<(const person& a, const person& b){
    return a.score<b.score;
}

I hope this helps you I m sorry for my bad grammar :)

Example of usage

sort(vec.begin(),vec.end(),[](const person& a, const person&b){return b<a});

Now your vector of person will be sorted upside - down order.

  • This question isn't tagged c++ at the moment. You might want to wait until they clarify. – Retired Ninja Jan 13 '17 at 03:08
  • Yes I saw it also now, but thiking he wants to put data in vector so I clarified its C++ I m sorry If I made mistake here. EDIT: This seems like assigment or something.... – Suad Halilović Jan 13 '17 at 03:09