Shop OBEX P1 Docs P2 Docs Learn Events
algorithm problem with PBASIC (MORE CLEARLY) — Parallax Forums

algorithm problem with PBASIC (MORE CLEARLY)

HillsHills Posts: 13
edited 2004-12-13 02:21 in BASIC Stamp
Hi guys (specially K de Jong)
·
I figured out that I badly explained my problem. It's not a restart or writing something in the EEPROM. It's simply a looping in the program·who continuously
changes·some values ( the elements of the array) ·and stops looping when a specified value is attented. Some people like K de Jong asked me to show the code to more understand.·So here is the program :
·
·
·' {$STAMP BS2}
·
·n·········· CON····· 5
·vecteur···· VAR····· Byte(n)
·compt······ VAR····· Nib
·p·········· VAR····· Nib(n)
·j·········· VAR····· Byte
·t·········· VAR····· Nib(n-1)
·minimum···· VAR····· Byte
·i·········· VAR····· Nib
·Result····· VAR····· Byte
·number····· VAR····· Nib


·vecteur(0)=255
·vecteur(1)=15
·vecteur(2)=255
·vecteur(3)=255
·vecteur(4)=4

·
·FOR j = 1 TO n
· ·IF NOT 2 <= j THEN NIL
·· IF NOT vecteur(j-1) < 255 THEN NIL
·· p(j-1)=1
·· lus :
·NEXT

·
·t(0)=2
·t(1)=3
·t(2)=4
·t(3)=5

·
·luse :
·IF NOT t(0) = 0 THEN Calcul·····

·
FOR compt = 0 TO n-1
· ·DEBUG ? p(compt)
·NEXT

·
END
·
·Calcul :
· ·minimum = 255
·· · FOR i = 0 TO n-1
··· ·· IF vecteur(i) < minimum THEN label
···· · lux :
···· NEXT
····· ·········· FOR i = 0 TO n-1
······ ············· IF vecteur(i) = minimum THEN labeld
······· ········ NEXT

·· luxe :
··· ·· FOR j = 0 TO n-1
···· ···· BRANCH i,[noparse][[/noparse]case_0,case_1,case_2,case_3,case_4]
····· ····· luss :
······ ······· IF Result = 255 THEN jump
······· ······ IF Result = 0·· THEN jump
········ ····· IF vecteur(j) > vecteur(i) + Result THEN change
········· · jump :
······ NEXT

····· ·GOTO luse
·IF NOT t(0) = 0 THEN Calcul
·FOR compt = 0 TO n-1
· ·DEBUG ? p(compt)
·NEXT

·END
·
·NIL :
· ·p(j-1)=0
·GOTO lus

·
·label :
· ·minimum = vecteur(i)
·GOTO lux

·labeld :
· ·t(i-1) = 0
·GOTO luxe

·
·case_0 :
· ·LOOKUP j,[noparse][[/noparse]0,15,255,255,4],Result
·GOTO luss

·
·case_1 :
· ·LOOKUP j,[noparse][[/noparse]255,0,255,255,255],Result
·GOTO luss

·
·case_2 :
· ·LOOKUP j,[noparse][[/noparse]255,3,0,2,255],Result
·GOTO luss

·
·case_3 :
· LOOKUP j,[noparse][[/noparse]10,3,255,0,255],Result
·GOTO luss

·
·case_4 :
· LOOKUP j,[noparse][[/noparse]255,255,7,5,0],Result
·GOTO luss

··································
·change :
· ·vecteur(j) = vecteur(i)+Result
·· p(j)=i+1
·GOTO jump

·
·
See.·If you are very good in programming, you would see that the program is the algorithm of Dijkstra ( an algorithm who can search the most short track in a graph ).
But it's not important to understand how the algorithm works. The only thing I want you guys to understand is the looping :··IF NOT t(0) = 0 THEN Calcul·····
·
So·everytime this command see that the array· " t( 0 ) " isn't equal to " 0 ",·the·program is supposed·to jump to the label " Calcul " to make a lot of operations and· return to the command above·( IF NOT t(0) = 0 THEN Calcul· ) with the new values of the arrays ( the values·are supposed to be·change during in the label Calcul ). If the array· "t( 0 )" still isn't equal to " 0 ", it jumps once again to the label Calcul and makes this looping continuously until the array "t( 0 ) " is equal to " 0 ". That's what I ment when I said " it always restart " in the past. The problem is that the arrays··" vecteur " and· " p " still keeps the same values. For example : the array " vecteur " still keeps these values :

·vecteur(0)=255
·vecteur(1)=15
·vecteur(2)=255
·vecteur(3)=255
·vecteur(4)=4

·
Consequently, the algorithm doesn't works and can't evolve·because the values of the elements don't want to change with the label " change " :

·change :
· ·vecteur(j) = vecteur(i)+Result
·· p(j)=i+1
·GOTO jump

·
·I doubt that you guys understand and have a solution for this problem but if you have one please let me know. If I don't find a solution, I would·have to·write and change the whole program and it would be very bad for me because I don't have time. Let me know if you still don't understand.

Thank you

Post Edited (Hills) : 12/12/2004 11:34:06 PM GMT

Comments

  • K de JongK de Jong Posts: 154
    edited 2004-12-13 00:42
    Hi Hill,

    I don't see anything wrong in your code. Maybe your variable t(o) just does not get to zero for some reason. Maybe it jumps to 'negative' directly and rolls over to 65535. I don't know.

    Did you try the following statement:

    luse :
    DEBUG t(0),CR
    IF NOT t(0) = 0 THEN Calcul

    You can yuse DEBUG statements on other places in your code (inside Calcule i.e.) to see what is going on behind the curtains.

    Try it and yoou will get a picture and, hopefully, solve the problem from there.

    Regards and lots of success,

    Klaus
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2004-12-13 01:22
    Hi Hills-

    Instead of using the form of the statement that you are presently using:

    ·····IF NOT t(0) = 0 then calcul

    Try this format instead and see if it works then:

    ···· IF t(0) <> 0 then calcul

    I'm not sure that it will make any difference, but the second form is a bit more conventional.

    Regards,

    Bruce Bates
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2004-12-13 02:21
    To follow up on comments by Klaus and Bruce, there are PBASIC structures that can make the program easier to follow. For example,
    FOR j = 1 TO n
    · ·IF NOT 2 <= j THEN NIL
    ·· IF NOT vecteur(j-1) < 255 THEN NIL
    ·· p(j-1)=1
    ·· lus :
    ·NEXT

    can be written (I think!?) as:

    FOR j = 1 TO n
    · ·IF j<2 OR vecteur(j-1)=255 THEN ·p(j-1)=0 ELSE·· p(j-1)=1
    ·NEXT

    That gets rid of two spagetti labels "nil" and "lus" and the NOT statements, which I too find mentally more than I can handle on a Sunday evening!

    And this statement:
    luse: IF NOT t(0) = 0 THEN Calcul
    Can be simplified and shortened (runs faster!) to this
    luse: IF t(0) THEN Calcul

    And this statement:
    ··· ·· IF vecteur(i) < minimum THEN label
    Can be simplified to
    ··· ·minimum = minimum MAX vecteur(i)
    which gets rid of the spagetti labels "label" and "lux", by using the PBASIC operator, MAX, which is shorter and faster. It may seem counterintuitive to use the MAX operator to keep a minimum, but that is the way it works.

    You get the drift. The PBASIC statements SELECTCASE are just right for the branching. I think with a hour of work you could shorten this program and makes its structure much more clear, and then maybe the problem would leap out at you. I don't see where the specific bug is, but I'm easily lost in the structure of the program as it stands.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.