Thread: A Problem Between "creating an array using rand()" and "the use of bubble"

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question A Problem Between "creating an array using rand()" and "the use of bubble"

    I would like to create an array which has 99 elements using function rand(). I created, and sorted with function bubblesort. But I met something that I cannot construe. The array was sorted as changed. The elements of array have changed. Why? What do you think about it? How it could be happen?

    A Problem Between "creating an array using rand()"  and  "the use of bubble"-rand-bubble-sort-large-png

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 99
    
    
    void bubblesort(int[],int);
    
    
    int main()
    {
        int response[SIZE];
        int i;
    
    
        srand(time(NULL));
    
    
        for(i=0;i<SIZE;i++){
            response[i]= 1 + rand()%9;
        }
    
    
        printf("An Array which is created with function rand() :\n");
        for(i=0;i<SIZE;i++) {
            if(i%10==0) {
                printf("\n%3d",response[i]);
            }
            else
                printf("%3d",response[i]);
        }
    
    
        bubblesort(response,SIZE);
        
        printf("\n\n\nAfter using bubble sort, Array :\n");
        for(i=0;i<SIZE;i++) {
            if(i%10==0) {
                printf("\n%3d",response[i]);
            }
            else
                printf("%3d",response[i]);
        }
    
        return 0;
    }
    
    
    void bubblesort(int array[],int length)
    {
        int pass,compare,hold;
    
    
        for(pass=0; pass<length; pass++){
            for(compare=0; compare<length-1; compare++){
                if(array[compare] > array[compare+1]){
                    //int hold;
                    hold = array[compare];
                    array[compare] = array[compare+1];
                    array[compare+1] = array[compare];
                }
            }
        }
    }
    *I tried to this programme with small-scale numbers. And it worked successfully. The programme with small-scale numbers is in the following:

    A Problem Between &quot;creating an array using rand()&quot;  and  &quot;the use of bubble&quot;-rand-bubble-sort-small-png

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 10
    
    
    void bubblesort(int[],int);
    
    
    int main()
    {
        int response[SIZE];
        int i;
    
    
        srand(time(NULL));
    
    
        for(i=0;i<SIZE;i++){
            response[i]= 1+ rand() %3;
        }
    
    
        for(i=0;i<SIZE;i++){
            printf("%3d",response[i]);
        }
    
    
        bubblesort(response,SIZE);
    
    
        printf("\n\n\nAfter using bubble sort, Array :\n");
        for(i=0;i<SIZE;i++) {
            printf("%3d",response[i]);
        }
    
    
    
    
        return 0;
    }
    
    
    void bubblesort(int array[],int length)
    {
        int pass,hold,compare;
    
    
            for(pass=0;pass<length;pass++) {
            for(compare=0;compare<SIZE-1;compare++) {
                if(array[compare]>array[compare+1]) {
                    hold = array[compare];
                    array[compare]=array[compare+1];
                    array[compare+1]=hold;
                }
            }
        }
    
    
    }
    Attached Images Attached Images A Problem Between &quot;creating an array using rand()&quot;  and  &quot;the use of bubble&quot;-rand-bubble-sort-small-png 
    Last edited by hefese; 07-17-2012 at 07:06 AM. Reason: missing brace{},missing if-else in "loop-for" for output

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't see anything wrong with your first program as far a the sorting goes. However it should not compile as presented. You seem to have at least one missing closing brace in your main() function. Your second program works correctly and if you change the size to a higher value the program seems to work correctly.

    Jim

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Arrow They are editted.

    When I was preparing to share the code here, I forgot something like if-else expression to display well. Now, I editted the code again.

    I notice that if preprocess #define SIZE 10 expression is incremented in the second program such as 20,30,...90 , the programme can be run correctly. But after the first programme is builded and run, it's displayed as folows or something like that:

    A Problem Between &quot;creating an array using rand()&quot;  and  &quot;the use of bubble&quot;-rand-bubble-sort-large-png

    I cannot find difference or differences between two programs above. Don't have to that both are being run properly?

    Note: I editted inside loop-for, e.i. if(i%10==0)), in the second programme to display very well if you increment the preprocess SIZE.

    Code:
    //SECOND PROGRAMME(SMALL-SCALE NUMBERS)
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 10
    
    
    
    
    void bubblesort(int[],int);
    
    
    
    
    int main()
    {
        int response[SIZE];
        int i;
    
    
    
    
        srand(time(NULL));
    
    
    
    
        for(i=0;i<SIZE;i++){
            response[i]= 1+ rand() %3;
        }
    
    
    
    
        printf("An Array which is created with function rand() :\n");
        for(i=0;i<SIZE;i++) {
            if(i%10==0) {
                printf("\n%3d",response[i]);
            }
            else
                printf("%3d",response[i]);
    
        }
    
    
        bubblesort(response,SIZE);
    
    
    
    
        printf("\n\n\nAfter using bubble sort, Array :\n");
        for(i=0;i<SIZE;i++) {
            if(i%10==0) {
                printf("\n%3d",response[i]);
            }
            else
                printf("%3d",response[i]);
    
        }
    
    
        return 0;
    }
    
    
    
    
    void bubblesort(int array[],int length)
    {
        int pass,hold,compare;
    
    
    
    
            for(pass=0;pass<length;pass++) {
               for(compare=0;compare<SIZE-1;compare++) {
                   if(array[compare]>array[compare+1]) {
                      hold = array[compare];
                      array[compare]=array[compare+1];
                      array[compare+1]=hold;
                   }
               }
           }
    }
    Last edited by hefese; 07-17-2012 at 07:48 AM. Reason: add information

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Compare your two "swap" routines. Are they the same?
    From program 1:
    Code:
    hold = array[compare];
    array[compare] = array[compare+1];
    array[compare+1] = array[compare]; // Check this line out.
    From program 2:
    Code:
    hold = array[compare];
    array[compare]=array[compare+1];
    array[compare+1]=hold; // Is this line the same?
    Jim

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Upss... Well. I don't know how I couldn't see it. Thank you so much for your responding and your affine , Jim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. Replies: 2
    Last Post: 03-13-2008, 11:34 AM
  3. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread