We've already made listing an array the easy way, but how about listing and printing the array in reverse order?
Make a program that will input an integer and then using loops, add items on an array/list one by one for the same number of times as that of the first inputted integer. Then, print out the array/list in ascending order. After doing that, print the array again on the next line in reverse order, that is, starting from the last item on the array/list down to the first one, each in separated lines.
Input
The first line contains the size of the array.
The next lines contains the items of the array (integers).
5
1
64
32
2
11
Output
The first line contains the elements of the array printed in ascending order.
The second line contains the elements of the array printed in reversed order.
1·64·32·2·11
11·2·32·64·1
#include <iostream>
using namespace std;
int main() {
int *arr;
int n;
cin >> n;
arr = new int[n];
for (int i=0; i<n; i++) {
cin >> arr[i];
}
for (int i=0; i<n; i++) {
cout << arr[i] << " ";
}
cout << endl;
for (int i=n-1; i>=0; i--) {
cout << arr[i] << " ";
}
delete [] arr;
return 0;
}
Comments
Leave a comment