Thread: Bubble Sort In C

  1. #1
    Registered User kw42chan's Avatar
    Join Date
    Jan 2013
    Location
    Hong Kong, China
    Posts
    18

    Bubble Sort In C

    Dear all
    I write bubble sort program for inputing 9 numbers. However, when compile and run it, it only allows me to input one number only. Could anyone see any fault in my code? I think the problem is on the first loop.

    Code:
    int main(void)
    {
        int num1=0;
        int array[9];
        int i = 0;
        int j = 0;
        
    for (i=0; i<=9;i++);
        {    
            printf("%s",&"please input numbers\n");
            scanf("%s",&array[i]);
        }    
    for ( i=0 ; i<=8 ; i++ );
        {
        for (j=i+1;j<=9;j++);
        {
            int temp;
            if(array[i]>array[j]);
            {
                temp = array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
    
    
    
    
    }//end


  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You have extra semicolons at the end of all your condition statements.

    Jim

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What Jim means is that
    Code:
    for (i=0; i<=9;i++);
        {    
            ...
    should be
    Code:
    for (i=0; i<=9;i++)
        {    
                        ....
    Moreover, after you make sure you delete all extra semicolons (like the one above), replace your printf with just this
    Code:
            printf("please input numbers\n");
    and in the next line of code
    you use the wrong format. %s is for strings. What you want to store is numerical data and because they are integers, you should use %d.
    So replace your scanf with this
    Code:
            scanf("%d",&array[i]);
        }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User kw42chan's Avatar
    Join Date
    Jan 2013
    Location
    Hong Kong, China
    Posts
    18
    Thanks all for the reply, i would try it Dev C++ tonight. (HK time is in the afternoon )

  5. #5
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    why u using string for reading or printing an variable?
    Code:
    int main(void) 
    {     int num1=0;     int array[9];     int i = 0;     int j = 0;     
     for (i=0; i<=9;i++);     
    {    printf("%s",&"please input numbers\n");         
          scanf("%s",&array[i]);     
    }
    "%s" <- for string
    "%d" <- for int
    thats funny when i am looking your code,
    but it seems this problem is fixed by std10093
    ,
    Last edited by loserone+_+; 01-07-2013 at 02:12 AM. Reason: wrong code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort
    By nynicue in forum C Programming
    Replies: 7
    Last Post: 04-15-2009, 05:09 AM
  2. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  3. Bubble Sort
    By bmx4christ in forum C Programming
    Replies: 3
    Last Post: 12-01-2003, 08:34 PM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Bubble Sort
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-28-2001, 08:19 AM