+ 3

Arr manipulatiom

sĂł i AM trying to inverte an array but for some reason i just cant find a way to, i have put my Code here https://code.sololearn.com/cotSN19mXH45/?ref=app

5th Feb 2018, 7:59 PM
diogo Neutel
diogo Neutel - avatar
1 Answer
+ 1
Here is the corrected code. #include <iostream> using namespace std; int main() { int n; cin >> n; int space = n; // Not n-1,as that would have // 1 element less. int arr[space]; int arr_[space]; int y = n-1; // Last index is n-1 int x; for(int i = 0;i < n; i++){ cin >> x; arr[i] = x; } for(int j = 0;j < n; j++){ arr_[y]=arr[j]; // Assign old to new,not vice versa. // arr_[y] has no values. arr[j] does. y--; } for(int k = 0;k < n; k++){ cout << arr_[k] << " "; } } But kindly note that initializing arrays with variable sizes is not a good practice. You should use pointers for that or have a constant size of like 50, 100, etc, but store only till n/space.
6th Feb 2018, 3:36 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar