-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathswapalternatively.cpp
More file actions
50 lines (44 loc) · 905 Bytes
/
swapalternatively.cpp
File metadata and controls
50 lines (44 loc) · 905 Bytes
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
#include<iostream>
using namespace std;
void inputarray( int arr[],int size){
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}
}
void printarray( int arr[],int size){
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int arr[100];
int size;
cout<<"ENTER THE NUMBER OF ELEMENTS IN ARRAY!!"<<endl;
cin>>size;
cout<<"TAKING INPUT"<<endl;
inputarray(arr,size);
cout<<"ARRAY BEFORE ALTERNATIVE SWAP :-"<<endl;
printarray(arr,size);
cout<<"ALTERNATIVELY SWAP ARRAY :-"<<endl;
if(size%2 == 0){
for (int i = 0; i < size; i=i+2)
{
if(i<size){
swap(arr[i],arr[i+1]);
}
}}
else{
for (int i = 0; i < size-1; i=i+2)
{
if(i<size){
swap(arr[i],arr[i+1]);
}
}
}
printarray(arr,size);
return 0;
}