+ 1
How to swap the adjacent elements in a 1D array?
if 6 elements are there of an array a[i] and; a[0]=0 a[1]=1 . . . a[5]=5 so I want to swap the elements of array and print it like 1,0,3,2,5,4 (the entry was: 0,1,2,3,4,5)
1 Answer
+ 4
Use this :
#include <algorithm>
It has a function that allows you to swap to values/elements .
After you include that, you can try this in your main statement :
int arr[]={1, 2};
cout<<arr[0]<<" "<<arr[1]<<endl;
swap(arr[0], arr[1]);
cout<<arr[0]<<" "<<arr[1];
You will see the result.
Basically the function is provides is this :
swap(a,b);
What is does is, it will swap the values of a and b.