DSA- Reverse an array

Kapil Patel
May 20, 2023

--

input array arr[] = {10, 20, 30, 50, 15, 16};

Expected output: 16 15 50 30 20 10


#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int main() {

//reverse an array
//int arr[] = {10, 20, 30, 50, 15, 16}; // even input
int arr[] = {10, 20, 30, 50, 15};//odd input
int arrLength = sizeof(arr) / sizeof(int);
int start = 0;
int end = arrLength - 1;
int swapHolder;
while (start < end) {
swapHolder = arr[end];
arr[end] = arr[start];
arr[start] = swapHolder;
start++;
end--;
}
for (int i = 0; i < arrLength; i++) {
cout << arr[i] << " ";
}
}

--

--

Kapil Patel
Kapil Patel

Written by Kapil Patel

Software engineer | loves working in a startup like environment

No responses yet