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 */



LinkBack URL
About LinkBacks



