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.

#include <stdio.h>
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

line 11: <=9 should be just <9 because there is no 9th element in that array.

line 13: should be printf("please input numbers\n"); no need for "%s"

line 14: "%s" is fof strings, "%d" or "%i" is for integers.

line 16: should be <8 not <=8, you want to run the loop 1 less than the number of elements in the array. Since there are 9 eledments in the array the loop should run 8 times (0, 1, 2, 3, 4, 5, 6, and 7)

line 18: should be <9, not <=9, for the same reason as that given for line 11 above.

Edited 3 Years Ago by Ancient Dragon

Comments
Made my post redundant ;-)

In C, a semi-colon marks the end of an expression. On these and other expressions, your semi-colon has cut them off - and you need to remove them.

for ( i=0 ; i<=8 ; i++ )**;**
{
for (j=i+1;j<=9;j++)**;**
{
int temp;
if(array[i]>array[j])**;**
{

thanks all for your reply, i would try it Dev C++ tonight. (HK time is in the afternoon :D)

This article has been dead for over six months. Start a new discussion instead.