Results 1 to 6 of 6

Thread: How to swap two variables values without using a third variable?

  1. #1
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,401
    Blog Entries
    1
    Rep Power
    155

    Do you know: How to swap two variables values without using a third variable?

    just do some math:

    Uses "Console"
    
    Double a = 1.234
    Double b = 5.678
    
    console_writeLine("A = " + str$(a) )
    console_writeLine("B = " + str$(b) )
    
    a += b
    b = a - b
    a = a - b
    
    console_writeLine("A = " + str$(a) )
    console_writeLine("B = " + str$(b) )
    
    console_waitKey()
    
    How about Strings?

    Uses "Console"
    
    String A = "World !"
    String B = "Hello "
    
    Console_WriteLine(A + B)
    
    A += B
    B = Left$( A, Len(A) - Len(B) )
    A = Right$(A, Len(A) - Len(B) )
    
    console_writeLine(A + B)
    
    console_waitKey()
    
    or in thinBasic:
    '...
    Swap A,B
    '...
    
    Last edited by ReneMiner; 06-03-2013 at 20:16.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Thanks a lot Rene. I appreciate!

    And what about:
    Printl instead of Console_WriteLine
    Print instead of Console_Write
    Waitkey instead of Console_WaitKey
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3

    Lightbulb Swap two Number using Bitwise operator

    We can also swap two Numbers using Bitwise operator


    var arg1 = int.Parse(Console.ReadLine());
    var arg2 = int.Parse(Console.ReadLine());
    Console.WriteLine("\n Before swapping arg1= {0} and arg2 = {1}", arg1, arg2);
    arg1 = arg1 ^ arg2;
    arg2 = arg1 ^ arg2;
    arg1 = arg1 ^ arg2;


    Here is full sample code
    https://corevoila.in/coding-problems...wise-operator/
    Last edited by cbz6109; 21-07-2020 at 16:46.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,083
    Blog Entries
    5
    Rep Power
    725
    Thank you for sharing the trick,

    it looks like this with thinBasic syntax:
    uses "console"
    
    print "Please enter the arg1: "
    int32 arg1 = console_readline()
    
    print "Please enter the arg2: "
    int32 arg2 = console_readline()
    
    Printl(strformat$("Before swapping arg1= {1} and arg2= {2}", arg1, arg2))
    
    arg1 = arg1 xor arg2
    arg2 = arg1 xor arg2
    arg1 = arg1 xor arg2
    
    Printl(strformat$("After swapping arg1= {1} and arg2= {2}", arg1, arg2))
    
    
    WaitKey
    

    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Junior Member
    Join Date
    Jan 2020
    Location
    France
    Posts
    5
    Rep Power
    2

    And what about compute time

    Hello,

    Little question

    Which method is less or more time consuming.

    The time may be an important parameter, example bubble sorting.


    Regards

  6. #6
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,401
    Blog Entries
    1
    Rep Power
    155
    Therefore i would suggest If your Bubbles are Stores to a String-Array to use Array Sort.
    If you stored values in a different manner - f.e. stored at heap-memory or AS subelements of udt it's probably still faster to sort the even numeric Elements as strings using the workaround and take advantage of a special behaviour of the array-functions - Here Array Extract - which will Sort the source-Array before extracting any values since the thinBasic-array-handling functions are incredible fast.
    So depending in how you stored Bubbles i would either convert them into an array or make the pointers to the bubbles an array to make use of array-functions -
    When your program uses "Dictionary" to store the data that is- in any case- the fastest and Most straightforward solution: it offers to sort however you like and it does not require to transfer any data before sorting. If you need to sort often in your program its worth to consider the use of Dictionary-module.
    If you are using "UI"-module anyway, create a listbox (if your Bubbles are values in an array of simple datataype, Listview If you have Data in an udt-array). Set its sorted-property true and Set to ascend or descend, fill in numeric values ("nVal") with leading zeros- or If in range of 32bit integer (Long, DWord) simply using Hex$(nVal, 8 ) :
    8 makes it formatted to 8 digits and all displayable values while f.e. MKE$(nVal), -range of Extended number-will result in a String containing Digits that can not be displayed if not re-converted (use CV...-functions). The list can stay invisible (mainly its enabled).
    It might not be the fastest way to sort - but very simple and it's easy to keep overview - you know anytime where your values are and what they are doing.
    And for the thinbasic-core-array-functions as said - If it's obviously not offered by Array Sort - use Array Extract with an easy extraction-pattern or even to select a few Special Elements and "abuse" the behaviour of Array Extract that will Sort the source-Array.
    Array Extract even offers to Extract pointed Elements - so you could make use of its behaviour to sort the source-Array and collect the VarPtr()s to your bubbles in an ordinary Array of DWord-values
    Last edited by ReneMiner; 30-07-2020 at 10:55.
    running thinBasic in HP Desktop Pavillon 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS1: Windows x64 Pro 10.0.18363.418
    OS2: ThinDE (WinRE+.NetCore-based customized Developement Environment)

Similar Threads

  1. Functions as Returned Values
    By danbaron in forum Science
    Replies: 0
    Last Post: 27-05-2011, 08:47
  2. Hex values
    By Michael Clease in forum thinDebug
    Replies: 2
    Last Post: 31-07-2010, 21:59
  3. byte values
    By Michael Clease in forum Sources, Templates, Code Snippets, Tips and Tricks, Do you know ...
    Replies: 1
    Last Post: 24-08-2009, 13:56
  4. Simple question about hex values
    By Michael Clease in forum thinBasic General
    Replies: 8
    Last Post: 26-05-2007, 10:09

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •