the book im studying had this swap prog to show passing by value, the swap was between 2 numbers, after i did that i added 2 more variables
Code:
#include <iostream>
void swap(int x, int y, int t, int z);
int main()
{
int x = 5, y = 10, t = 20, z = 30;
std::cout<<"Swap1 below\n\n";
std::cout << "Main1, before swap, x: "<< x << " y: "<< y << "\n";
swap (x, y, t, z);
std::cout <<"Main1, after swap, x: "<< x << " y: " << y << "\n\n";
std::cout<<"Swap2 below\n\n";
std::cout <<"Main2, before swap, t: "<< t << " z: "<< z <<"\n";
swap (x, y , t, z);
std::cout <<"Main2, after swap, t: "<< t << " z: "<< z <<"\n";
return 0;
}
void swap (int x, int y, int t, int z)
{
int temp;
int temp2;
std::cout <<"Swap1, before swap, x: "<< x << " y: " << y << "\n";
temp = x;
x = y;
y = temp;
std::cout<<"Swap1, after swap, x: "<< x << " y: " << y << "\n";
std::cout<<"Swap2, before swap, t: "<< t << " z: " << z << "\n";
temp2 = t;
t = z;
z = temp2;
std::cout<<"Swap2, after swap, t: "<< t << " z: " << z << "\n";
}
as it is now it swaps the 1st and 2nd number and then it swaps the 3rd and 4th number. the result tho seems to be a bit scrabbled, swap1 and swap2 appear in eachother.
what did i do wrong?