1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
CurlyKid
  • 13
  • 1
  • 2
  • 7
  • 2
    The first number is the number of items. You need to store that and use it for the loop comparison. – Jerry Jeremiah May 15 '20 at 00:56
  • 1
    for (i = MAX_ELEMENTS - 1; i >= 1; --i should actually be for (i = MAX_ELEMENTS - 1; i >= 0; --i or you won't get all the numbers if you were to have 20 int in the array – Fabrizio Botalla May 15 '20 at 01:03
  • 2
    You can't enter less than `MAX_ELEMENTS` elements. In your example it seems like you entered only 5 out of 20 numbers, and printed 20 numbers (that 15 of them are garbage uninitialized values). Please enter 20 numbers or change `MAX_ELEMENTS` to 5. Share the result. – Coral Kashri May 15 '20 at 01:12
  • [reverse iterators](https://en.cppreference.com/w/cpp/iterator/reverse_iterator) are your friends in this case :-) See [`std::rbegin()`](https://en.cppreference.com/w/cpp/iterator/rbegin) and [`std::rend()`](https://en.cppreference.com/w/cpp/iterator/rend). Or change `int userVals[MAX_ELEMENTS]` to `std::array userVals;` and then use [`userVals.rbegin()`](https://en.cppreference.com/w/cpp/container/array/rbegin) and [`userVals.rend()`](https://en.cppreference.com/w/cpp/container/array/rend). – Remy Lebeau May 15 '20 at 01:21
  • your code works for me, after changing `i>=1` to `i>=0` in the second `for` loop. – NAND May 15 '20 at 01:25
  • @NAND not if you continue using `MAX_ELEMENTS` as the loop counter – Remy Lebeau May 15 '20 at 01:34
  • Since people, reasonably often, find it easier to reason about a loop that runs forward, an alternative for the second loop can be `for (i = 0; i < MAX_ELEMENTS; ++i) {cout << userVals[MAX_ELEMENTS-1-i] << " ";}` - so the loop runs forward (incrementing `i`), but accesses elements in reverse. Also, the code does not actually read the number of values to follow, so doesn't behave as required. – Peter May 15 '20 at 02:13

1 Answers1

1

You are not following the instructions you were given, in particular this sentence:

The input begins with an integer indicating the number of integers that follow.

As you surmised, you are indeed reading and printing out exactly 20 values, even if the user actually enters fewer values (and the instructions provide). The 1st number given specifies the count of subsequent numbers. Read that number first and store it in a variable that you can then use to control your loops.

And speaking the loops, your output loop is skipping the 1st element of the array. The loop condition needs to be i >= 0 instead of i >= 1.

Try this instead:

#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, count; // <-- ADD count!

  cin >> count; // <-- ADD THIS!

  for (i = 0; i < count; ++i) { // <-- use count, not MAX_ELEMENTS!
    cin >> userVals[i];
  }

  for (i = count - 1; i >= 0; --i) { // <-- use count, not MAX_ELEMENTS!
    cout << userVals[i] << " ";
  }

  cout << endl;
  return 0;
}

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770