Author Topic: WHILE AND DO LOOP CYCLES  (Read 9641 times)

0 Members and 1 Guest are viewing this topic.

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #30 on: November 09, 2020, 06:11:09 pm »

from your advice i searched google and i found this code

but it doesn't work and I don't understand why this code doesn't work?

@Kiara87  No odds/evens, find Prime Numbers from 2 to 100 with the help of an array to store findings. (You don't have to, of course, just trying to find ways to for you to practice with arrays.)


Code: QB64: [Select]
  1. DEFLNG A-Z
  2. DECLARE SUB BubbleSortInt(a(), Ans)
  3. DECLARE SUB FillArrayInt(a())
  4. DECLARE SUB ShowArrayInt(a())
  5.  
  6. SUB BubbleSortInt(a(), Ans)
  7.     DIM Var_Swap
  8.     DIM Last
  9.     DIM i
  10.     DIM n
  11.     DIM Temp
  12.     DIM A1
  13.     DIM A2
  14.    
  15.     n = UBOUND(a) + 1
  16.     Var_Swap = 1
  17.     Last = n - 1
  18.     i = 0
  19.     DO WHILE Var_Swap
  20.         Var_Swap = 0
  21.         FOR i = 0 TO Last - 1
  22.             IF a(i) > a(i + 1) THEN
  23.                 A1 = a(i)
  24.                 A2 = a(i + 1)
  25.                 Temp = a(i)
  26.                 a(i) = a(i + 1)
  27.                 a(i + 1) = Temp
  28.                 Var_Swap = 1
  29.                 IF Ans = 1 THEN
  30.                     PRINT "SWAP     i: " + STR$(A1) + "   i+1: " + STR$(A2)
  31.                     ShowArrayInt a
  32.                 END IF
  33.             END IF
  34.         NEXT
  35.     LOOP
  36.  
  37. SUB FillArrayInt(a())
  38.     DIM i
  39.     DIM Length
  40.    
  41.     Length = UBOUND(a) + 1
  42.     FOR i = 0 TO Length - 1
  43.         a(i) = INT(RND * Length)
  44.     NEXT
  45.  
  46. SUB ShowArrayInt(a())
  47.     DIM i
  48.     DIM s$
  49.     DIM Length
  50.    
  51.     Length = UBOUND(a) + 1
  52.     s$ = ""
  53.     FOR i = 0 TO Length - 1
  54.         IF i = Length - 1 THEN
  55.             s$ = s$ + STR$(a(i))
  56.         ELSE
  57.             s$ = s$ + STR$(a(i)) + " "
  58.         END IF
  59.     NEXT
  60.     PRINT s$
  61.  
  62. ' Main
  63. DIM Num
  64. DIM Ans$
  65. DIM AnsI
  66. DIM i
  67.  
  68. PRINT "[IT] Quanti elementi? " + CHR$(13) + CHR$(10) + "[EN] Enter no of elements : "
  69. INPUT Num
  70. PRINT "[IT] Vuoi vedere il contenuto del vettore quando scambio? (S/Y/N)" + CHR$(13) + CHR$(10) + "[EN] Do you want to see swapped elements in array? (S/Y/N)"
  71. INPUT Ans$
  72. IF Ans$ = "S" OR Ans$ = "s" OR Ans$ = "Y" OR Ans$ = "y" OR LEN(Ans$) > 0 THEN
  73.     AnsI = 1
  74.     AnsI = 0
  75. DIM Arr(Num)
  76.  
  77. FillArrayInt Arr
  78. PRINT "[IT] Vettore non ordinato: " + CHR$(13) + CHR$(10) + "[EN] Unordered Array: "
  79. ShowArrayInt Arr
  80. BubbleSortInt Arr, AnsI
  81. PRINT "[IT] Vettore ordinato: " + CHR$(13) + CHR$(10) + "[EN] Ordered Array: "
  82. ShowArrayInt Arr
  83.  
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #31 on: November 09, 2020, 06:47:14 pm »
@Kiara87
the code that you posted here above isn't original Qb64... to see how it works (runs) you must translate into this one
Code: QB64: [Select]
  1. DEFLNG A-Z
  2. DECLARE SUB BubbleSortInt(a(), Ans)
  3. DECLARE SUB FillArrayInt(a())
  4. DECLARE SUB ShowArrayInt(a())
  5.  
  6. ' Main
  7. DIM Num
  8. DIM Ans$
  9. DIM AnsI
  10. DIM i
  11.  
  12. PRINT "[IT] Quanti elementi? " + CHR$(13) + CHR$(10) + "[EN] Enter no of elements : "
  13. INPUT Num
  14. PRINT "[IT] Vuoi vedere il contenuto del vettore quando scambio? (S/Y/N)" + CHR$(13) + CHR$(10) + "[EN] Do you want to see swapped elements in array? (S/Y/N)"
  15. INPUT Ans$
  16. IF Ans$ = "S" OR Ans$ = "s" OR Ans$ = "Y" OR Ans$ = "y" OR LEN(Ans$) > 0 THEN
  17.     AnsI = 1
  18.     AnsI = 0
  19. DIM Arr(Num)
  20.  
  21. FillArrayInt Arr()
  22. PRINT "[IT] Vettore non ordinato: " + CHR$(13) + CHR$(10) + "[EN] Unordered Array: "
  23. ShowArrayInt Arr()
  24. BubbleSortInt Arr(), AnsI
  25. PRINT "[IT] Vettore ordinato: " + CHR$(13) + CHR$(10) + "[EN] Ordered Array: "
  26. ShowArrayInt Arr()
  27.  
  28.  
  29.  
  30. SUB BubbleSortInt (a(), Ans)
  31.     DIM Var_Swap
  32.     DIM Last
  33.     DIM i
  34.     DIM n
  35.     DIM Temp
  36.     DIM A1
  37.     DIM A2
  38.  
  39.     n = UBOUND(a) + 1
  40.     Var_Swap = 1
  41.     Last = n - 1
  42.     i = 0
  43.     DO WHILE Var_Swap
  44.         Var_Swap = 0
  45.         FOR i = 0 TO Last - 1
  46.             IF a(i) > a(i + 1) THEN
  47.                 A1 = a(i)
  48.                 A2 = a(i + 1)
  49.                 Temp = a(i)
  50.                 a(i) = a(i + 1)
  51.                 a(i + 1) = Temp
  52.                 Var_Swap = 1
  53.                 IF Ans = 1 THEN
  54.                     PRINT "SWAP     i: " + STR$(A1) + "   i+1: " + STR$(A2)
  55.                     ShowArrayInt a()
  56.                 END IF
  57.             END IF
  58.         NEXT
  59.     LOOP
  60.  
  61. SUB FillArrayInt (a())
  62.     DIM i
  63.     DIM Length
  64.  
  65.     Length = UBOUND(a) + 1
  66.     FOR i = 0 TO Length - 1
  67.         a(i) = INT(RND * Length)
  68.     NEXT
  69.  
  70. SUB ShowArrayInt (a())
  71.     DIM i
  72.     DIM s$
  73.     DIM Length
  74.  
  75.     Length = UBOUND(a) + 1
  76.     s$ = ""
  77.     FOR i = 0 TO Length - 1
  78.         IF i = Length - 1 THEN
  79.             s$ = s$ + STR$(a(i))
  80.         ELSE
  81.             s$ = s$ + STR$(a(i)) + " "
  82.         END IF
  83.     NEXT
  84.     PRINT s$
  85.  
  86.  

it is about Bubblesort a fundamental algorhytm about sorting a list of omogeneus things.

@bplus,
yes  the challange is to find Prime Number from 2 to 100, but  a first pass to find Prime Number is to evaluate if the number is even, if yes and more than 2 it is NOT prime number, otherwise if it is ODD I must divide it by its previous numbers to get no integer result to find the Prime Number.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #32 on: November 09, 2020, 07:03:54 pm »
@TempodiBasic

Don't have to do 2's separate:
Code: QB64: [Select]
  1. REDIM n(2 TO 100) AS INTEGER
  2. n(2) = 2
  3. FOR i = 3 TO 100
  4.     FOR j = 2 TO SQR(i)
  5.         IF i / j = INT(i / j) THEN n(i) = j: EXIT FOR
  6.     NEXT
  7.     IF n(i) = 0 THEN n(i) = i
  8. PRINT "primes between 2 and 100:"
  9. FOR i = 2 TO 100
  10.     IF n(i) = i THEN PRINT i,
  11. PRINT: PRINT "Lowest dividers:"
  12. FOR i = 2 TO 100
  13.     IF n(i) <> i THEN PRINT i; " "; n(i),
  14.  
« Last Edit: November 09, 2020, 07:08:33 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #33 on: November 10, 2020, 09:20:56 am »
@dear Bplus
I think that we can save CPU work using STEP 2 in the external FOR NEXT loop.
In fact the only even Prime number is 2. And with this we reduce to half the work to accomplish by the program.
And as consequence the inner FOR NEXT starts from 3 to SQR(i) step 2 because if we're processing only odd numbers there is no matter to divide by 2 and by its multiplies.
so your code can be converted into this one
Code: QB64: [Select]
  1. REDIM n(2 TO 100) AS INTEGER
  2. n(2) = 2
  3. FOR i = 3 TO 100 STEP 2
  4.     FOR j = 3 TO SQR(i) STEP 2
  5.         IF i / j = INT(i / j) THEN n(i) = j: EXIT FOR
  6.     NEXT
  7.     IF n(i) = 0 THEN n(i) = i
  8. PRINT "primes between 2 and 100:"
  9. FOR i = 2 TO 100
  10.     IF n(i) = i THEN PRINT i,
  11. PRINT: PRINT "Lowest dividers:"
  12. FOR i = 2 TO 100
  13.     IF n(i) <> i THEN IF n(i) = 0 THEN PRINT i; " 2", ELSE PRINT i; " "; n(i),
  14.  

from 2 to 100 the time difference is minimal, but using _integer64 from 2 to 1.000.000 the difference can be visible.
Thanks to reply to me
Programming isn't difficult, only it's  consuming time and coffee

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #34 on: November 10, 2020, 12:36:54 pm »
@dear Bplus
I think that we can save CPU work using STEP 2 in the external FOR NEXT loop.
In fact the only even Prime number is 2. And with this we reduce to half the work to accomplish by the program.
And as consequence the inner FOR NEXT starts from 3 to SQR(i) step 2 because if we're processing only odd numbers there is no matter to divide by 2 and by its multiplies.
so your code can be converted into this one
Code: QB64: [Select]
  1. REDIM n(2 TO 100) AS INTEGER
  2. n(2) = 2
  3. FOR i = 3 TO 100 STEP 2
  4.     FOR j = 3 TO SQR(i) STEP 2
  5.         IF i / j = INT(i / j) THEN n(i) = j: EXIT FOR
  6.     NEXT
  7.     IF n(i) = 0 THEN n(i) = i
  8. PRINT "primes between 2 and 100:"
  9. FOR i = 2 TO 100
  10.     IF n(i) = i THEN PRINT i,
  11. PRINT: PRINT "Lowest dividers:"
  12. FOR i = 2 TO 100
  13.     IF n(i) <> i THEN IF n(i) = 0 THEN PRINT i; " 2", ELSE PRINT i; " "; n(i),
  14.  

from 2 to 100 the time difference is minimal, but using _integer64 from 2 to 1.000.000 the difference can be visible.
Thanks to reply to me

ciao @TempodiBasic
questo è quello che intendeva bplus?
devo studiare REDIM Non so cosa fa questa istruzione poi vedo un for to nex annidato credo si chiama cosi annidato oppure indentato,
poi un print numeri primi compresi tra 2 e 100
un if che verifica la matrice se è uguale a i dentro al ciclo for è seguenziale un altro for con all interno un if
ci studio un po su questo codice che non mi è chiaro alcune cose che devo capire
grazie per il tuo aiuto
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: WHILE AND DO LOOP CYCLES
« Reply #35 on: November 10, 2020, 01:13:08 pm »
@dear Bplus
I think that we can save CPU work using STEP 2 in the external FOR NEXT loop.
In fact the only even Prime number is 2. And with this we reduce to half the work to accomplish by the program.
And as consequence the inner FOR NEXT starts from 3 to SQR(i) step 2 because if we're processing only odd numbers there is no matter to divide by 2 and by its multiplies.
so your code can be converted into this one
Code: QB64: [Select]
  1. REDIM n(2 TO 100) AS INTEGER
  2. n(2) = 2
  3. FOR i = 3 TO 100 STEP 2
  4.     FOR j = 3 TO SQR(i) STEP 2
  5.         IF i / j = INT(i / j) THEN n(i) = j: EXIT FOR
  6.     NEXT
  7.     IF n(i) = 0 THEN n(i) = i
  8. PRINT "primes between 2 and 100:"
  9. FOR i = 2 TO 100
  10.     IF n(i) = i THEN PRINT i,
  11. PRINT: PRINT "Lowest dividers:"
  12. FOR i = 2 TO 100
  13.     IF n(i) <> i THEN IF n(i) = 0 THEN PRINT i; " 2", ELSE PRINT i; " "; n(i),
  14.  

from 2 to 100 the time difference is minimal, but using _integer64 from 2 to 1.000.000 the difference can be visible.
Thanks to reply to me

Ah! I see just skip the 2's if n(i) is still 0 then it was divisible by 2, nice one! @TempodiBasic

Yes, there are faster ways to do this when you have a much higher limit to go, I just wanted to get the ball rolling with some code.
« Last Edit: November 10, 2020, 01:15:36 pm by bplus »

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #36 on: November 14, 2020, 12:29:25 pm »
@Kiara87 - Have you solved the task in the meantime? This book is good, maybe it is in Italian too. "MS-DOS QBasic" Handbuch für Programmierer, The Waite Group. Theory and practice. Try the tasks right away. And then think up your own tasks, that's how you learn to program.
http://www.antonis.de/qbuecher/index.htm

Here is another program for finding prime numbers with indication of the number up to input, without an array. From 10,000 the formatting gets out of hand.

Code: QB64: [Select]
  1. 'Output of all prime numbers up to input - 14. Nov. 2020
  2.  
  3.  
  4. DIM eingabe AS LONG, anzahlprim AS LONG
  5. DIM primzahl AS LONG, j AS LONG
  6.  
  7. PRINT "Ausgabe aller Primzahlen bis zur Eingabe"
  8. INPUT "Alle Primzahlen ausgeben bis: ", eingabe
  9.  
  10. anzahlprim = 0
  11. FOR primzahl = 2 TO eingabe STEP 1
  12.   j = 2
  13.   DO WHILE primzahl MOD j <> 0
  14.     j = j + 1
  15.   LOOP
  16.   IF j = primzahl THEN PRINT USING "#####"; primzahl;
  17.  
  18.   IF j = primzahl THEN anzahlprim = anzahlprim + 1
  19. PRINT USING "Bis #### gibt es #### Primzahlen"; eingabe, anzahlprim
  20.  
  21.  
« Last Edit: November 14, 2020, 12:30:37 pm by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #37 on: November 14, 2020, 05:35:01 pm »
@Kiara87 - For explanation:
For each number -primzahl- it is tried out whether there are other positive factors than 1 and -primzahl-. This is how you find out whether -primzahl- is a prime number or not. Since a divisor of -primzahl- cannot be greater than -primzahl- so you only need to check the -zahlenbereich- between 1 and -primzahl-.
-zahlenbereich- is Number range.

I hope the program is more understandable now.

Code: QB64: [Select]
  1. 'Ausgabe aller Primzahlen bis Eingabe - 14. Nov. 2020
  2.  
  3.  
  4. DIM eingabe AS LONG, anzahlprim AS LONG
  5. DIM primzahl AS LONG, zahlenbereich AS LONG
  6.  
  7. PRINT "Ausgabe aller Primzahlen bis zur Eingabe"
  8. INPUT "Alle Primzahlen ausgeben bis: ", eingabe
  9.  
  10. anzahlprim = 0
  11. FOR primzahl = 2 TO eingabe STEP 1
  12.   zahlenbereich = 2
  13.   DO WHILE primzahl MOD zahlenbereich <> 0
  14.     zahlenbereich = zahlenbereich + 1
  15.   LOOP
  16.   IF zahlenbereich = primzahl THEN PRINT USING "#####"; primzahl;
  17.  
  18.   IF zahlenbereich = primzahl THEN anzahlprim = anzahlprim + 1
  19. PRINT USING "Bis #### gibt es #### Primzahlen"; eingabe, anzahlprim
  20.  
  21.  
« Last Edit: November 14, 2020, 05:39:28 pm by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #38 on: November 14, 2020, 06:06:07 pm »
@Kiara87 - For explanation:
For each number -primzahl- it is tried out whether there are other positive factors than 1 and -primzahl-. This is how you find out whether -primzahl- is a prime number or not. Since a divisor of -primzahl- cannot be greater than -primzahl- so you only need to check the -zahlenbereich- between 1 and -primzahl-.
-zahlenbereich- is Number range.

I hope the program is more understandable now.

Code: QB64: [Select]
  1. 'Ausgabe aller Primzahlen bis Eingabe - 14. Nov. 2020
  2.  
  3.  
  4. DIM eingabe AS LONG, anzahlprim AS LONG
  5. DIM primzahl AS LONG, zahlenbereich AS LONG
  6.  
  7. PRINT "Ausgabe aller Primzahlen bis zur Eingabe"
  8. INPUT "Alle Primzahlen ausgeben bis: ", eingabe
  9.  
  10. anzahlprim = 0
  11. FOR primzahl = 2 TO eingabe STEP 1
  12.   zahlenbereich = 2
  13.   DO WHILE primzahl MOD zahlenbereich <> 0
  14.     zahlenbereich = zahlenbereich + 1
  15.   LOOP
  16.   IF zahlenbereich = primzahl THEN PRINT USING "#####"; primzahl;
  17.  
  18.   IF zahlenbereich = primzahl THEN anzahlprim = anzahlprim + 1
  19. PRINT USING "Bis #### gibt es #### Primzahlen"; eingabe, anzahlprim
  20.  
  21.  
very understandable 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 NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: WHILE AND DO LOOP CYCLES
« Reply #39 on: November 15, 2020, 11:44:24 pm »
Ok I like
 
DO
LOOP
****
You can do this only if there is a good reason
DO
    IF whatever GOTO label
LOOP
label:
****

Otherwise

DO
    IF whatever EXIT DO
LOOP
« Last Edit: November 15, 2020, 11:45:27 pm by NOVARSEG »