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; +}