1. ## Arrays

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.

2. What do you mean by "won't work"? Explain what you expect to have happen, and what does or doesn't happen.

Quzah.

3. Maybe just a posting oversight, but you are missing a closing curly brace on the for loop.

4. Why are you breaking when k is zero? Since you never modify k, it will always be zero the first time you check it.

5. I mean it won't sort the numbers at all. It just kind of skips around.
I think all the curly braces are in there, actually.

6. apparently this
Code:
```if (k == 0)
break ;```
was left over from a bad copy paste.. I took it out and didn't affect anything.

7. IT WORKS! I've been messing around with this for 5 hours and it finally works.

Thank you all very much. Here's what the code is supposed to look like:

Code:
```// asc array.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "stdafx.h"
#include "conio.h"

int
main ()
{
int a[ 5 ]; /* 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("Please enter %d data points\n", 5);

for (i = 0; i < 5; i++) {
printf("Enter data no. %d : ", i+1);
scanf("%d", &a[i]);}

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 ;
}
}
}

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;
}```

8. In this code snippet, the stack is defined as global variable, because it needs to be accessible to all other functions.