Thread: structure, and sorting it

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    33

    structure, and sorting it

    So I'm currently learning more about structures, but I'm running into a problem.
    Code:
    1. #include <stdio.h>
    2. struct person
    3. {
    4. char first_name[20];
    5. char last_name[20];
    6. int age;
    7. };
    8. void print_person_info(struct person info);
    9. void sort_age(int n, struct person peeps[]);
    10. int main()
    11. {
    12. int i, n = 5;
    13. struct person student[5] =
    14. {
    15. {"Bob", "Smith", 21},
    16. {"Jim", "Smith", 13},
    17. {"Amy", "Smith", 19},
    18. {"Tam", "Smith", 24},
    19. {"Sally", "Smith", 17}
    20. };
    21. for (i=0; i < n; i++)
    22. print_person_info(student[i]);
    23. sort_by_age(5, student);
    24. /*printf("----After Sorting------\n");
    25. for (i=0; i < n; i++)
    26. [I] print_person_info(student);*/
    27. }
    28. void print_person_info(struct person info)
    29. {
    30. printf("Name = %s %s\n", info.first_name, info.last_name);
    31. printf("Age = %i\n\n", info.age);
    32. }
    33. void sort_age(int n, struct person peeps[])
    34. {
    35. printf("n = %i %s %s %i", n , peeps[1].first_name, peeps[1].last_name, peeps[1].age);
    36. }
    I'm trying to make function that sort the structure by youngest to oldest, but I'm having a problem with the function it self and the test inside of it. When I try to compile it, I get a reference error.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good that you posted your code in code tags and with some attempt at indentation. However, it would be even better to use more than one space per indent level, to separate parts of your code with blank lines, and to be a little more consistent. For example:
    Code:
    #include <stdio.h>
    
    struct person
    {
        char first_name[20];
        char last_name[20];
        int age;
    };
    
    void print_person_info(struct person info);
    void sort_age(int n, struct person peeps[]);
    
    int main()
    {
        int i, n = 5;
        struct person student[5] =
        {
            {"Bob", "Smith", 21},
            {"Jim", "Smith", 13},
            {"Amy", "Smith", 19},
            {"Tam", "Smith", 24},
            {"Sally", "Smith", 17}
        };
    
        for (i = 0; i < n; i++)
        {
            print_person_info(student[i]);
        }
        sort_by_age(5, student);
    
        /*printf("----After Sorting------\n");
        for (i=0; i < n; i++)
        [I]        print_person_info(student);*/
    }
    
    void print_person_info(struct person info)
    {
        printf("Name = %s %s\n", info.first_name, info.last_name);
        printf("Age  = %i\n\n", info.age);
    }
    
    void sort_age(int n, struct person peeps[])
    {
        printf("n = %i  %s %s %i", n , peeps[1].first_name, peeps[1].last_name, peeps[1].age);
    }
    I suggest:
    Code:
    struct person student[] =
    {
        {"Bob", "Smith", 21},
        {"Jim", "Smith", 13},
        {"Amy", "Smith", 19},
        {"Tam", "Smith", 24},
        {"Sally", "Smith", 17}
    };
    const size_t n = sizeof(student) / sizeof(student[0]);
    This way, you can add or remove entries from the initialiser without worrying about forgetting to change the "5" or the initialiser for n. I changed n to be a size_t because size_t is the type of the result of sizeof, and besides, it is non-negative. You should change the declaration of i to match.

    You probably do not want to copy the struct person object for printing, so you should pass a pointer to it:
    Code:
    void print_person_info(const struct person *info)
    {
        printf("Name = %s %s\n", info->first_name, info->last_name);
        printf("Age  = %i\n\n", info->age);
    }
    I used the const keyword since within the context of this function, the struct person object is effectively constant: you have no need to modify it to print its contents.

    Likewise, you could create another function for printing the entire list of persons:
    Code:
    void print_persons(const struct person persons[], size_t n)
    {
        size_t i;
        for (i = 0; i < n; i++)
        {
            print_person_info(&persons[i]);
        }
    }
    This way, you avoid duplicating code for printing the list before and after the sort.

    Quote Originally Posted by screwlu
    I'm trying to make function that sort the structure by youngest to oldest, but I'm having a problem with the function it self and the test inside of it. When I try to compile it, I get a reference error.
    What exactly is the error message?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    ...
    I haven't learn about const, size_t, nor '->', so I think it's best if I don't put that in my code for now. As for the pointer you mention in the print function, I been watching video and my teacher had been explaining it many times, but I still don't have a grasp of it (like when to use it and why). I know (unless it's wrong) that pointer "literally point" to something, and it's can be use as deference, but that's all.
    As for the error, it's this structure, and sorting it-32b91dc0958211dcff0adfb41ed9aa9b-png

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Look carefully at how you declared the function:
    Code:
    void sort_age(int n, struct person peeps[]);
    Now, look at how you called it:
    Code:
    sort_by_age(5, student);
    Notice the discrepancy?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    [QUOTE=laserlight;1243685]Ah. Look carefully at how you declared the function:
    Code:
    void sort_age(int n, struct person peeps[]);
    Now, look at how you called it:
    Code:
    sort_by_age(5, student);
    EDIT: omg, wow. Thanks!
    Last edited by screwlu; 11-07-2015 at 12:40 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay. What is the difference between sort_age and sort_by_age? If you still don't get it, what is the difference between sort_age and sort_BY_age?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Okay. What is the difference between sort_age and sort_by_age? If you still don't get it, what is the difference between sort_age and sort_BY_age?
    lol, yea I got it, I had to re-read it like 3 time just to see the difference lmao

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Okay. What is the difference between sort_age and sort_by_age? If you still don't get it, what is the difference between sort_age and sort_BY_age?
    I'm running into a problem with structure again...
    Code:
    #include <stdio.h>//coded by Tam Nguyen 11/6/15
    struct person
    {
        char first_name[20];
        char last_name[20];
        int age;
    };
    
    
    void print_person_info(struct person info);
    void sort_age(int n, struct person peeps[]);
    
    
    int main()
    {
        int i, n = 5;
        struct person student[5] =
        {
            {"Bob", "Smith", 24},
            {"Jim", "Smith", 49},
            {"Amy", "Smith", 75},
            {"Tam", "Nguyen", 100},
            {"Sally", "Smith", 17}
        };
    
    
        for (i=0; i < n; i++) print_person_info(student[i]);
    
    
        sort_age(n, student);
    
    
        printf("----After Sorting------\n\n");
        for (i=0; i < n; i++) print_person_info(student[i]);
    }
    
    
    void print_person_info(struct person info)
    {
        printf("Name = %s %s\nAge  = %i\n\n", info.first_name, info.last_name, info.age);
    }
    
    
    void sort_age(int n, struct person peeps[])
    {
        int first, last, tempAGE;
        char *tempFirst, *tempLast;
    
    
    
    
        //printf("inside sortage\n");
        //set first age to the first struct of array
        for (first = 0; first < n-1; first++)
        {
            //set "last" to be second struct of array
            for (last = first+1; last < n; last++)
            {
                //compare and switch
                if (peeps[first].age > peeps[last].age)
                {
    
    
                    // VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV error's here. Ignore the comment
                    //hold on to 'first' age
                    tempAGE = peeps[first].age;
                    *tempFirst = peeps[first].first_name;
                    *tempLast = peeps[first].last_name;
                    //set 'first' age to 'last' age
                    peeps[first].age = peeps[last].age;
                    peeps[first].first_name = peeps[last].first_name;
                    peeps[first].last_name = peeps[last].last_name;
                    //set 'last' age to old 'first' age
                    peeps[last].age = tempAGE;
                    peeps[last].first_name = *tempFirst;
                    peeps[last].last_name = *tempLast;
    
    
                }
            }
        }
    }
    it give me this error structure, and sorting it-ea8c4c93752ad138b0f898c044b31d09-png

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're making it too complicated.
    Even crusty old C allows structure assignment.

    Eg.
    struct person temp = peeps[first];
    peeps[last] = peeps[first];
    peeps[first] = temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    Quote Originally Posted by Salem View Post
    You're making it too complicated.
    Even crusty old C allows structure assignment.

    Eg.
    struct person temp = peeps[first];
    peeps[last] = peeps[first];
    peeps[first] = temp;
    I tried something like that (making a temp struct then set it to peeps[first]) but everytime i compile it, it crashes... I don't know why your code is working while mine didnt... anyway, thanks!
    Last edited by screwlu; 11-10-2015 at 12:10 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an array of strings in a structure?
    By mugiwara528 in forum C Programming
    Replies: 6
    Last Post: 03-04-2011, 12:37 PM
  2. Can I use STL qsort() for sorting structure?
    By franziss in forum C++ Programming
    Replies: 14
    Last Post: 09-20-2005, 05:34 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. I need help with sorting a simple structure please
    By AlmostBeginner in forum C Programming
    Replies: 2
    Last Post: 04-11-2003, 03:01 AM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 11:45 AM