I am doing an bubble sort assignment, the goal is to enter up to a max of 10 numbers and sort them. The problem that I am running into is when I run the code, I have to hit ^D to have the next line of the array displayed and I am getting a negative number sometimes displayed which is not one of the numbers I entered. Basically what I am asking is there something wrong with my main method as I believe my sort method is correct. Thanks in advance.
#include <stdio.h>
#define MAXIMUM 10
void sort(int a[], int n)
{
int j, jj;
int t;
for(j=0; j<(n-1); j++) /* outer loops and counts passes */
{ for(jj=0; jj< (n-(1-j)); jj++) /* inner loop and compares adjenct items */
{ if (a[jj] > a[jj+1]) /* out of order */
{
t= a[jj];
a[jj] = a[jj+1];
a[jj + 1] = t;
}
}
}
}
int main()
{
int a[MAXIMUM];
int j; /* loops */
int x; /* get data */
int cnt = 0; /* actually count */
printf("enter number or -1 to quit");
scanf(" %d" , &x); /* into x */
while( x != -1 && cnt < MAXIMUM)
{
a[cnt++] = x; /* put data into array */
printf("enter number or -1 to quit");
scanf("%d",&x);
}
printf("number of items entered %d \n", cnt);
for (j=0; j < cnt; j++)
{
printf("%d %d\n",j, a[j]);
}
if (cnt > 0)
{
sort(a, cnt);
printf("sorted list \n");
}
for (j =0; j < cnt; j++)
{
printf("%d %d\n", j, a[j]);
if (cnt = MAXIMUM)
printf("full-overflow\n");
else printf("enter a number or -1 to quit");
scanf("%d", &x);
}
}