-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReversing Array C++
31 lines (27 loc) · 954 Bytes
/
Reversing Array C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int main ()
{
int arr[50], num, temp, i, j;
cout << " Please, enter the total no. you want to enter: ";
cin >> num;
// use for loop to enter the numbers
for (i = 0; i < num; i++)
{
cout << " Enter the element " << i+1 << ": ";
cin >> arr[i];
}
for ( i = 0, j = num - 1; i < num/2; i++, j--)
{
temp = arr[i];
arr[i] = arr[j]; // using loop to swap
arr[j] = temp;
}
cout << "\n Reverse all elements of the array: " << endl;
// use for loop to print the reverse array
for ( i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
return 0;
}