I'm trying to sort an array of numbers input from the keyboard. I've done some searches on here, but nothing can point me in the right direction. Without defining the numbers, I can get the array to work, but when I put in the user-definition of the numbers, everything fails.
here's what I've got without user-defined numbers:
Code:
int main ( void )
{
int a[ 5 ] = {3, 1, 6, 25, 92}; /* declare and initialize array a */
int pass; /* passes counter */
int i; /* comparisons counter */
int hold; /* temporary location used to swap arrays elements */
int j = 0;
int k;
printf( "Data items in original order:\n\n" );
for ( i = 0; i < 5; i++ ){ /* display array output */
printf( "%4d", a[ i ] );
} /* end of for loop */
for (pass = 5 - 1 ; pass > 0 ; pass--) {
k = 0 ;
for (i = 0 ; i < pass ; i++) {
j++ ;
if (a[i] > a[i + 1]) {
hold = a[i] ;
a[i] = a[i + 1] ;
a[i + 1] = hold ;
}
}
if (k == 0)
break ;
}
printf( "\nData items in ascending order:\n\n" );
for ( i = 0; i < 5; i++ ){ /* output array in ascending order */
printf( "%4d", a[ i ] );
}
printf( "\n\n" );
getch();
return 0;
}
and here's the user-defined portion
Code:
printf(“Please enter %d data points\n”, 5);
for (i = 0; i < 5; i++) {
printf(“Enter data no. %d : ”, i+1);
scanf(“%d”, &a[i]);
in other words, this code works, but won't when I try to input numbers for the array.
Any help would be appreciated.