From 3fd7a9d4cd6e515d52a8a3ee04d3733dcbee4a58 Mon Sep 17 00:00:00 2001 From: Ritvik Garg <70766301+ritvik-garg@users.noreply.github.com> Date: Thu, 21 Oct 2021 19:03:22 +0530 Subject: [PATCH] Create reverseArray.cpp --- .../Arrays/reverseArray.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 dsa-roadmaps/Love Babbar Questions/Arrays/reverseArray.cpp diff --git a/dsa-roadmaps/Love Babbar Questions/Arrays/reverseArray.cpp b/dsa-roadmaps/Love Babbar Questions/Arrays/reverseArray.cpp new file mode 100644 index 0000000..0160352 --- /dev/null +++ b/dsa-roadmaps/Love Babbar Questions/Arrays/reverseArray.cpp @@ -0,0 +1,46 @@ +// Recursive C++ program to reverse an array +#include +using namespace std; + +/* Function to reverse arr[] from start to end*/ +void rvereseArray(int arr[], int start, int end) +{ + if (start >= end) + return; + + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + + // Recursive Function calling + rvereseArray(arr, start + 1, end - 1); +} + + +/* Utility function to print an array */ +void printArray(int arr[], int size) +{ +for (int i = 0; i < size; i++) +cout << arr[i] << " "; + +cout << endl; +} + +/* Driver function to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6}; + + // To print original array + printArray(arr, 6); + + // Function calling + rvereseArray(arr, 0, 5); + + cout << "Reversed array is" << endl; + + // To print the Reversed array + printArray(arr, 6); + + return 0; +}