-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayElementSwaps.C++
More file actions
47 lines (36 loc) · 1.31 KB
/
ArrayElementSwaps.C++
File metadata and controls
47 lines (36 loc) · 1.31 KB
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
#include <iostream>
#include <cmath>
using namespace std;
void ArrayPrinter(int array[] , int size){
for(int i = 0 ; i<size ; i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
void ArrayExchange(int array[] , int initial , int next , int size){
int CopiedArray[size] ={};
for(int i = 0 ; i <size ; i++){
CopiedArray[i] = array[i];
}
array[initial] = CopiedArray[next];
array[next] = CopiedArray[initial];
/*These two lines could have been in one line in case of python
and we would have not been required to copy the array , Elements would have been exchanged automatically in one statement ,
but here if we do so no change is seen in the array.
-->In python arrays are implemented through lists. */
ArrayPrinter(array , size);
}
int main(){
int array[5] = {54,78,34,43,23};
int size = sizeof(array)/4;
int initial;
int next;
cout<<"Please enter the indices you want to exchange each other with. [ 0 t o 4 o n l y ]"<<endl;
cin>>initial;
cin>>next;
cout<<"Initially your array is this -:";
ArrayPrinter(array , size);
cout<<endl<<"Finally , after exchange Your array is this -:";
ArrayExchange(array , initial , next , size);cout<<endl;
return 0 ;
}