here is my bubblesort code:
what i can't understand is why main only calls printf ("Enter your numbers:\n") after the for loop and not before it as it is so in the code. any suggestions? also any ideas on how to re-declare N so it is not a global variable?Code:#include <stdio.h> int N; void swap(int *p, int *q) { int tmp; tmp = *p; *p = *q; *q = tmp; } void bubble (int a[N]) { int i, j; for (i = 0; i < N; i++) for (j = N - 1; i < j; j--) if (a[j - 1] > a[j]) swap(&a[j - 1], &a[j]); } int main (void) { int i; int a[N]; printf ("Enter the amount of numbers to be sorted: "); scanf ("%d\n", &N); printf ("Enter your numbers:\n"); for (i = 0; i < N; i++){ scanf (" %d", &a[i]);} bubble (a); for (i = 0; i < N; i++) printf("%d ", a[i]); printf("\n"); printf("\n"); return 0; }



LinkBack URL
About LinkBacks



