Avoiding too many GOSUBs, possible?


+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Avoiding too many GOSUBs, possible?

    Hello.
    I have some code, which, based on value of the X variable (changes from 1 to 30)
    Should call some GOSUBs as shown below:

    Code:
    IF X=1 THEN GOSUB ONE
    IF X=2 THEN GOSUB ONE: GOSUB TWO
    IF X=3 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE
    IF X=4 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE
    IF X=5 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE: GOSUB TWO
    IF X=6 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE: GOSUB TWO: GOSUB THREE
    This is from 1 to 6, but I have to do it up to 30, and there it gets way too long. So maybe there's some way to make things less complex?

    One way I see it to create another set of GOSUBs, each for group of 3, like

    [code]
    ONE-X:
    GOSUB ONE
    RETURN

    TWO-X:
    GOSUB ONE: GOSUB TWO
    RETURN

    THREE-X:
    GOSUB ONE: GOSUB TWO: GOSUB THREE
    RETURN
    [code]

    But how to "split" the X in the proper way?

    So for example, when X=8, THREE-X will be called 2 times and ONE-X - 1 times?

  2. #2
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    [QUOTE=CuriousOne;154813]Hello.
    I have some code, which, based on value of the X variable (changes from 1 to 30)
    Should call some GOSUBs as shown below:

    Code:
    IF X=1 THEN GOSUB ONE
    IF X=2 THEN GOSUB ONE: GOSUB TWO
    IF X=3 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE
    IF X=4 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE
    IF X=5 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE: GOSUB TWO
    IF X=6 THEN GOSUB ONE: GOSUB TWO: GOSUB THREE: GOSUB ONE: GOSUB TWO: GOSUB THREE
    I am just sitting here with a cup of coffee and wondering if this might work?

    Code:
    FOR LP = 1 to X/3
         [If X//3 = 0 THEN] GOSUB ONE: GOSUB TWO: GOSUB THREE
    ? NEXT LP
         If X//3 = 1 THEN GOSUB ONE
         If X//3 = 2 THEN GOSUB ONE: GOSUB TWO
    ? NEXT LP
    Looking again, it may be that I have misread your intent. I think then that a bare loop is not right and you might complete the loop portion before evaluating the remainder. If this is the case, no "IF" is required (The x//3 = 0 conditional]
    Sorry that I'm not able to test this. In my mind it was so clear and produced the pattern illustrated with beautiful simplicity. Perhaps it might still be of some use.
    Last edited by Amoque; - 22nd December 2023 at 14:28.

  3. #3
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    I need somehow to break down the incoming number into 3s 2s and 1s, and giving them priority....

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    I Assume your objective is to get something like this

    Code:
    READY
    
    
    1	1
    2	1,2
    3	1,2,3
    4	1,2,3,1
    5	1,2,3,1,2
    6	1,2,3,1,2,3
    7	1,2,3,1,2,3,1
    8	1,2,3,1,2,3,1,2
    9	1,2,3,1,2,3,1,2,3
    10	1,2,3,1,2,3,1,2,3,1
    11	1,2,3,1,2,3,1,2,3,1,2
    12	1,2,3,1,2,3,1,2,3,1,2,3
    13	1,2,3,1,2,3,1,2,3,1,2,3,1
    14	1,2,3,1,2,3,1,2,3,1,2,3,1,2
    15	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    16	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1
    17	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2
    18	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    19	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1
    20	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2
    21	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    22	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1
    23	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2
    24	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    25	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1
    26	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2
    27	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    28	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1
    29	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2
    30	1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
    So for example, when X=8, THREE-X will be called 2 times and ONE-X - 1 times?
    perhaps should be
    So for example, when X=8, THREE-X will be called 2 times and ONE-X - 3 times and TWO-X - 3 times?

    I need somehow to break down the incoming number into 3s 2s and 1s, and giving them priority....
    priority over what exactly ?
    Warning I'm not a teacher

  5. #5
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    Yes my code looks like that
    Priority I mean 10 can be achieved with 1+1+1+1+1+1+1+1+1+1 combo, but I need it to be done with 3+3+3+1

    So I need a formula which will break down the incoming number into 3s 2s and 1s.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    So what happened to the twos*s then
    you Need to state your objectives more effectively
    Warning I'm not a teacher

  7. #7
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    So if output is 11, then it will be 3+3+3+2

    8=3+3+2
    9=3+3+3
    10+3+3+3+1
    11=3+3+3+2
    12=3+3+3+3

    But these are sub-routines, which already include other statement, s so 3 here is actually doing 1+2+3, 2 is doing 1+2, and 1 is doing just 1

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    MY code that produced the printout in #4 called digit 1 ,2 and 3 subs [c1 c2 c3 ]as needed when the input is broken down

    Name:  Untitled.jpg
Views: 99
Size:  110.2 KB
    Warning I'm not a teacher

  9. #9
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    Thanks, but there's no code in #4?

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Avoiding too many GOSUBs, possible?

    you can remove all the gosubs entirely with a bit of thought to get the same printout

    Code:
    LOOPY:        FOR X = 1 TO 30
            DEBUG 13,10,DEC X,9
            GOSUB CX
        NEXT
        PAUSE 5000    
    GOTO LOOPY 
    CX:
         XX = x
         y = 0
         WHILE XX  
             DEBUG  49 + y
             y = y + 1
             if y == 3 then y = 0
             Xx = Xx - 1
             IF  XX  THEN DEBUG ","
         WEND
     RETURN
    Warning I'm not a teacher

Similar Threads

  1. Avoiding of 255 IF-THEN in the code, possible?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 23rd September 2022, 12:03
  2. Line Following, Obstacle Avoiding , and Victim Finding
    By cryptex in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th June 2011, 16:34
  3. Avoiding getting stuck in a loop
    By AndrewC in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st July 2008, 11:41
  4. variables and gosubs???
    By guest_05 in forum mel PIC BASIC Pro
    Replies: 38
    Last Post: - 19th November 2006, 02:46
  5. cancelling gosubs
    By rossfree in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 21st February 2005, 18:15

Members who have read this thread : 16

You do not have permission to view the list of names.

Posting Permissions

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