+ 2

Why it's showing error ? How to reverse an array elements?

#include <iostream> using namespace std; int main() { int n; int a[n]; cout<<"enter a number"; cin>>n; for(int i=0;i<n;i++) {cin>>a[i]; } for(int i=n-1;i>=0;i--) {cin>>a[i]; cout<<a[i]; } return 0; }

27th Feb 2022, 3:43 AM
Diya
3 Respostas
+ 3
Diya Why you are taking input inside 2nd loop when you have taken inside 1st loop. int a[n] should be declare after taking n input ------+++------ #include <iostream> using namespace std; int main() { int n; cout << "enter a number" << endl; cin >> n; int a[n]; for(int i = 0; i < n; i++) { cin >> a[i]; } for(int i = n - 1; i >= 0; i--) { cout << a[i] << " "; } return 0; }
27th Feb 2022, 3:52 AM
A͢J
A͢J - avatar
+ 1
Now I got it thanks
27th Feb 2022, 4:01 AM
Diya