Author Topic: on what occasion it can be used  (Read 4406 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
on what occasion it can be used
« on: September 25, 2020, 05:22:12 pm »
hi all I saw an instruction that changes a variable and is called
swap variable
then I noticed that you can also create it without swap
leaning on a third variable making a transfer of variable

uno$ = "buon"
due$ = "giorno"


tre$ = uno$
uno$ = due$
due$ = tre$


PRINT uno$, uno$
PRINT due$, due$

invece swap

a$ = "one"
b$ = "two"

SWAP a$, b$

PRINT a$
PRINT b$


my question and what can make this change useful for?

if you want to write a variable you can write one first and after the other there would be no need to change?
someone to an example of some program to make me understand why this can be useful?
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: on what occasion it can be used
« Reply #1 on: September 25, 2020, 06:00:35 pm »
Very often used in sorting where you want to switch the values of 2 array items so they will be in alphabetic order.

Have you ever tried making a little sort routine?
« Last Edit: September 25, 2020, 06:02:50 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: on what occasion it can be used
« Reply #2 on: September 25, 2020, 06:27:07 pm »
Quick... Someone get Steve a bucket!

Code: QB64: [Select]
  1. REM SWAP demo to alphabetize a list...
  2. myarray$(1) = "dog"
  3. myarray$(2) = "cat"
  4. myarray$(3) = "rabbit"
  5. myarray$(4) = "frog"
  6. myarray$(5) = "horse"
  7. h = 5
  8. FOR i = 1 TO h
  9.     FOR j = 1 TO h
  10.         IF i <> j THEN
  11.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  12.         END IF
  13. NEXT j, i
  14.  
  15. FOR i = 1 to h
  16. PRINT myarray$(i)
  17.  

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: on what occasion it can be used
« Reply #3 on: September 25, 2020, 06:50:57 pm »
Mainly convenience, you also avoid creating a new variable tre$ which compromises code readability and possibly memory usage.

Also, some computers are faster at swapping things than copying things, this lets them know that what you really want is a mere swap which they are much more eager to perform.

Code: QB64: [Select]
  1. un$ = "palpiti"
  2. dos$ = "sentir "
  3.  
  4.  
  5. ? un$, dos$
  6.  
  7. for i=1 to len(un$)
  8.         un$ = mid$(un$,1,i-1)+chr$(asc(mid$(un$, i, 1)) xor asc(mid$(dos$, i, 1)))+mid$(un$,i+1, len(un$)-1)
  9.         dos$ = mid$(dos$,1,i-1)+chr$(asc(mid$(dos$, i, 1)) xor asc(mid$(un$, i, 1)))+mid$(dos$,i+1, len(dos$)-1)
  10.         un$ = mid$(un$,1,i-1)+chr$(asc(mid$(un$, i, 1)) xor asc(mid$(dos$, i, 1)))+mid$(un$,i+1, len(un$)-1)
  11.  
  12. ? un$, dos$
  13.  
  14.  

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: on what occasion it can be used
« Reply #4 on: September 26, 2020, 08:00:36 am »
thank you all
I understood that it also recognizes letters if they are major or minor as in the example below
example recognizes that a is less than b and b is less than c

Code: QB64: [Select]
  1. REM SWAP demo TO alphabetize a LIST...
  2. myarray$(1) = "aa"
  3. myarray$(2) = "ab"
  4. myarray$(3) = "ac"
  5. myarray$(4) = "ad"
  6. myarray$(5) = "af"
  7. myarray$(6) = "ag"
  8. myarray$(7) = "ah"
  9. myarray$(8) = "ai"
  10. myarray$(9) = "al"
  11. myarray$(10) = "am"
  12.  
  13.  
  14.  
  15.  
  16.  
  17. h = 10
  18. FOR i = 1 TO h
  19.     FOR j = 1 TO h
  20.         IF i <> j THEN
  21.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  22.         END IF
  23. NEXT j, i
  24.  
  25. FOR i = 1 TO h
  26.     PRINT myarray$(i)
  27.  

another question because the for loop only gets to 10 if I go beyond it doesn't go anymore

like in this example?

Code: QB64: [Select]
  1. REM SWAP demo TO alphabetize a LIST...
  2. myarray$(1) = "aa"
  3. myarray$(2) = "ab"
  4. myarray$(3) = "ac"
  5. myarray$(4) = "ad"
  6. myarray$(5) = "af"
  7. myarray$(6) = "ag"
  8. myarray$(7) = "ah"
  9. myarray$(8) = "ai"
  10. myarray$(9) = "al"
  11. myarray$(10) = "am"
  12. myarray$(11) = "ao"
  13.  
  14.  
  15.  
  16.  
  17. h = 11
  18. FOR i = 1 TO h
  19.     FOR j = 1 TO h
  20.         IF i <> j THEN
  21.             IF myarray$(i) < myarray$(j) THEN SWAP myarray$(i), myarray$(j)
  22.         END IF
  23. NEXT j, i
  24.  
  25. FOR i = 1 TO h
  26.     PRINT myarray$(i)
  27.  

so i can't get from a to z ?

thanks
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: on what occasion it can be used
« Reply #5 on: September 26, 2020, 11:41:13 am »
You have to DIM an array > 10

ie DIM MyArray$(1 to 11)

fixed
Code: QB64: [Select]
  1. REM SWAP demo TO alphabetize a LIST...
  2.  
  3. DIM myArray$(1 TO 11)
  4. myArray$(1) = "aa"
  5. myArray$(2) = "Ab"
  6. myArray$(3) = "ac"
  7. myArray$(4) = "ad"
  8. myArray$(5) = "af"
  9. myArray$(6) = "Ag"
  10. myArray$(7) = "ah"
  11. myArray$(8) = "ai"
  12. myArray$(9) = "al"
  13. myArray$(10) = "am"
  14. myArray$(11) = "Ao"
  15.  
  16.  
  17.  
  18.  
  19. h = UBOUND(myArray$)
  20. FOR i = 1 TO h - 1 '<<<<<<<<<<<< fix for slightly better code
  21.     FOR j = i + 1 TO h '<<<<<<<< fix so j <> i ever
  22.         'IF i <> j THEN    i <> j always!!!
  23.         IF myArray$(i) > myArray$(j) THEN SWAP myArray$(i), myArray$(j) ' descending switch < to >
  24.         'END IF
  25. NEXT j, i
  26.  
  27. FOR i = 1 TO h
  28.     PRINT myArray$(i)
  29.  
  30.  
  31.  

If you have to sort a much bigger array you might want something faster than this, but fine for little arrays.
« Last Edit: September 26, 2020, 11:44:34 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: on what occasion it can be used
« Reply #6 on: September 26, 2020, 03:06:39 pm »
10 arrays ought to be enough for anybody... B.G.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: on what occasion it can be used
« Reply #7 on: September 26, 2020, 06:28:57 pm »
Arrays the extra then ;-))

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: on what occasion it can be used
« Reply #8 on: September 26, 2020, 09:33:24 pm »
10 arrays ought to be enough for anybody... B.G.

