Thread: array sorting problem?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    array sorting problem?

    These are the randomly generated numbers:
    0.157576
    0.740321
    -0.428193
    0.029333
    -1.092242
    1.358499
    0.299805
    0.686089
    -0.459023
    -0.283233
    -0.153792
    1.156692
    -0.301258
    0.524468
    -0.471968
    -0.559687
    0.836371
    -1.299736
    -0.405264
    0.644075
    0.211556
    0.660141
    0.460306
    0.663083
    -2.056002
    1.078263
    -0.287218
    0.191338
    -0.513143
    0.134491
    -0.015950
    0.973524
    0.749160
    0.402113
    -2.335234
    1.155122
    -0.545235
    0.193364
    1.928616
    -1.813681
    -0.670871
    0.898627
    0.377252
    0.781241
    -0.427119
    1.150533
    -0.304515
    0.178504
    -0.679288
    0.716458

    And this is what happens when I tried to sort it...
    a[0] = -2.000000
    a[1] = -2.000000
    a[2] = -1.000000
    a[3] = -1.000000
    a[4] = -1.000000
    a[5] = 0.000000
    a[6] = 0.000000
    a[7] = 0.000000
    a[8] = 0.000000
    a[9] = 0.000000
    a[10] = 0.000000
    a[11] = 0.000000
    a[12] = 0.000000
    a[13] = 0.000000
    a[14] = 0.000000
    a[15] = 0.000000
    a[16] = 0.000000
    a[17] = 0.000000
    a[18] = 0.000000
    a[19] = 0.000000
    a[20] = 0.000000
    a[21] = 0.000000
    a[22] = 0.000000
    a[23] = 0.000000
    a[24] = 0.000000
    a[25] = 0.000000
    a[26] = 0.000000
    a[27] = 0.000000
    a[28] = 0.000000
    a[29] = 0.000000
    a[30] = 0.000000
    a[31] = 0.000000
    a[32] = 0.000000
    a[33] = 0.000000
    a[34] = 0.000000
    a[35] = 0.000000
    a[36] = 0.000000
    a[37] = 0.000000
    a[38] = 0.000000
    a[39] = 0.000000
    a[40] = 0.000000
    a[41] = 0.000000
    a[42] = 0.157576
    a[43] = 0.740321
    a[44] = 1.000000
    a[45] = 1.000000
    a[46] = 1.000000
    a[47] = 1.000000
    a[48] = 1.358499
    a[49] = 1.928616

    I used this method to sort them:
    Code:
    int tmp = 0, j = 0; 
        for(i = 0; i < N; i++)
          {
    	for(j = 0; j < N-1-i; j++)
    	  {
    	    if (a[j] > a[j + 1])
    		{
    		  tmp = a[j + 1];
    		  a[j + 1] = a[j];
    		  a[j] = tmp;
    		}
    	  }
          }
        
        printf("\nAscending Order:\n");
        for(i = 0; i < N; i++)
          {
    	printf("a[%d] = %f\n", i, a[i]);
          }
    Can anyone explain why it doesn't sort correctly?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    For starters, you might consider having your temp variable to be the same type as the elements of your array. You are cramming a floating point number into an int. This causes your decimals to be truncated and no, they won't return when you put the number back into the array.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    :P Thanks

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by vutek0328
    :P Thanks
    Another question...

    So on becuase this Gaussian random number generator generates a bell-shaped curve, I want to prove (both numerically and graphically) that more of the random numbers falls near the Xo rather than near the tails. So i'm writing a function additionally that would place each number into a "bin", each bin is the same interval and the length is determined upon command line.

    I think the easiest way for me to write this function is to compare each number with the lower bound of the bin and then upper bound of the bin.

    Code:
        double A=0,B=0,C=0;
        for(i = 0; i < N; i++)
          {
    	if (A < B)
    	  {
    	    if (B < C)
    	      {
    	    A = b[i];
    	    B = a[i];
    	    C = b[i] + 2 * Y / M;
    	    printf ("Yes!");
    	      }
    	  }
    	else 
    	  printf ("No!");
          }
    b[i] = lower bound of the bin
    b[i] + 2Y/M = upper bound of bin
    a[i] = random number

    This part of the code seems to have a lot of mistakes...wonder if I'm on the right track and how I should modify this to place the right number in the right bin.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Write a function which calculates the bin number, given a random value and a parameter for the number of bins

    Code:
    int bins[N] = { 0 };
    binNo = getBinNumber( a[i], N );
    bins[binNo]++;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    not sure I understand what you mean

    Quote Originally Posted by Salem
    Write a function which calculates the bin number, given a random value and a parameter for the number of bins
    I have now a function which generates the upper and lower bound of each bin according to the number of bins entered at command line.

    Code:
        for (i = 0; i < M; i++)
          {
    	double k = -Y + 2 * i * Y / M;
    	bin[i] = k;
    	printf ("\n%.2f to %.2f",  bin[i], bin[i] + 2 * Y / M);
          }
    whereas
    M = number of bins wanted
    Y = the bounds of the graph, from [Xo-Y] to [Xo+Y]

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    If your order algorithm is working, well then is ok
    However, for future references remember there is an standard function in the stdlib named qsort
    you have to create a function to compare the items and pass a pointer of that function to qsort.
    I guess it will save time for you.

    ------------
    http://www.uberum.com

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    qsort()'s useful, but if you're learning how to program then it's informative to write your own sorting code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    never got an actual answer, thanks for the advice on qsort tho.

    so I have a list of bins, say:
    -2.50 to -2.00
    -2.00 to -1.50
    -1.50 to -1.00
    -1.00 to -0.50
    -0.50 to 0.00
    0.00 to 0.50
    0.50 to 1.00
    1.00 to 1.50
    1.50 to 2.00
    2.00 to 2.50

    and a list of numbers:
    a[0] = -1.560800
    a[1] = -1.443761
    a[2] = -1.356102
    a[3] = -1.238508
    a[4] = -1.111993
    a[5] = -1.024359
    a[6] = -0.887842
    a[7] = -0.876591
    a[8] = -0.866904
    a[9] = -0.827389
    a[10] = -0.748203
    a[11] = -0.743547
    a[12] = -0.668352
    a[13] = -0.313693
    a[14] = -0.289167
    a[15] = -0.281864
    a[16] = -0.249217
    a[17] = -0.244982
    a[18] = -0.155720
    a[19] = -0.120759
    a[20] = -0.034794
    a[21] = -0.004531
    a[22] = 0.005659
    a[23] = 0.065637
    a[24] = 0.074373
    a[25] = 0.090674
    a[26] = 0.105830
    a[27] = 0.268969
    a[28] = 0.375321
    a[29] = 0.410788
    a[30] = 0.413647
    a[31] = 0.432920
    a[32] = 0.501529
    a[33] = 0.588264
    a[34] = 0.591639
    a[35] = 0.659567
    a[36] = 0.673095
    a[37] = 0.705943
    a[38] = 0.810920
    a[39] = 1.114770
    a[40] = 1.175180
    a[41] = 1.205583
    a[42] = 1.248732
    a[43] = 1.306904
    a[44] = 1.381767
    a[45] = 1.577235
    a[46] = 1.651503
    a[47] = 1.665884
    a[48] = 2.308061
    a[49] = 2.743726

    How do I check each number to fit them into a specific bin?

    I wrote a preliminary function to check whether each number fits in the bins or not
    Code:
        
    for(i = 0; i < N; i++)
          { 
    	if (bin[i]+2 * Y / M > a[i])
    	  {
    	    if (a[i]>bin[i])
    	      printf("Yes!");
    	    else
    	      printf("No!");
    	  }
    	  else
    	    printf("No!");
          }
    am I on the right track? because I get a string of No!s near the end of the sequence every time...
    Last edited by vutek0328; 09-06-2006 at 04:30 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >am I on the right track?
    You need two for-loops, something like:
    Code:
    for(i = 0; i < N; i++)
    { 
    	for (j=0; j<M; j++)
    	{
    		if (a[i]<bin[j] + 2 * Y / M)
    		{
    			/* Add one to your count for bin[j] */
    			break;
    		}
    	}
    }

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Quote Originally Posted by swoopy
    >am I on the right track?
    You need two for-loops, something like:
    Code:
    for(i = 0; i < N; i++)
    { 
    	for (j=0; j<M; j++)
    	{
    		if (a[i]<bin[j] + 2 * Y / M)
    		{
    			/* Add one to your count for bin[j] */
    			break;
    		}
    	}
    }
    I figured it out 5 minutes before the post hehe, thanks for the help tho, I basically used
    Code:
        for(i = 0; i < N-1; i++)
          { 
    	for(j = 0; j < M-1; j++)
    	  {
    	    if (a[i] >= bin[j] && a[i] < bin[j+1])
    	      printf("Yes!");
    	    else
    	      printf("No!");
    	  }
    	printf("\n");
          }

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    lol, one last question...

    How do I tally up how many numbers belong to each bin?

    Code:
        for(j = 0; j < M-1; j++)
          { 
    	for(i = 0; i < N-1; i++)
    	  {
    	    if (a[i] >= bin[j] && a[i] < bin[j+1])
    	      printf("Yes!");
    	    else
    	      printf("No!");
    	  }
    	printf("\n");
          }
    output:
    Code:
    Yes!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!
    No!Yes!Yes!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!
    No!No!No!Yes!Yes!Yes!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!
    No!No!No!No!No!No!Yes!Yes!Yes!Yes!Yes!Yes!Yes!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!
    No!No!No!No!No!No!No!No!No!No!No!No!No!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!
    No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!Yes!No!No!No!No!No!No!No!No!No!No!
    No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!Yes!Yes!Yes!No!No!No!No!No!No!No!
    No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!Yes!Yes!Yes!No!No!No!No!
    No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!No!Yes!Yes!Yes!Yes!
    I need to basically count how many YES! are in each line...how do I do that in the code?
    Last edited by vutek0328; 09-06-2006 at 06:25 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int yeses = 0;
    /* ... */
    yeses ++;
    /* ... */
    printf("Yeses: %i\n", yeses);
    What's wrong with that?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
    /* Placing randomly generated numbers into various bins on the number line to test the how the probability varies by distance from the mean */
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    #include <gsl/gsl_randist.h>
    #include "add_array.h"
    
    
    int main(int argc, char *argv[])
    {
      int N,i,M,j=0;
      double Xo, mean, sigma, variance, *a, total, X, Y, *bin,tmp=0;
      
      {
        if(argc != 6) 
        {
          printf("Error!\nUsage:N(int) Xo(double) sigma(double) Y(double) M (int)\n");
          return 0;
        }
      else
        sscanf( argv[1], "%d", &N );
      }
      sscanf( argv[2], "%lf", &Xo);
      sscanf( argv[3], "%lf", &sigma);
      sscanf( argv[4], "%lf", &Y);
      sscanf( argv[5], "%d", &M);
      
      
      const gsl_rng_type * T;
        gsl_rng * r;
        
        gsl_rng_env_setup();
        
        T = gsl_rng_default;
        r = gsl_rng_alloc (T);
        gsl_rng_set (r, time(NULL));
        
        a = (double *) malloc (N * sizeof *a);
        bin = (double *) malloc ((M + 1) * sizeof *bin);
        
        printf ("\nInput Parameters: \n\n%d %lf %lf %lf %d\n", N, Xo, sigma, Y, M);  
        printf ("\nLength of Bins: \n");
        
        for (i = 0; i <= M; i++)
          {
    	double k = -Y + 2 * i * Y / M;
    	bin[i] = k;
          }
    
        for (i = 0; i < M; i++)
          {
    	printf ("\n%.2f to %.2f", bin[i], bin[i+1]);
          }
    
        printf ("\n");
        
        for (i = 0; i < N; i++)
          {
    	double u = gsl_ran_gaussian(r,sigma);
    	a[i] = u + Xo;
          }
        
        for(i = 0; i < N; i++)
          {
    	for(j = 0; j < N-1-i; j++)
    	  {
    	    if (a[j] > a[j + 1])
    	      {
    		  tmp = a[j + 1];
    		  a[j + 1] = a[j];
    		  a[j] = tmp;
    		}
    	  }
          }
        
        printf("\nAscending Order:\n");
        for(i = 0; i < N; i++)
          {
    	printf("a[%d] = %f\n", i, a[i]);
          }
        
        printf("\n");
        
        for(j = 0; j < M-1; j++)
          { 
    	for(i = 0; i < N-1; i++)
    	  {
    	    if (a[i] >= bin[j] && a[i] < bin[j+1])
    	      printf("1");
    	    else
    	      printf("O");
    	  }
    	printf("\n");
          }
        
        total= add_array(a, N);
        X = add_squares(a, N);
        
        //printf("\nX = %lf\n", X);
        mean = total/N;
        variance = X/N;
        //printf ("\nTotal:%lf\nMean:%lf\nVariance:%lf\n", total, mean, sqrt(variance));
        
        free(a);
        gsl_rng_free (r);
        return 0;
    }
    I wanted to play around with the code but I was having trouble with a couple things:
    1) code should work for arbitrary mean (edit: got it )
    2) code should handle outliers correctly (sometimes the random number generator spits out ocassional outliers that the program does not cover, I need to find a way to check the number before I run it through thr for-loop)
    3) instead of generating a list of randomly generated numbers, I want to generate one at a time and test them for which bin they would fall under individually...

    I'm trying to edit all of the above things at once from my original code, because I can't successfully figure out how to do each individual one
    Last edited by vutek0328; 09-11-2006 at 06:00 PM.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This seems like a kind of pointless variable:
    Code:
            double k = -Y + 2 * i * Y / M;
            bin[i] = k;
    Why not just do this?
    Code:
            bin[i] = -Y + 2 * i * Y / M;
    As for question 3 . . .
    3) instead of generating a list of randomly generated numbers, I want to generate one at a time and test them for which bin they would fall under individually...
    You could have a loop that generates a random number at the beginning, and then processes that number (possibly by calling a function).

    2) code should handle outliers correctly (sometimes the random number generator spits out ocassional outliers that the program does not cover, I need to find a way to check the number before I run it through thr for-loop)
    What are examples of these numbers? You should be able to handle them with a while/do-while loop (while the number is invalid generate a new one).

    I need to basically count how many YES! are in each line...how do I do that in the code?
    Did you fix this? It doesn't look like it . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  3. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 05:07 AM
  4. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  5. 3D array problem
    By rainman39393 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2008, 06:09 PM