gosh, but that code looks familiar! Anyway:
Loop 101, here:
Code:
while ( (scanf("%d", &array[i]) == 1) && (i<MAX) ) {
scanf("%d", &array[i]); //there's an echo around here!
i++;
}
//what is the value of i, right here?
while (i>=0) {
printf("%d ", array[i]); //i is one number too high, and out of bounds
i--; //on the first time thru this loop
}
To fix the i problem:
Code:
while (i>0) { //change the test condition, just a bit by removing the =
printf("%d ", array[--i]); //decrement i first, before you access arra[i]
//i--; <===== don't need this anymore
}
Loops that have a comparison test, will run one number past the stop test point. Say you were looping from 0 to 9, in a for loop:
Code:
for(i=0;i<10;i++) {
printf("%d", i);
}
printf("%d", i); //10 will be printed here, not 9
Try that out.