Thread: variation of swap

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    variation of swap

    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?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It depends on what you are really trying to do. Do you realize that everytime you call the function swap(), it performs all the code inside that function. Since you call swap twice, then x and y are swapped and t and z are swapped twice each. Also, all the output is printed twice.

    If you wanted to try swapping different sets of numbers, maybe this is what you had in mind:
    Code:
    #include <iostream>
    
    void swap(int x, int y);
    
    int main()
    {
        int a = 5, b = 10, t = 20, z = 30;
    
        std::cout<<"Swap1 below\n\n";
    
        std::cout << "Main1, before swap, a: "<< a << " b: "<< b << "\n";
        swap (a, b);
        std::cout <<"Main1, after swap, a: "<< a << " b: " << b << "\n\n";
    
        std::cout<<"Swap2 below\n\n";
    
        std::cout <<"Main2, before swap, t: "<< t << " z: "<< z <<"\n";
        swap (t, z);
        std::cout <<"Main2, after swap, t: "<< t << " z: "<< z <<"\n";
        return 0;
    }
    void swap (int x, int y)
    {
        int temp;
    
        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";
    }
    Notice how you call the function twice, each time with different sets of values. Also notice how it doesn't matter what the name of the variables are inside main() that you pass to swap, they don't have to be the same.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    thats exactly what i was trying to do, but when i tryed to use
    Code:
    swap (a, b);
    it was given me error saying swap function doesnt take 2 parameters.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    do you want to modify the vars passed into swap like?
    void swp(int& x, int& y)
    {
    int t = x;
    x = y;
    y = t;
    }
    x and y then are passed by reference instead of by value and the function modifies the physical parameters passed in.

    I think some of the compiler problems with swap may be that std::swap is defined within #include <algorithm> It really shouldn't do this, but some of the older versions of g++ had incomplete support for namespaces. The good thing is I would just not write the swap function yourself. Use std::swap and you'll be fine.
    Last edited by okinrus; 04-19-2004 at 09:46 PM.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yes but im not doing this to gain time or something, im trying to learn so coding experience is nessecary besides, ppl say... a programmer that relies of default compiler is an unemployed programmer, something along those lines lol

    jlou was correct i was trying to do exactly what he did, but i still dont understand how his swap works, i mean
    Code:
     temp = x;
        x = y;
        y = temp;
    only swaps x and y, how is t and z get swaped? cuz they do get swaped but how ?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    yes but im not doing this to gain time or something, im trying to learn so coding experience is nessecary besides, ppl say... a programmer that relies of default compiler is an unemployed programmer, something along those lines lol
    Yeah, well, pretty much unemployed and in school here The problem I will have to solve is proving my coding experience.

    One simple way of viewing a pass by value function call is by substitution. For example, say you have

    Code:
    void swap(int x, int y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    
    int main(void) 
    {
         int a = 5;
         int b = 6; 
         swap(a, b);
    }
    You can expand swap(a, b) using the definition of swap, replacing x with a and y with b

    Code:
    int main(void) 
    {
         int a = 5;
         int b = 6; 
    
        {
           int temp = a;
           a  = b;
           b = temp;
        }
    }
    Of course this isn't pass by value, but it is conceptually close to what the function call does, with the exception that there's no side effects from a function called by value. a and b are not modified after the function call returns.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    As okinrus explained, the first key is to understand what a function does. Your swap function is not a very good example for this because it is actually a good example of pass by reference which you can't understand until you understand how a function works.

    A function is something that does something for you, like a dishwasher. A dishwasher washes dishes, but it doesn't care what dishes you put into it, it will just wash them. So if you want your dishwasher to wash only the plates, you would just fill it with plates. If you then wanted it to wash only your glasses, you would run it again but this time fill it with glasses.

    Here is a better example of a function. Notice how the function is called (in red) five different times. Each time it is called with different information passed to it, just like each time you run the dishwasher you can wash different dishes. The function runs the same code each time (in blue), but because you pass it different data, it ends up outputting different information. If you compile this code and run it, you will see the different values output. That is because the function runs five times, once for each different value passed.
    Code:
    #include <iostream>
    
    void function(int x);
    
    int main()
    {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 4;
        int e = 5;
    
        function(a);
        function(b);
        function(c);
        function(d);
        function(e);
    
        return 0;
    }
    
    void function(int x)
    {
        std::cout << "The value passed in is: " << x << std::endl;
    }
    Now if you apply that concept to my swap, you see that the swap function is called two times. The first time, the values in a and b are passed in. The second time, the values in t and z are passed in. Since the function just takes what you give it and uses that each time it is called, it swaps a and b the first time and t and z the second time. I purposely changed 'x' and 'y' to 'a' and 'b' inside main() to show that it doesn't matter what you pass into the function, it will use whatever you give it. It doesn't know anything about main() or the variables in main(), it only knows what you give it.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    cool thanks, im on the function chapter btw ehehh so not very familiar with everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 09:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM
  5. Skipping DX swap chain node - not good...
    By Magos in forum Game Programming
    Replies: 0
    Last Post: 03-02-2006, 08:37 PM