Domanda

I have to write program on my IT classes:

  1. display 100 random characters and sort it using qsort function
  2. user put 5 words, sort this words alphabetically using qsort function

For now I have something like this, but it doesn't work very well :/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>

int compare1(const void * a, const void * b) {
return ( *(char*)a - *(char*)b );
}

int compare2(const void *a, const void *b) {
    return strncmp((const char *)a, (const char *)b, 20);
}


int main(){

 int i; 
 char words[5][20], c[27]="QWERTYUIOPASDFGHJKLZXCVBNM", t[101];
 srand(time(0));
 printf("QSORT PROGRAM\n\n");
 for (i=0;i<100;i++)
     t[i]=c[rand()%26];
 printf("Order before sorting:\n");
 for (i=0;i<100;i++)
     printf("%c", t[i]);
 qsort(t,100,sizeof(char),compare1);
 printf("\n\nOrder after sorting:\n");
 for (i=0;i<100;i++)
     printf("%c", t[i]);
 printf("\n\nEnter 5 words\n");
 for(i=0;i<5;i++)
     gets(words[i]);
 qsort(words,5,sizeof(char),compare2);
 printf("\nThe sorted order:\n");
 for(i=0;i<5;i++)
     puts(words[i]);
 printf("Press Any Key to Continue ");
 getch();
 return 0;
}
È stato utile?

Soluzione

From the manpage for qsort:

   void qsort(void *base, size_t nmemb, size_t size,
              int(*compar)(const void *, const void *));

The qsort() function sorts an array with nmemb elements of size size. The base argument points to the start of the array.

The contents of the array are sorted in ascending order according to a comparison function pointed to by compar, which is called with two arguments that point to the objects being compared.

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined.

So, what qsort() will do is call the comparison function you specify, each time pointing to two 'somethings', that something being held in an array. It will then swap the 'somethings' around if it wants.

There are two obvious ways to make this work.

The first and perhaps most obvious (but least useful) way)is that you could have a two dimensional array of chars, and the qsort could swap one line of chars with another (in which case each of the arrays of chars would need to be of fixed length size, including the terminating zero, and all the strings would need to abut eachother exactly, so that it can index the start of the nth string at offset n * size.

The second, more subtle, but more useful way is for your array to be a list of pointers to the strings you want to sort. Then qsort just swaps around the pointers.

Your main problem here is that you are doing neither. In lines like:

qsort(t,100,sizeof(char),compare1);

you are saying the size of each entry is sizeof(char), i.e. one character long. So qsort will think you are sorting an array of only 100 characters. That isn't going to work.

As this is your homework, I think I should leave you to it from here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top