tracing

This is a discussion on tracing within the C Programming forums, part of the General Programming Boards category; Below i have implemented a bubble sort function but i need help in making the bubble sort having a trace ...

  1. #1
    cman
    Guest

    tracing

    Below i have implemented a bubble sort function but i need help in making the bubble sort having a trace function. I just want to print out each pass of the sort until it is finally sorted.

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    int main()
    {
    	int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
    	int i, pass, hold;
    
    	printf( "Data items in original order\n" );
    
    	for ( i = 0; i <= SIZE - 1; i++ )
    		printf( "%4d", a[ i ] );
    
    	for ( pass = 1; pass <= SIZE - 1; pass++ )
    		for( i = 0; i <= SIZE - 2; i++ )
    		
    		if ( a[ i ] > a[ i + 1 ] ) {
    		hold = a[ i ];		/* store the value */
    		a[ i ] = a[ i + 1 ];	/* assign a[i] the a[i+1] value */
    		a[ i + 1 ] = hold;	/* assign a[i+1] the hold value */
    	}	
    		
    	printf( "\nData items in ascending order\n" );
    
    	for ( i = 0; i <= SIZE - 1; i++ )
    		printf( "%4d", a[ i ] );
    
    	printf( "\n" );
    
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I just want to print out each pass of the sort until it is finally sorted.

    So...put printf() in the appropriate place then
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    cman
    Guest
    hehe good idea
    Thanks.

    its done

  4. #4
    cman
    Guest
    one last thing,

    with my comments for the swap, just after the if statement, is that the correct idea?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by cman
    with my comments for the swap, just after the if statement, is that the correct idea?
    Is what the correct idea? If you mean putting comments in your code, try to comment the "why" and not so much the "how". We can see you're assigning stuff, that obvious from the code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tracing execution with Ptrace
    By tommyb05 in forum C Programming
    Replies: 9
    Last Post: 05-20-2009, 10:51 AM
  2. what is the best method of tracing this code on paper..
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 04-10-2009, 12:46 PM
  3. Tips in tracing C Codes
    By mark25 in forum C Programming
    Replies: 5
    Last Post: 11-08-2008, 10:02 AM
  4. String Pointer Tracing
    By bobby19 in forum C Programming
    Replies: 8
    Last Post: 04-13-2006, 01:22 AM
  5. tracing pointers
    By makimura in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2001, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21