Many moons ago, Ten Arrays put on his war paint and met Loops While True in single combat. The battle was long and GUI.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: on what occasion it can be used
« Reply #9 on: September 26, 2020, 09:39:21 pm »
Now that's a post that deserves many moons. It's getting one from me right now. Oops, wrong forum, I'm Skyping on the Oprah Forum. Sorry ladies.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: on what occasion it can be used
« Reply #10 on: September 27, 2020, 02:13:16 pm »
@Kiara87
hi
here an example of using Swap for graphic values
Code: QB64: [Select]
  1. 'impostare una finestra grafica/ set a graphic window
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. ' imposta il nome finestra / set name of window
  4. _TITLE "OpenGL 1"
  5.  
  6. ' costanti generali / general constants
  7. CONST True = -1, False = 0
  8.  
  9. ' constanti colori / colors constants
  10. CONST Black = _RGBA32(0, 0, 0, 255), White = _RGBA32(255, 255, 255, 255)
  11. ' variabili/ variables
  12. DIM SHARED Gc AS SINGLE, Rc AS SINGLE, Bc AS SINGLE, Gc1 AS SINGLE, Rc1 AS SINGLE, Bc1 AS SINGLE
  13. 'inizializzazione delle variabili /initalitation of variables
  14. FS = True
  15. Init = True
  16. Kb = 0
  17. Done = False
  18.  
  19. Rc = 1.0: Gc = 1.0: Bc = 0.0: Rc1 = 0.0: Gc1 = 1.0: Bc1 = 1.0
  20. ' ciclo principale /main loop
  21.     ' area di ingresso dati /area of input
  22.     Kb = _KEYHIT
  23.     ' traduttore dei comandi dati con tastiera/ translator commands given by keyboard
  24.     IF Kb THEN
  25.         ' se barra spazio cambio lo stato di FS
  26.         IF Kb = 32 THEN
  27.             FS = NOT FS
  28.             Init = True
  29.         END IF
  30.         IF Kb = 27 THEN Done = True
  31.         IF Kb = 13 THEN
  32.             SWAP Gc, Gc1
  33.             SWAP Bc, Bc1
  34.             SWAP Rc, Rc1
  35.         END IF
  36.     END IF
  37.     ' esecutore dei comandi individuati dal traduttore comandi
  38.     ' executor of commands taken from  commands of translator
  39.     IF FS = True THEN
  40.         _FULLSCREEN
  41.     ELSE
  42.         _FULLSCREEN OFF
  43.     END IF
  44. LOOP UNTIL Done = True
  45. END ' fine logica del programma / logical end of the program
  46.  
  47.  
  48. '----Area Subroutines and Functions--------
  49.  
  50. SUB _GL ()
  51.     'qui vanno tutte le istruzioni _gl / here all _gl instructions
  52.     IF Init THEN
  53.         ' area di inizializzazione/initialization area
  54.  
  55.         ' se  e' schermo intero imposta l'intero schermo come viewport
  56.         ' if it is  full screen it sets whole screen as viewport
  57.  
  58.         IF FS THEN
  59.             _glViewport 0, 0, _WIDTH, _HEIGHT
  60.         ELSE
  61.             ' se non e' schermo intero imposta il quarto in basso a destra come viewport
  62.             ' if it is not full screen  it sets the quarter at bottom right as viewport
  63.             _glViewport _WIDTH / 2, _HEIGHT / 2, _WIDTH, _HEIGHT
  64.         END IF
  65.  
  66.         _glClearColor 0.7, 0.0, 0.0, 1.0 ' colore sfondo / background
  67.         Init = False ' disattiva il flag  / it disactivates the flag
  68.         'END IF
  69.     ELSE
  70.         _glClear (_GL_COLOR_BUFFER_BIT OR _GL_DEPTH_BUFFER_BIT) ' rigenera il color buffer bit / it clear color buffer bit
  71.  
  72.  
  73.         IF FS THEN
  74.             ' esegue a tutto schermo/ it executes in fullscreen mode
  75.             _glBegin _GL_LINE_LOOP
  76.             _glColor3f Rc, Gc, Bc
  77.             _glVertex3d -1.0, -1.0, 0.0
  78.             _glVertex3d 0.0, 1.0, 0.0
  79.             _glVertex3d -0.0, -0.5, 0.0
  80.             _glEnd
  81.         ELSE
  82.             ' esegue a finestra / it executes in window mode
  83.             _glBegin _GL_LINE_STRIP
  84.             _glColor3f Rc1, Gc1, Bc1
  85.             _glVertex3d 0.0, 0.0, 0.0
  86.             _glVertex3d 1.0, -0.6, -0.7
  87.             _glVertex3d 1.0, 0.0, 0.0
  88.             _glVertex3d 0.0, -0.6, -0.7
  89.             _glEnd
  90.         END IF
  91.         _glFlush
  92.     END IF
  93.  

every time you press Enter you change the color of the lines drawn... with Spacebar you change the mode and the lines that you can see, and at the end Escape let you quit the program.
You save to typing more code and save RAM creating less variables as our friends has shown you above.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: on what occasion it can be used
« Reply #11 on: September 27, 2020, 05:00:19 pm »
I just used SWAP here: https://www.qb64.org/forum/index.php?topic=3053.msg123250#msg123250

To select the lesser of two random picked x, y coordinates for a top left corner of a box, and the other random picked x, y for the bottom right corner of a box which needed a greater x and y than used in top left corner.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: on what occasion it can be used
« Reply #12 on: September 27, 2020, 06:02:46 pm »
To address the simplest answer:

Swap *never* has to be used.  If you want to trade values from X and Y, you can *always* use a temp variable as the go between to do so.

temp = X
X = Y
Y = temp

So why *would* you ever want to use SWAP?

1) Readability.  Once you’re used to the command, it’s simply reading and deciphering one single, self-documenting function, in your code.

2) Reduced typing.  With expressive character names, or types, it’s much quicker and less strain than typing multiple variables names over and over.  Without using SWAP, you type those variable names six times.  With it, you only type them twice.  Reduce repetitive stress as much as possible — your joints will thank you for it later!

3) Less memory usage and program overhead.  Adding a temp variable means allocating the memory for that variable.  Now, on modern 128GB systems, 4-bytes isn’t anything to care about, but it’s never a good practice to get into making bloatware.  Who knows what microprocessor/system you’ll want to code on in the future?

SWAP *never* has to be used, but generally speaking, it *should* generally be your goto method for swapping two values.  When you can use it, use it.  Honestly, it doesn’t make any sense not to, that I know of.

Can anyone out there think of a time/reason when it’d be best to use a temp variable as a middleman and manually do the process, ratherthan just use SWAP?  I’m honestly drawing a blank on why one wouldn’t use it, when they could.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: on what occasion it can be used
« Reply #13 on: September 27, 2020, 07:16:01 pm »
Can anyone out there think of a time/reason when it’d be best to use a temp variable as a middleman and manually do the process, ratherthan just use SWAP?  I’m honestly drawing a blank on why one wouldn’t use it, when they could.

Yes, dammit, because some of us like doing everything the hard way!!!!!!

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: on what occasion it can be used
« Reply #14 on: October 02, 2020, 03:25:20 pm »
obviously it doesn't make sense to use a temporary variable

since there is education

which changes the variable automatically

thank

To address the simplest answer:

Swap *never* has to be used.  If you want to trade values from X and Y, you can *always* use a temp variable as the go between to do so.

temp = X
X = Y
Y = temp

So why *would* you ever want to use SWAP?

1) Readability.  Once you’re used to the command, it’s simply reading and deciphering one single, self-documenting function, in your code.

2) Reduced typing.  With expressive character names, or types, it’s much quicker and less strain than typing multiple variables names over and over.  Without using SWAP, you type those variable names six times.  With it, you only type them twice.  Reduce repetitive stress as much as possible — your joints will thank you for it later!

3) Less memory usage and program overhead.  Adding a temp variable means allocating the memory for that variable.  Now, on modern 128GB systems, 4-bytes isn’t anything to care about, but it’s never a good practice to get into making bloatware.  Who knows what microprocessor/system you’ll want to code on in the future?

SWAP *never* has to be used, but generally speaking, it *should* generally be your goto method for swapping two values.  When you can use it, use it.  Honestly, it doesn’t make any sense not to, that I know of.

Can anyone out there think of a time/reason when it’d be best to use a temp variable as a middleman and manually do the process, ratherthan just use SWAP?  I’m honestly drawing a blank on why one wouldn’t use it, when they could.
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione