Thread: Flowchart Question

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    Flowchart Question

    Hello Everyone,

    I am trying to flowchart the following code but I am getting stuck on the nested loops. Can someone help me understand how the code flows.

    Code:
    #include<stdio.h>
    #define SIZE 10
    
    int main()
    {
        
        int a[ SIZE ] = { 10, 9, 8, 7 , 6, 5, 4, 3, 2, 1 }; /* Use initializer list to initialize array a */
        int counter; /* Variable in which counter will be stored */
        int pass; /* Variable in which pass will be stored */
        int hold; /* Variable in which hold will be stored */
        int swap; /* Variable in which swap will be stored */
        
        printf( "Data items in original order\n" );
        
        /* Output original array */
        for( counter = 0; counter < SIZE; counter ++ ) {
            printf( "%4d", a[ counter ] );
        } /* End for */
        
        printf( "\n\n" );
        
        /* Bubble sort */
        for( pass = 1; pass < SIZE; pass ++ ) {
        
            swap = 0;
            
            /* Loop to control number of comparisions per pass */
            for( counter = 0; counter < SIZE - 1; counter ++ ) {
            
                    /* Compare adjacent elements and swap them if first
                       element is greater than second element */
                    if( a[ counter ] > a[ counter + 1 ] ) {
                       swap = 1;
                       hold = a[ counter ];
                       a[ counter ] = a[ counter + 1 ];
                       a[ counter + 1 ] = hold;
                    } /* End if */
            } /* End for */
                            
            printf( "Pass %d: ", pass );
            
            /* Loop to control if a swap has been made per pass */
            for( counter = 0; counter <= SIZE - pass; counter ++ )
                    printf( " %d", a[ counter ]);
                    printf( "\n" );
                    if( swap != 1 )
                          break;
            
        } /* End for */
        
        printf( "\nData items in ascending order\n" );
        
        /* Output sort array */
        for( counter = 0; counter < SIZE; counter ++ ) {
            printf( "%4d", a[ counter ] );
        } /* End for */
        
        printf( "\n" );
        
        return 0; /* Indicates successful termination */
        
    }
    Thanks in advance

    DMKanz07

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well it's not exactly a bubble sort to begin with.

    Code:
    for ( sorted = SIZE-1 ; sorted > 0 ; sorted-- ) {
      for ( toSort = 0 ; toSort < sorted ; toSort++ ) {
        // compare a[sorted] with a[toSort] and swap
      }
      // at this point, you can say a[sorted] is in the correct position
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Flowchart question: Classes
    By darnok in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 06:25 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM