new and delete
Completed
Read n, allocate an array with new int[n], read the numbers, print them in reverse, then delete[] it.
Input
Line 1: n. Line 2: n integers.
Output
One line.
Example
Input
4 1 2 3 4
Output
4 3 2 1
Editor
C++
Hints
new[] must be paired with delete[].
Solution explanation
Manual new/delete is error-prone; modern C++ uses vector or smart pointers instead.
Show solution
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = n - 1; i >= 0; i--) cout << a[i] << (i > 0 ? " " : "\n");
delete[] a;
return 0;
}