From 69f11a2d0d7f5e62d71323fb7b3e31c0784b1e62 Mon Sep 17 00:00:00 2001 From: anshusharma8442 <72985068+anshusharma8442@users.noreply.github.com> Date: Wed, 21 Oct 2020 10:36:08 +0530 Subject: [PATCH] Create reverse_an_array --- reverse_an_array | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 reverse_an_array diff --git a/reverse_an_array b/reverse_an_array new file mode 100644 index 0000000..c34ee78 --- /dev/null +++ b/reverse_an_array @@ -0,0 +1,46 @@ +// Iterative 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) +{ + while (start < end) + { + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } +} + +/* 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}; + + int n = sizeof(arr) / sizeof(arr[0]); + + // To print original array + printArray(arr, n); + + // Function calling + rvereseArray(arr, 0, n-1); + + cout << "Reversed array is" << endl; + + // To print the Reversed array + printArray(arr, n); + + return 0; +}