Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain less than 20 integers.
Input : 5 2 4 6 8 10
Expected output: 10 8 6 4 2
My output is:
0 4196464 0 4196944 0 0 0 0 0 4197021 0 2 0 4196929 10 8 6 4 2
I have the answer at the very end, but I don't know how to get rid of the numbers in front. I'm guessing that its looping 20 times and that's why my answer is weird. I changed my max to fit the number of input but that's cheating for my assignment.
My Code:
#include <iostream>
using namespace std;
int main() {
const int MAX_ELEMENTS = 20; // Number of input integers
int userVals[MAX_ELEMENTS]; // Array to hold the user's input integers
int i;
for (i = 0; i < MAX_ELEMENTS; ++i) {
cin >> userVals[i];
}
for (i = MAX_ELEMENTS - 1; i >= 1; --i) {
cout << userVals[i] << " ";
}
cout << endl;
return 0;
}