The program will continuously scan numbers into an array, where the array will be no larger than 100 values.
However, the counter in the first while loop, 'i', continues to count up to 99 despite the program exiting after the 3rd value is entered. Thus, when the second while loop is initiated, it prints the values starting from 99.
How could you make the counter stop when the loop is exited?
This is a homework assignment and the first time touching arrays in C.
I have already tried using an if statement to exclude the zeros for all array values not necessary but sometimes 0 can be entered into the array and needs to be printed.
#include <stdio.h>
int main(void) {
printf("Enter numbers forwards:\n");
int numbers[99] = {0};
// Components of the scanning while loop
int i = 0;
while (i <= 98) {
scanf("%d", &numbers[i]);
i = i + 1;
}
// Components of while loop
int counter = i - 1;
printf("Reversed:\n");
while (counter >= 0) {
printf("%d\n", numbers[counter]);
counter--;
/*if (numbers[counter] == 0) {
counter--;
} else {
printf("%d\n", numbers[counter]);
counter--;
}*/
}
Expected Results: Enter numbers forwards: 10 20 30 40 50 CTRL-D Reversed: 50 40 30 20 10
Actual Results: Enter numbers forwards: 10 20 30 40 50 CTRL-D Reversed: 0 0 0 ... 50 40 30 20 10