-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_21.cpp
78 lines (62 loc) · 1.16 KB
/
question_21.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// // swap alternate value of array.
// // arr = [1,2,3,4,5,6] ---> [2,1,4,3,6,5]
#include<iostream>
using namespace std;
int swapAlter(int arr [], int size)
{
int st = 0;
int en = 1;
while(st <= en)
{
swap(arr[st],arr[en]);
st = st + 2;
en = en + 2;
}
}
int arrPrint(int arr [], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout<< endl;
}
int main()
{
int n;
cout<<"Enter the size of an array : "<< " ";
cin>>n;
int arr[100];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
swapAlter(arr, n);
arrPrint( arr, n);
}
// Another algorithm to solve this
// #include<iostream>
// using namespace std;
// int main()
// {
// int n;
// cout << "Enter the size : ";
// cin >> n;
// int arr[100];
// for(int i = 0; i < n; i++)
// {
// cin >> arr[i];
// }
// for(int i = 0; i < n; i = i+2)
// {
// if(i + 1 < n)
// {
// swap(arr[i],arr[i+1]);
// }
// }
// cout << endl;
// for(int i = 0; i < n; i++)
// {
// cout << arr[i] << " ";
// }
// }