Thread: Need help with Bubblesort with random numbers

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Need help with Bubblesort with random numbers

    hi,
    i'm trying to fill an array with random numbers and then sort them via bubblesort. it seems to work so far. the problem is, that i seem to get the same numbers for the same input. somehow the randomness isn't working. anyone an idea why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {    
        int a, b, c, d, e, f;
        
    printf("number of elements from 1 to x:\n");
    scanf("%d", &b);
    printf("max random number:\n");
    scanf("%d", &f);
        int array[b];
        for(a=0; a<b; a++)
        {
            c = rand() %f+1;
            array[a] = c;
        }
        for(a=0; a<b; a++)
        {
            for(d=0; d<b-1; d++)
            {
                if(array[d] > array[d+1])
                {
                    e=array[d];
                    array[d]=array[d+1];
                    array[d+1]=e;
                }
            }
        }
        for(a=0; a<b; a++)
        {
            printf("%d\n", array[a]);
        }
        return 0;
        }
    greetings,
    grindbert

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to seed the random number generator with "srand()".

    It's typical to use the current time as a seed, to ensure different values each run. See here for an example: FAQ > Generate random numbers? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    thanks!
    i'll look into it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use names that are descriptive. Generally, single letter variable names are only used for loop/array indices, or in certain computations to correspond to their naming in some mathematical formula. For example, instead of using b to mean "number of elements", you could have used number_of_elements, element_count, num_elements, or something along those lines that give the reader an idea of what it is for.

    Next, note that you are creating a variable length array here:
    Code:
    int array[b];
    If this works for you, well and good, but note that C implementations are not required to support the feature.

    Quote Originally Posted by grindbert
    it seems to work so far. the problem is, that i seem to get the same numbers for the same input. somehow the randomness isn't working. anyone an idea why?
    You did not call srand to seed the pseudorandom number generator differently on each run of the program.

    By the way, it is good to see your effort at proper indentation, but the four printf/scanf calls should have been properly indented too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting random numbers with bubblesort
    By Sam19911 in forum C Programming
    Replies: 16
    Last Post: 11-11-2011, 07:37 AM
  2. Random Numbers (mean)
    By olig1905 in forum C Programming
    Replies: 9
    Last Post: 10-09-2010, 05:43 AM
  3. Random numbers not random?
    By yacek in forum C Programming
    Replies: 6
    Last Post: 10-08-2010, 06:57 PM
  4. Random numbers
    By Chris_1980 in forum C Programming
    Replies: 2
    Last Post: 06-16-2010, 09:18 AM
  5. Replies: 4
    Last Post: 11-16-2004, 07:29 AM

Tags for this Thread