Magazine

How to Reverse an Array in C++?

Posted on the 12 May 2021 by Amit Kumar @buddymantra

In today’s fast – paced digital era, having a good command over programming languages proves to be quite beneficial for an individual. C++ is one such high – in – demand language, which is having a quite crucial value. In this article, you’ll be learning how you can reverse an Array in C++. Given an array arr[] of elements (integers) Array arr[] : 1, 5, 7, 8, 9, 2, 3, 11 After reversal, the new array arr[] will be   Array arr[]:   11, 3, 2, 9, 8, 7, 5, 1 Approach Consider 2 pointers start and end. Start will initially point to beginning of the array and end will point to end of the array. Swap the elements of the start and end positions. Increment the start pointer and decrement the end pointer. Repeat the process until start is greater or equal to  the end pointer. Output: Before Reversal1 4 3 5 7 8 9 8 2 6After Reversal6 2 8 9 8 7 5 3 4 1 Also Read Introduction to C++ STL Basic Operations on Matrices in C++ Basic Operations on Matrices in C++ Five Ways to Calculate Power in C++


Back to Featured Articles on Logo Paperblog