Note that there are some explanatory texts on larger screens.

plurals
  1. POC (beginner) : Why won't my qsort work? EDIT: From one error to another
    primarykey
    data
    text
    <p>I'm doing K&amp;R exercise 6-4, which is:</p> <blockquote> <p>6-4. Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.</p> </blockquote> <p>What I decided to do is create a struct called dstncFreqNode6_4:</p> <pre><code>struct dstncFreqNode6_4 { char *word; int count; struct dstncFreqNode6_4 *left; struct dstncFreqNode6_4 *right; }; </code></pre> <p>I then parsed the input for words, and for each distinct word created one "dstncFreqNode6_4" node and two pointers to it: one to insert in to a BST (to add new words/update the counts of already encountered words), and one to insert in to a global array of "dsntFredNode6_4" pointers.</p> <p>This was done so that I could update the counts of words (nodes) by traversing the BST (which contains pointers to all the words encountered so far). After the entire input has been parsed, the array of pointers would be sorted by the members' "count" variables, and then printed. Since the </p> <p>The code the adding of new words/updating the counts is here:(<em>I don't think anything is wrong with it,since the BST and array appear to be populated correctly, so you may ignore this)</em>:</p> <pre><code>//"wordCounts" is originally a global dstncFreqNode6_4** declared outside the function, numWords is the current length of the array struct dstncFreqNode6_4 *addFreqNode6_4(struct dstncFreqNode6_4 *root, char *word) { if(root == NULL) { root = (struct dstncFreqNode6_4 *) malloc(sizeof(struct dstncFreqNode6_4)); root -&gt; word = word; root -&gt; count = 1; root -&gt; left = NULL; root -&gt; right = NULL; struct dstncFreqNode6_4 **p; wordCounts = (struct dstncFreqNode6_4 **) realloc(wordCounts, sizeof(struct dstncFreqNode6_4*) * (numWords +1)); p = wordCounts + numWords++; (*p) = root; } else if(strcmp(word, root -&gt; word) &lt; 0) root -&gt; left = addFreqNode6_4(root -&gt; left, word); else if(strcmp(word, root -&gt; word) &gt; 0) root -&gt; right = addFreqNode6_4(root -&gt; right, word); else root -&gt; count++; return root; </code></pre> <p>}</p> <p>So I've got everything working right except the sorting; it simply won't sort the array. That is to say... <strike>the order of the elements remains unchanged</strike> <strike><strong>EDIT:</strong> I get a segmentation error.</strike> <strong>EDIT #2:</strong> No segmentation error now; the original problem still exists.</p> <p>I'm using stlib.h's qsort method; the compare function i'm using is:</p> <pre><code>int qcmp6_4(const void *a, const void *b) { return (*(struct dstncFreqNode6_4 **)a) -&gt; count - (*(struct dstncFreqNode6_4 **)b) -&gt; count; } </code></pre> <p>I can't seem to figure out why it won't sort correctly. I actually implemented my own quicksort algorithm and am getting the same results. I really don't know at this point.</p> <p>It would be nice to get some fresh, and expert eyes to point me in the right direction. Thanks.</p> <p><strong>EDIT</strong></p> <p>Sorry, here's the call to qsort:</p> <pre><code>qsort(wordCounts, numWords, sizeof(struct dstncFreqNode6_4 *), qcmp6_4); </code></pre> <p><strong>EDIT #2:</strong></p> <p>Following the suggestion, I've made "wordCounts" an array of pointers to nodes (<strong>all the code in this post has been updated to reflect this</strong>). So in essence, the BST and the array contain identical information (in fact the array pointers are initialized to the corresponding pointer in the BST), but their uses are different. The BST is used to add new words/update the counts of already encountered ones, and the array is sorted at the end (by the each of the words' counts) and printed. However, i'm running in to the same problem I originally ran in to: the order of the array remains the same after the call to qsort.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload