do-while loop doesn't work
for some reason my do-while implementation doesn't work. here is my code:
Code:
#include <stdio.h>
char getAns (char mesg[])
{
char ans;
printf ("%s", mesg);
ans = getchar();
getchar();
return toupper(ans);
}
void swap(int *p, int *q)
{
int tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
void bubble (int a[], int 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)
{
char ans;
int i, N;
int a[N + 1];
do
{
printf ("Enter the amount of numbers to be sorted: ");
scanf ("%d", &N);
printf("Now enter the numbers:\n");
for (i = 0; i < N; i++){
scanf ("%d", &a[i]);}
bubble (a, N);
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\nYour numbers were sorted using BubbleSort.\n");
ans = getAns ("\nSort again (Y/N) ?");
} while (ans == 'Y');
return 0;
}
thanks for any suggestions