Question: Write a program that takes an input array of integers and prints the elements in reverse order.
Requirements:
printReverse
that takes two parameters: an integer array (arr
) and its size (size
).Example Output:
Enter the size of the array: 5
Enter the elements of the array:
Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
The elements in reverse order are:
5 4 3 2 1
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main() {
vector<int> arr;
int sizeOfArray;
int num;
cout << "Enter the size of the array: ";
cin >> sizeOfArray;
for(int i = 0; i < sizeOfArray; i++){
cout << "Element " << i <<": ";
cin >> num;
arr.push_back(num);
}
reverse(arr.begin(), arr.end());
cout << "The elements in reverse order are: ";
for(int i = 0; i < sizeOfArray; i++){
cout << arr[i] << " ";
}
}