Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

qsort problems

Posted on 2003-03-11
4
Medium Priority
?
304 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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

571 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