sorting and shuffling array

Discussion in 'C Programming' started by SilverWolf, Oct 13, 2003.

  1. SilverWolf

    SilverWolf Guest

    I need some help with sorting and shuffling array of strings. I can't
    seem to get qsort working, and I don't even know how to start to shuffle
    the array. Here is what I have for now:

    #include <stdio.h>

    void main(void)
    {
    char lines[100][100];
    int count = 0, i;

    while (gets(lines[count]))
    ++count;

    for (i = 0; i < count; ++i)
    puts(lines);
    }

    Thanks.
    SilverWolf, Oct 13, 2003
    #1
    1. Advertising

  2. SilverWolf

    Eric Sosman Guest

    SilverWolf wrote:
    >
    > I need some help with sorting and shuffling array of strings. I can't
    > seem to get qsort working, and I don't even know how to start to shuffle
    > the array. Here is what I have for now:


    Not much, but it already has three errors, two of them
    quite serious. I'll point them out and provide some hints
    about how to fill in the rest -- hints only, because this
    has a homework-y whiff to it ...

    By the way, I'm not going to address the "shuffling"
    question until you explain more clearly what you want to
    do, and how you're going to decide whether an array is
    "shuffled" or "unshuffled." For now, sorting only!

    > #include <stdio.h>


    Since you want to sort, you'll probably want to use the
    qsort() library function. And since the things to be sorted
    are strings, you'll probably want to use the strcmp() function
    to compare them. If so, include the appropriate headers:

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

    > void main(void)


    (Sigh.) For the INT_MAX'th time: main() returns an `int'
    to indicate whether the program completed successfully or
    otherwise. Read all about it in your favorite C reference --
    but if that reference advocates `void main', get a different
    reference. Use the pages of the old one while seated in the
    smallest room of your house, in such a way that they wind up
    behind you.

    > {
    > char lines[100][100];
    > int count = 0, i;
    >
    > while (gets(lines[count]))
    > ++count;


    Two serious errors in two lines. The first is that you
    are using the deadly gets() function; see Question 12.23 in
    the comp.lang.c Frequently Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/s12.html

    .... to learn why you should never do this.

    The second error is similar in nature to the first, except
    that you've written it yourself instead of inheriting it from
    the library: What happens if somebody feeds 315 strings into
    this program? You blindly try to fill a 100-place array with
    215 more strings than it can hold, and there's no telling what
    might happen. Most likely, the "overflow" strings will stomp
    all over some other piece of memory and your program will crash,
    but that's only if you're lucky.

    Okay: If you've made it this far, you have an array holding
    `count' 100-character strings. You can now use qsort() to put
    them into order, like this:

    qsort (lines, count, sizeof lines[0], compare_func);

    The first argument is the array to be sorted, the second is the
    number of strings in the array, the third is the number of bytes
    in each string, and the last is the name of a comparison function.

    "A comparison function," you ask? Yes: qsort() rearranges
    the array in accordance with an ordering relation that you
    define, and the way you define it is by providing a function.
    This function accepts pointers to two array elements and decides
    what their order should be in the sorted array. If the first
    must precede the second, the function returns a negative value.
    If the second must precede the first, the function returns a
    positive value. If their order doesn't matter (they are "equal"
    in all respects that are important to you), the function returns
    zero.

    Question 13.8 in the FAQ illustrates how to write such a
    comparison function, *but* note that it is sorting an array of
    pointers to strings, not an array of strings themselves as in
    your case. The FAQ's code won't work for you -- in fact, the
    FAQ's question wouldn't even arise for you! Ponder it anyhow,
    to get an idea of how comparison functions work.

    > for (i = 0; i < count; ++i)
    > puts(lines);


    Since main() returns an `int' (see above), you need a `return'
    statement here. Any `int' at all will do, but you may confuse
    your operating environment if you return some nonsensical status.
    Three values that are guaranteed to make as much sense as possible
    to the environment are zero (indicating success), EXIT_SUCCESS
    (also indicating success, possibly in a different way), and
    EXIT_FAILURE (indicating failure). The latter two values are
    defined in <stdlib.h>, which you've already included in order to
    use qsort().

    > }
    >
    > Thanks.


    You're welcome. Good luck!

    --
    Eric Sosman, Oct 13, 2003
    #2
    1. Advertising

  3. SilverWolf

    Mike Wahler Guest

    "Eric Sosman" <> wrote in message
    news:...
    > SilverWolf wrote:
    > > char lines[100][100];


    > > for (i = 0; i < count; ++i)
    > > puts(lines);


    >
    > qsort (lines, count, sizeof lines[0], compare_func);
    >
    > The first argument is the array to be sorted, the second is the
    > number of strings in the array, the third is the number of bytes
    > in each string,


    While this is indeed the correct argument to use,
    sizeof lines[0] will be 100, the size of each ("second
    dimension") element of the array 'lines'. I'd say that
    the number of bytes in each *string*, is determined by
    strlen(lines). I know you're probably trying to keep
    things "simple", but imo this could lead to confusion
    later.


    To "SiverWolf": 'qsort()' needs to be told the size
    is 100, since it cannot know how many of the 100 bytes
    are actually part of the string (anything after the
    first '\0' character is not part of the string).

    Other than that, an excellent reply, Eric.

    $.02,
    -Mike
    Mike Wahler, Oct 14, 2003
    #3
  4. SilverWolf wrote:

    > I need some help with sorting and shuffling array of strings.


    Don't try to run before you can walk. You have the wrong return type for
    main(), and you are using the broken gets() function.

    > I can't
    > seem to get qsort working,


    qsort already works just fine. You just don't know how to drive it. And it
    doesn't make sense to try to drive qsort correctly when your knowledge of
    much more basic stuff, such as main and gets, is so flawed. Build only on
    truth, and your understanding of the harder stuff will be much clearer.

    > and I don't even know how to start to shuffle
    > the array.


    If you had been paying attention eight or nine hours ago in IRC, you would
    have seen how to do the array shuffle. Array shuffling is dealt with in FAQ
    13.19 in the book. I haven't checked the online version.

    <snip>

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glowfire

    random shuffling

    glowfire, Feb 19, 2005, in forum: C++
    Replies:
    16
    Views:
    792
    Martijn Mulder
    Feb 23, 2005
  2. greenflame

    shuffling elements of a list

    greenflame, May 31, 2006, in forum: Python
    Replies:
    22
    Views:
    637
    Peter Otten
    Jun 2, 2006
  3. justin

    Shuffling rows in a GridView

    justin, Sep 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    8,981
    Alvin Bruney [MVP]
    Sep 15, 2006
  4. Replies:
    8
    Views:
    349
    Howard
    Mar 20, 2006
  5. Alexey Verkhovsky
    Replies:
    8
    Views:
    157
    Hal Fulton
    Jul 18, 2004
Loading...

Share This Page