[Solved] Reverse Array C++? What am I doing wrong?


Change the condition to i >= 0, There’s a chance that n - 1 >= 0 might lead to an infinite loop. But it doesn’t matter whether it does or not, because in both cases, it’ll not produce the required results.

for(int i = n-1; i >= 0; i--){
   std::cout << " "<< arr[i] << " ";
}

Or use, std::reverse.

std::reverse(arr.begin(), arr.end());
for (int i = 0; i < n; i++){
    std::cout << arr[i] << " ";
}

See the second approach live here.

1

solved Reverse Array C++? What am I doing wrong?