0
#include <stdio.h>

typedef struct  
{  int occurrence
   char  charArray[101]; 
}Wordy;


int comparing(const void *a, const void *b) 
{
   //....
}

int main(void)
{  
        int i=0;
    Wordy array[99999];

  //.....



    return 0;

}

*/

This program reads words from a file, then the occurrence of the word is calculated. Everything works, but I don't think the qsort function is working correctly in the comparing function. It's suppose to sort array in terms of its occurrence. (Finding its occurrence is correct.)

My sample text file:

zero one
two zero
three three

Output:

Before: zero one two three 

After:  one two three 

(notice that zero is missing after sorting)

1
  • 2
    You refer to array[1].word and array[0].word but Wordy has no member named word, does this compile? Commented Jan 15, 2014 at 15:23

2 Answers 2

2

This:

qsort(array, i, sizeof(int), comparing);

is wrong, array is not an array of int, it's an array of Wordy. When you specify the wrong element size to qsort(), the results are more or less random since you're going to get pointers into int-sized "slices" of the Wordy struct array.

Try:

qsort(array, i, sizeof array[0], comparing);
Sign up to request clarification or add additional context in comments.

Comments

0

Is it because your printf statements are different? word and charArray ordered differently.

Before:

array[0].charArray, array[1].word, array[2].charArray, array[3].charArray);

After:

array[0].word, array[1].charArray, array[2].charArray, array[3].charArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.