Skip to content

Commit 06cd2e5

Browse files
authored
Merge pull request #5 from garud-dwar/garud-dwar-patch-1
Create Write a program to reverse an array or string
2 parents 9086f88 + 5c8af3c commit 06cd2e5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Iterative C++ program to reverse an array
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/* Function to reverse arr[] from start to end*/
6+
void rvereseArray(int arr[], int start, int end)
7+
{
8+
while (start < end)
9+
{
10+
int temp = arr[start];
11+
arr[start] = arr[end];
12+
arr[end] = temp;
13+
start++;
14+
end--;
15+
}
16+
}
17+
18+
/* Utility function to print an array */
19+
void printArray(int arr[], int size)
20+
{
21+
for (int i = 0; i < size; i++)
22+
cout << arr[i] << " ";
23+
24+
cout << endl;
25+
}
26+
27+
/* Driver function to test above functions */
28+
int main()
29+
{
30+
int arr[] = {1, 2, 3, 4, 5, 6};
31+
32+
int n = sizeof(arr) / sizeof(arr[0]);
33+
34+
// To print original array
35+
printArray(arr, n);
36+
37+
// Function calling
38+
rvereseArray(arr, 0, n-1);
39+
40+
cout << "Reversed array is" << endl;
41+
42+
// To print the Reversed array
43+
printArray(arr, n);
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)