This program is an example of an array reversal in C++. It declares an integer array called “numbers” and initializes it with the values 1 to 7. Then it uses two for loops to print the array and its reverse. In the first for loop, it uses the range-based loop to iterate over the “numbers” array and print each element. In the second for loop, it starts at the end of the array and goes backwards, printing each element. The program ends with a return statement, indicating that it ran successfully.
#include <iostream>
using namespace std;
int main () {
int numbers[] = {1, 2, 3, 4, 5, 6, 7};
int arrayLength;
arrayLength = end(numbers) - begin(numbers);
// print array
for (int i = 0; i < arrayLength; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// print array in reverse order
for (int i = arrayLength - 1; i >= 0; i--) {
cout << numbers[i] << " ";
}
return 0;
}
1 2 3 4 5 6 7
7 6 5 4 3 2 1