Array in reverse in C++

Level 398 keystrokes

The loop starts at the last index (4) and counts down to 0, so the array is printed in reverse: 5 4 3 2 1. The array itself is not changed.

C++ code

1int arr[] = {1, 2, 3, 4, 5};
// Starts at the last index (size 5, so 4) and decrements while i >= 0
2for (int i = 4; i >= 0; i--) {
// Prints arr[4], arr[3], … arr[0]: 5 4 3 2 1
3 cout << arr[i] << " ";
4}
// Ends the line after the loop
5cout << endl;

Output

5 4 3 2 1 
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 6
  • ;× 5
  • ,× 4
  • =× 3
  • -× 2
  • "× 2
  • [× 2
  • ]× 2
  • {× 2
  • }× 2

More C++ examples at level 3

← C++ typing practice