![]() |
|
#1
|
|||
|
|||
sorting a two-dimensional array using qsortHello again
I'm trying to sort a 2D array using qsort, but I'm not sure how can I use qsort with this kind of arrays. First of all, my array is declared as a char **array. I have as many rows as, let's say, numLines, and in every position I have a string of 3 chars. CPP / C++ / C Code:
Now, I want to sort the values of the array as if they where integers. I have declared a comparison function like that: CPP / C++ / C Code:
And when I want to sort the array, I call the qsort function like that: CPP / C++ / C Code:
I don't know how many mistakes I've done in all these steps. Maybe my comparing function is not correct? I've tested with two individual members of the array and it returned the correct values. Maybe the call to qsort is absolutly incorrect? I think that this is the cause of my problems, but don't know how to solve it. Thanks in advance for your help and patience. |
|||
|
#2
|
|||
|
|||
|
qsort is usually used to sort 1D arrays, but I think sorting 2D arrays can be done as well.
Quote:
CPP / C++ / C Code:
Your comparing function has a big confusion in the "return ..." line. However the whole thing should be modified because we are quick-sorting 2D arrays instead of 1D. So I suggest the following: CPP / C++ / C Code:
CPP / C++ / C Code:
Now the qsort sorts actually the pointers of the array. To understand the function Compare notice that the function gets pointers to pointers as an argument. Notice that array is not an array of chars but an array of pointers. so the quick sort gives pointers to pointers for the compare. Then the Compare gives the pointing targets to the atoi() functions..and finally the qsort rearranges the order of the pointers in array (a bit confusing to name an array as "array" some more descriptive name would be better) |
|
#3
|
|||
|
|||
|
Thanks a lot again, maprich. Great explanation.
I have a problem yet. When I use your version of Compare, I get an error: Warning:"Dereferencing void* pointer" Error: void value not ignored as it ought to be The bold lines have the problem. Quote:
I see that you are using a double pointer, but in fact, I think that the function has to handle only 1D arrays, cause it have to compare strings. So maybe I have to get rid of one of the pointers. What do you think? |
|
#4
|
|||
|
|||
|
Quote:
|
|
#5
|
|||
|
|||
|
Ok I had errors because I didn't actually compile my code.. However I did now and detected several small and some big errors.
The compiler protects the "const" variables so jealously that the conversions are difficult to make. But not impossible.. here is my entire code: CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
|
^^^ as you see inside the qsort I had to make that quite horrendous function typecast so that Compare() function could take pointers to pointers as a parameter... all that just so that atoi could get access to the actual strings.
also originally I forgot to typecast the malloc void pointers.. and in Compare() returnvalue was inverted.. meaning that if you do "return (iB-iA);" you get descending order and if you do "return (iA-iB);" you get ascending order. |
|
#7
|
|||
|
|||
|
CPP / C++ / C Code:
Last edited by Dave Sinkula : 04-Aug-2005 at 08:54.
Reason: Made 'Compare' cast free (in C).
|
|
#8
|
|||
|
|||
|
Ok thanks dave I see your solution is better.. and I didn't first see the reason behind the "return (da > db) - (da < db);" My apologies.
|
|
#9
|
|||
|
|||
|
maprich, Dave, thank you very much.
Can I question one last thing? I have understand the function typecasting explanation from maprich, but I don't know the reason of this declaration Dave: CPP / C++ / C Code:
Maybe it is because a is a const void * and contains a char * variable? |
|
#10
|
|||
|
|||
|
Quote:
If we had written a compare function for ints, we might use this: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting numbers through array | zidanefreak01 | C++ Forum | 3 | 26-Jun-2005 02:41 |
| Dynamic allocation of multi dimensional array | pointer | C Programming Language | 7 | 13-May-2005 23:50 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Sorting a 2D array c++ | mike3340 | C++ Forum | 4 | 15-Dec-2003 13:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The