C/C++ simple challenge

Discussion in 'Programming' started by shoegoe, Oct 30, 2004.

Thread Status:
Not open for further replies.
  1. shoegoe

    shoegoe New Member

    Joined:
    Oct 19, 2004
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    !~The World~!

    we all know how to swap numbers in a program..

    BUT we use a variable temp/like that to store the initial value..

    can the same be done without using a TEMP variable?
     
  2. cg84

    cg84 New Member

    Joined:
    Oct 22, 2004
    Messages:
    50
    Likes Received:
    0
    Trophy Points:
    0
    very simple...
    take 2 nos. x & y and do the following:
    x=x+y
    y=x-y
    x=x-y
    and ur nos r swapped... :)
     
  3. sujithtom

    sujithtom New Member

    Joined:
    Aug 14, 2004
    Messages:
    512
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Not anywhere near you
    That was a cool idea. How about manipulating fuctions???
     
  4. cg84

    cg84 New Member

    Joined:
    Oct 22, 2004
    Messages:
    50
    Likes Received:
    0
    Trophy Points:
    0
    what??? didnt get ur question...
     
  5. sms_solver

    sms_solver New Member

    Joined:
    Jan 2, 2004
    Messages:
    420
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    somewhere
    cg84 from where did you find this solution?

     
  6. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    its very old question !!

    there are many more answers possible
    x= x*y
    y= x/y
    x= x/y
     
  7. NikhilVerma

    NikhilVerma Member

    Joined:
    May 12, 2004
    Messages:
    930
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    UK
    Ok now here is another simple one....

    Do the same swapping with strings... without using temp variable...
     
  8. cooljeba

    cooljeba The Photoshop Guy

    Joined:
    Jun 30, 2004
    Messages:
    606
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    On Your monitor!
    this is the best way to do it with out using a temp variable ;)


    ..:: peace ::..
    jeba
     
  9. vysakh

    vysakh New Member

    Joined:
    Sep 1, 2004
    Messages:
    860
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Old trafford
  10. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    Write the smallest C program which prints itself.
     
  11. swatkat

    swatkat Active Member

    Joined:
    Mar 12, 2004
    Messages:
    2,060
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    Shimoga/ಶಿವಮೊಗ್ಗ
    main()
    {
    getche();
    }
    How about this?
     
  12. lamrinnirmal

    lamrinnirmal New Member

    Joined:
    Jan 14, 2004
    Messages:
    158
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    /bin/sh
    you mean manipulating program execution flow?as in the main calls function A but you wanna call B, without B(); anywhere in the program?
     
  13. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    dont know the answer ..
    but i dont think that's correct
     
  14. OP
    OP
    shoegoe

    shoegoe New Member

    Joined:
    Oct 19, 2004
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    !~The World~!
  15. cnukutti

    cnukutti New Member

    Joined:
    Feb 10, 2004
    Messages:
    99
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai,India
    What is the thing about manipulating functions??
    and what do you mean by printing itself.
    should it read the .c file and print the source code???
     
  16. swatkat

    swatkat Active Member

    Joined:
    Mar 12, 2004
    Messages:
    2,060
    Likes Received:
    1
    Trophy Points:
    38
    Location:
    Shimoga/ಶಿವಮೊಗ್ಗ
    "Write the smallest C program which prints itself."
    What do you mean by this?
     
  17. DKant

    DKant New Member

    Joined:
    Jul 10, 2004
    Messages:
    265
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    In a creaking plashtik chair
    Simple? Well I dunno. Sumthin' with pointers? But I don't see how u can do any manips on pointers. :?

    Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.
     
  18. alib_i

    alib_i New Member

    Joined:
    Jun 24, 2004
    Messages:
    1,191
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    omnipresent
    @swatcat ...
    frankly speaking .. even i dont know ... this exactly was a problem statement in one programming contest long time back ..
    i still dont know wht it means and how to solve it
     
  19. vysakh

    vysakh New Member

    Joined:
    Sep 1, 2004
    Messages:
    860
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Old trafford
    i give up
    whats the answer???
     
  20. OP
    OP
    shoegoe

    shoegoe New Member

    Joined:
    Oct 19, 2004
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    !~The World~!

    Ths...
    BUT

    How to swap more than 2 variables .....
    (DONT TELL ME TO USE THE SAME CODE FOR EACH 2 SWAPS

    ANSWERS??
     
Thread Status:
Not open for further replies.

Share This Page