Trimming an int array for bucket sort in C -


i writing bucket sort program in c. after merge small buckets large bucket, need remove -1s padded small buckets with.

i'm pretty new c, there simple solution overlooking.

this solution, seems return array 1 trailing junk value , trailing 0s fill array until size of untrimmed bucket (where desired result bucket without -1s, junk values, , trailing 0s).

    // function trim bucket of given size bucket containing no -1s     int* trimbucket(int* bucket, int size)     {         int n = 0, = 0;         int* newbucket;          // loop count number of elements between 0 , 9999         for(n = 0; n < size; n++)          {             if(bucket[n] != -1 && bucket[n] < 10000)                 i++;         }          // create new bucket equal number of elements counted         // filled -2 differentiate -1s contained in bucket array         newbucket = allocateandinitiateoned(i, -2);          = 0;          for(n = 0; n < size; n++)         {             // want values between 0-9999 put new array             if(bucket[n] != -1 && bucket[n] < 10000)              {                 newbucket[i] = bucket[n];                 i++;             }                }          free(bucket); // doing right?         return newbucket;     } 

allocateandinitiateoned function:

    // function allocate memory 1 dimensional array , fill given value     int* allocateandinitiateoned(int x, int initialnum)     {         int runs = 0;         int* onearray;          onearray = malloc(sizeof(int) * x);          for(runs = 0; runs < x; runs++)             onearray[runs] = initialnum;          return onearray;     } 

could please me understand i'm doing wrong , how desired result?

thanks help!

edit: compiling , running on unix system. possibly not related, multi-processed program using mpi library (this doesn't seem problem lies).

looks working, need return new size function well, without idea of length of array, can read right off end of newly chosen size , garbage (1 trailing junk value & zeroes...).

an array in c has 2 pieces, always. start pointer, , size. size implicit, needs there somehow, or you'll keep reading forever.

if need return multiple things function, either:

  • return (one or both) via pointer parameter
  • return them both through struct

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -