?
Solved

qsort problems

Posted on 2003-03-11
4
Medium Priority
?
303 Views
Last Modified: 2010-04-15
I've got my program working so that it will allocate memory for 10 random characters and then reallocate it for 5 more.  My problem is the qsort.  I cannot get it to now take the random characters and generate a sorted list of the random characters.  Here is the code I've tried for doing the qsort.  Any help would be greatly appreciated.

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

#define Max 10


int compare();

int main(void)
{
  int i, NewMax;
  char array[] = "abcdefghijklmnopqrstuvwxyz0123456789";
  char *ptr_array;
 
  /* Allocates 10 bytes of memory */
  ptr_array = malloc(Max * sizeof *ptr_array);
 
    srand((unsigned)time(NULL));
   
    /* Fill the 10 spots */
    for (i=0; i<Max; i++)
      ptr_array[i] = array[rand() % (sizeof(array)-1)];

    /* Print all 10 characters */
    for (i=0; i<Max; i++)
      printf("%c", ptr_array[i]);

    printf("\n");
   
    /* Reallocated to 15, save in a second pointer in case of failure */
    NewMax = Max + 5;

    ptr_array = realloc ( ptr_array, NewMax * sizeof *ptr_array );

      /* Fill the next 5 spots */
      for (i=Max; i<NewMax; i++)
        ptr_array[i] = array[rand() % (sizeof(array)-1)];
     
      /* Print all 15 characters */
      for (i=0; i<NewMax; i++)
        printf("%c", ptr_array[i]);

      printf("\n");
   

    free(ptr_array);


     //Sort Characters
    qsort(ptr_array, NewMax, sizeof (ptr_array[0]), compare);
          printf("\nSorted characters are:\n");
          for(i=0; i<NewMax; i++)  
          printf("%c\n",ptr_array[0]);

     return 0;
}

//Compare Characters
int compare(int *a, int *b)
{
    return (*a - *b);
}
0
Comment
Question by:beeLeaf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8114525
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define Max 10

//Compare Characters
int compare(const void *a, const void *b)
{
   int x = (*(int*)a - *(int*)b);
   return x;
}

int main(void)
{
 int i, NewMax;
 char array[] = "abcdefghijklmnopqrstuvwxyz0123456789";
 int *ptr_array;
 
 /* Allocates 10 bytes of memory */
 ptr_array = (int *)malloc(Max * sizeof(int));
 
   srand((unsigned)time(NULL));
   
   /* Fill the 10 spots */
   for (i=0; i<Max; i++)
     ptr_array[i] = array[rand() % (sizeof(array)-1)];

   /* Print all 10 characters */
   for (i=0; i<Max; i++)
     printf("%c", ptr_array[i]);

   printf("\n");
   
   /* Reallocated to 15, save in a second pointer in case of failure */
   NewMax = Max + 5;

   ptr_array = (int *)realloc ( ptr_array, NewMax * sizeof *ptr_array );

     /* Fill the next 5 spots */
     for (i=Max; i<NewMax; i++)
       ptr_array[i] = array[rand() % (sizeof(array)-1)];
     
     /* Print all 15 characters */
     for (i=0; i<NewMax; i++)
       printf("%c", ptr_array[i]);

     printf("\n");
   

//   free(ptr_array);


    //Sort Characters
   qsort((void *)ptr_array, NewMax, sizeof (int), compare);
         printf("\nSorted characters are:\n");
         for(i=0; i<NewMax; i++)  
         printf("%c\n",ptr_array[i]);

    return 0;
}

0
 
LVL 6

Expert Comment

by:gj62
ID: 8114548
The posted code corrects a few minor glitches - as follows:

Compare wants const void *, so you have to do a bit of casting - but since pointers are all the same size, no worries...  I included an extra statement in the compare function in case you want to put a breakpoint and see just what is happening - you can rewrite to:

return (*(int*)a - *(int*)b);
 
Also, you were freeing ptr_array before calling qsort - definitely the way to a quick exit...  I did not put back the call to free(ptr_array); - you can put it just before you return 0; from main()
0
 
LVL 6

Accepted Solution

by:
gj62 earned 200 total points
ID: 8114575
Oh, I changed ptr_array to int, you can change it back to char, as shown below - I had a typo initially and instead of changing my typo to char, I changed your stuff to int...

Here it is more like your original:

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

#define Max 10

int compare(const void *a, const void *b);

int main(void)
{
 int i, NewMax;
 char array[] = "abcdefghijklmnopqrstuvwxyz0123456789";
 char *ptr_array;
 
 /* Allocates 10 bytes of memory */
 ptr_array = (char *)malloc(Max * sizeof(int));
 
   srand((unsigned)time(NULL));
   
   /* Fill the 10 spots */
   for (i=0; i<Max; i++)
     ptr_array[i] = array[rand() % (sizeof(array)-1)];

   /* Print all 10 characters */
   for (i=0; i<Max; i++)
     printf("%c", ptr_array[i]);

   printf("\n");
   
   /* Reallocated to 15, save in a second pointer in case of failure */
   NewMax = Max + 5;

   ptr_array = (char *)realloc ( ptr_array, NewMax * sizeof *ptr_array );

     /* Fill the next 5 spots */
     for (i=Max; i<NewMax; i++)
       ptr_array[i] = array[rand() % (sizeof(array)-1)];
     
     /* Print all 15 characters */
     for (i=0; i<NewMax; i++)
       printf("%c", ptr_array[i]);

     printf("\n");
   


    //Sort Characters
   qsort((void *)ptr_array, NewMax, sizeof(ptr_array[0]), compare);
         printf("\nSorted characters are:\n");
         for(i=0; i<NewMax; i++)  
         printf("%c\n",ptr_array[i]);

   free(ptr_array);

    return 0;
}

//Compare Characters
int compare(const void *a, const void *b)
{
   return (*(char*)a - *(char*)b);
}

0
 

Author Comment

by:beeLeaf
ID: 8116654
Thank you SO much qi62...That was just help I needed!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question