My call by reference bubble sort code isn't displaying the correct output

This is a discussion on My call by reference bubble sort code isn't displaying the correct output within the C Programming forums, part of the General Programming Boards category; The program compiles, however the output is incorrect Output: Code: Input size of array5 Input elements3 5 4 7 8 ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    25

    My call by reference bubble sort code isn't displaying the correct output

    The program compiles, however the output is incorrect

    Output:
    Code:
    Input size of array5
    Input elements3
    5
    4
    7
    8
    Enter 1 to sort in ascending order,
    Enter 2 to sort in descending order: 1
    
    Data items in ascending order
       0327673276716064169201606420320
    logout
    
    [Process completed]
    Code:
    /* This program puts values into an array, sorts the values into
     ascending order, and prints the resulting array. */
    #include <stdio.h>
    
    void sortascending( int * const array, const int size ); /* prototype */
    void sortdescending( int * const array, const int size );
    void swap( int *element1Ptr, int *element2Ptr ); /* prototype */
    
    int main( void )
    {
    	
    	int i; /* counter */
    	int order;
    	/* initialize array a */
    	int SIZE;
    	int a[ SIZE ];
    	printf("Input size of array");
    	scanf("%d", &SIZE);
    	printf("Input elements");
    	for (i=0; i<SIZE; i++) {
    		scanf("%d", &a[SIZE]);
    	}
    	
    	printf( "Enter 1 to sort in ascending order,\n" 
               "Enter 2 to sort in descending order: " );
    	scanf( "%d",  &order );
    	
    	/* sort array in ascending order; pass function ascending as an
         argument to specify ascending sorting order */
    	switch (order) {
    		case 1:
    			sortascending( a, SIZE);
    			printf( "\nData items in ascending order\n" );
    			/* loop through array a */
    			for ( i = 0; i < SIZE; i++ ) {
    				printf( "%4d", a[ i ] );   
    			} /* end for */
    			printf( "\n" );
    			break;
    		case 2:
    			sortdescending( a, SIZE);
    			printf( "\nData items in descending order\n" );
    			/* loop through array a */
    			for ( i = 0; i < SIZE; i++ ) {
    				printf( "%4d", a[ i ] );   
    			} /* end for */
    			printf( "\n" );
    			break;
    		default:
    			break;
    			return 0; /* indicates successful termination */
    	} /* end main */
    }
    
    /* sort an array of integers using bubble sort algorithm */
    void sortascending( int * const array, const int size )
    {
    	int pass; /* pass counter */
    	int j;    /* comparison counter */
    	
    	/* loop to control passes */
    	for ( pass = 0; pass < size - 1; pass++ ) {
    		
    		/* loop to control comparisons during each pass */
    		for ( j = 0; j < size - 1; j++ ) {
    			
    			/* swap adjacent elements if they are out of order */
    			if ( array[ j ] > array[ j + 1 ] ) {
    				swap( &array[ j ], &array[ j + 1 ] );
    			} /* end if */
    			
    		} /* end inner for */
    		
    	} /* end outer for */
    	
    } /* end function bubbleSort */
    
    /* sort an array of integers using bubble sort algorithm */
    void sortdescending( int * const array, const int size )
    {
    	int pass; /* pass counter */
    	int j;    /* comparison counter */
    	
    	/* loop to control passes */
    	for ( pass = 0; pass < size - 1; pass++ ) {
    		
    		/* loop to control comparisons during each pass */
    		for ( j = 0; j < size - 1; j++ ) {
    			
    			/* swap adjacent elements if they are out of order */
    			if ( array[ j ] < array[ j + 1 ] ) {
    				swap( &array[ j ], &array[ j + 1 ] );
    			} /* end if */
    			
    		} 			
    	} 
    	
    }
    /* swap values at memory locations to which element1Ptr and
     element2Ptr point */
    void swap( int *element1Ptr, int *element2Ptr )
    {
    	int hold = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = hold;
    } /* end function swap */
    Last edited by dsured; 02-09-2011 at 06:31 PM. Reason: edit code

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to tell the program what the value of SIZE is, before you declare the array to be of SIZE elements.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    You're not reading the items into valid positions within the array, to begin with.
    Look at your scanf.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    int main( void )
    {
    	
    	int i; /* counter */
    	int order;
    	/* initialize array a */
    	int SIZE;
    	printf("Input size of array");
    	scanf("%d", &SIZE);
                    int a[ SIZE ];	
                    printf("Input elements");
    	for (i=0; i<SIZE; i++) {
    		scanf("%d", &a[SIZE]);
    	}
                    ...
    You declared the array with SIZE elements before you assigned a value to SIZE.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    If you declare an array as
    Code:
    int array[SIZE]
    The the value of SIZE needs to be known at compile time.

    If you want to read SIZE in from the use during program execution, you have to allocate the array dynamically:
    Code:
    ..read in value for SIZE
    int *array;
    if ((array=malloc(SIZE*sizeof(int)))==NULL)
    {
        printf("Array allocation failed\n");
        abort();
    }
    C is fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Help with Bi-Directional Bubble Sort in C
    By cunninglinguist in forum C Programming
    Replies: 0
    Last Post: 04-19-2002, 02:32 PM
  4. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM

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