Thread: Bubble sort error

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Bubble sort error

    Hiya im writing a program that reads 20 floats into an array then sorts
    them using bubble sort and prints them out in assending order.

    But I am getting an error that says variable 'i' is undecared. ( I have marked the line with the error ). I delcared 'i' before the program inside main so I do not understand why it doesnt see that I have delcared it.

    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 21
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       /*declare array and size*/
       float data[ ARRAY_SIZE ];
       int temp;
       int i;
    
       printf("Enter 20 floats: ");
    
       for ( i = 0; i < ARRAY_SIZE; i++ )
       {
          scanf("%f", &data[ i ]);
       }
    
       /*sort array with bubble sort*/
       for ( i = 0; i < ARRAY_SIZE -1; i++ )
       {
          for ( j = 0; j < ARRAY_SIZE -1 -i; j++ ) // line with error
             if ( data[ j + 1 ] < data[ j ] )
             {
                int tmp = data[ j ];
                data[ j ] = data[ j +1 ];
                data[ j + 1 ] = tmp;
             }
       }
    
       /*display sorted array*/
       for ( i = 0; i < ARRAY_SIZE; i++ )
       {
          printf("%.2f", data[ i ]);
       }
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    Double Helix STL

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It says j is undeclared

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Sorry I must need my eyes tested I could of sworn it was an i and not a j. Cheers mike much appreiciated
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM