+ 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
1 Resposta
+ 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.