sorting and shuffling array

S

SilverWolf

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.
 
E

Eric Sosman

SilverWolf said:
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 said:
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
which you've already included in order to said:
}

Thanks.

You're welcome. Good luck!
 
M

Mike Wahler

Eric Sosman said:
SilverWolf said:
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
 
R

Richard Heathfield

SilverWolf said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top