Swap with temp variable is not giving any output but swap() is giving an desired output. I am trying to swap arr[i] with arr[arr[i] 1].
This code is giving the desired output
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n],i,temp;
for(i=0;i<n;i ) {
cin>>arr[i];
}
for(i=0;i<n;i ) {
swap(arr[i],arr[arr[i] 1]);
}
for(i=0;i<n;i ) {
cout<<arr[i]<<" ";
}
}
But with temp variable there's no output. Code is given below
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n],i,temp;
for(i=0;i<n;i ) {
cin>>arr[i];
}
for(i=0;i<n;i ) {
temp=arr[i];
arr[i]=arr[arr[i] 1];
arr[arr[i] 1]=temp;
}
for(i=0;i<n;i ) {
cout<<arr[i]<<" ";
}
}
I don't know what's the issue. I hope will get an response soon. Thank You!
CodePudding user response:
The different results is because std::swap does not use a temporary variable this way, it works slightly differently. Hence the different results.
To understand the different results consider the most simple case when the array has two values:
--- ---
arr: | 0 | 1 |
--- ---
arr[0] is 0 and arr[1] is 1. This can't be any more simpler, but let's just consider the only the initial swap, when i is 0:
std::swap(arr[i],arr[arr[i] 1]);
Let's evaluate both parameters when i is 0.
The first parameter to std::swap is, therefore arr[0].
The second parameter to std::swap is arr[arr[0] 1]. arr[0] is 0, so this becomes arr[0 1], or arr[1].
Conclusion: the first parameter to std::swap is arr[0], the second parameter is arr[1].
std::swap then proceeds and swaps arr[0] with arr[1]. The exact way it does this is not important, it's only important to understand that the two values get swapped as expected.
Now, let's walk through what happens, with the same arr, when a temporary variable is used. Let's do this, one step at a time:
temp=arr[i];
Since i is 0, this becomes temp=arr[0], so temp becomes 0.
arr[i]=arr[arr[i] 1];
i is still 0, so this becomes arr[0]=arr[arr[0] 1]. arr[0] is still 0, so this becomes arr[0]=arr[0 1], or arr[0]=arr[1].
The current contents of the array are now, at this point:
--- ---
arr: | 1 | 1 |
--- ---
And now, the final statement gets executed:
arr[arr[i] 1]=temp;
i is still 0. So, this becomes arr[arr[0] 1]=temp.
What it arr[0]? It's value is 1. So this becomes arr[1 1]=temp, or arr[2]=temp.
There is no arr[2]. Instead of swapping anything, the end result of the shown code is undefined behavior, memory corruption, and demons flying out of your nose.
Now, it's also possible to have undefined behavior using the std::swap version as well, depending on what's actually in the arr. This is only an explanation of why using a temporary variable is not the same as using std::swap, with all other things being equal.
CodePudding user response:
If your goal is to swap every element with the element next to it as you move through the array from 0 to n then all you need is: swap(arr[i], arr[i 1]); You will have to be sure to stop before i 1 is greater than the number of indexes in your array.
if(i 1 >= n) break;
CodePudding user response:
Source :
#include <iostream>
using namespace std;
//swap
void swap(int &a,int &b){
//cout<<"swap "<<a<<" "<<b<<endl;
int temp=b;
b=a;
a=temp;
}
int main()
{
int n;
cin>>n;
//arr is swapped using swapped method
int arr[n],i,temp;
//arr2 is swapped using temp variable
int arr2[n];
for(i=0;i<n;i ) {
cin>>arr[i];
arr2[i]=arr[i];
}
for(i=0;i<n;i ) {
swap(arr[i],arr[arr[i] 1]);
//cout<<"swap2 "<<arr2[i]<<" "<<arr2[arr2[i] 1]<<endl;
//swap(arr2[i],arr2[arr2[i] 1]);
//store both the varibles before giving value
int temp1=arr2[i];
int temp2=arr2[arr2[i] 1];
//arr2[i]=arr2[arr2[i] 1];
//arr2[arr2[i] 1]=temp;
//swapping
arr2[arr2[i] 1]=temp1;
arr2[i]=temp2;
/*
cout<<"after swapping arr"<<endl;
for(int j=0;j<n;j ) {
cout<<arr[j]<<" ";
}
cout<<endl;
cout<<"after swapping arr2"<<endl;
for(int j=0;j<n;j ) {
cout<<arr2[j]<<" ";
}
cout<<endl;
*/
}
for(i=0;i<n;i ) {
cout<<arr[i]<<" ";
}
cout<<endl;
for(i=0;i<n;i ) {
cout<<arr2[i]<<" ";
}
}
Input :
$ g array.cpp && ./a.out
4
0 1 2 2
1 0 2 2
1 0 2 2
