Thread: Interchange the contents of two variables

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    Interchange the contents of two variables

    Alright, so I'm reading a book on C programming and this is an assignment after the first chapter.

    "Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and "

    I've managed to do it but to me it seems like I've done it too complicated. There should be an easier solution to doing this :P

    My solution:

    Code:
    main()
    
    {
       int C, D, C1, D1;
    
       printf("Enter a value into location C: ");
         scanf("%d", &C);
       printf("Enter a value into location D: ");
         scanf("%d", &D);
    
       printf("%d %d", C, D );
       printf("\n");
    
       C1 = C + 1;
       D1 = D + 1;
    
       C = D1 - 1;
       D = C1 - 1;
    
       printf("%d %d", C, D);
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You only need one extra value. Put one of your numbers in the extra slot, then overwrite the number you just moved out.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To swap A and B:

    C = A (or B)
    A (or B) = B (or A)
    B (or A) = A (or B)

    Take three cups, put a coin in each two. Only moving one coin at a time, without having more than one coin per cup, move the coins so that the starting positions of the coins are reversed.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Alright, so this is how I did it instead and it worked. Is that how it should be?

    E = C;
    C = D;
    D = E;

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by glasvegas View Post
    Alright, so this is how I did it instead and it worked. Is that how it should be?

    E = C;
    C = D;
    D = E;
    Yep. But do make a habit of using more descriptive variable names in your real programs.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And also you main function declaration looks all flat read up this int main( void ) and should return 0

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Suppose I place two coffee cups on your desk and ask you to swap them, is this task also hard to understand? The two cups can't be in the same place at the same time... It's almost as if you need some extra... space... for the exchange to occur
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    Suppose I place two coffee cups on your desk and ask you to swap them, is this task also hard to understand? The two cups can't be in the same place at the same time... It's almost as if you need some extra... space... for the exchange to occur
    First, I drink cup one. Then I drink cup two. What were we doing again?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    First, I drink cup one. Then I drink cup two. What were we doing again?
    And you can even put it back when you're done with it...

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by quzah View Post
    First, I drink cup one. Then I drink cup two. What were we doing again?
    He never said there was anything in them
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. append contents
    By aniket1988 in forum C Programming
    Replies: 4
    Last Post: 10-15-2010, 12:30 PM
  2. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  3. Replies: 3
    Last Post: 11-28-2006, 03:44 PM
  4. interchange nodes in a linked list
    By Gustavo in forum C++ Programming
    Replies: 0
    Last Post: 10-26-2004, 07:14 PM
  5. Interchange
    By Juganoo in forum C Programming
    Replies: 1
    Last Post: 12-20-2002, 08:10 PM