Shop OBEX P1 Docs P2 Docs Learn Events
need some help with code — Parallax Forums

need some help with code

wiiiiiiiiiiiwiiiiiiiiiii Posts: 2
edited 2009-04-07 02:44 in BASIC Stamp
hey all. i need some help.


i need to make it so i can enter 2 numbers and then have it add all the numbers in between.

ex

1 and 5

1+2+3+4+5=15.

thanks.

Comments

  • SRLMSRLM Posts: 5,045
    edited 2009-04-06 02:50
    Pseudo code:

    for i = |(a -b)|
    total += the smaller of (a,b) + i
  • wiiiiiiiiiiiwiiiiiiiiiii Posts: 2
    edited 2009-04-06 10:40
    can you explain the code? lol
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-06 14:00
    This is a very very simple problem. Obviously it's a project for a class and it would be irresponsible of us to give you the answer.

    The "What's a Microcontroller?" tutorial is a good introduction to programming, particularly for the Parallax Stamps and Stamp Basic.
    You should download it and work through the exercises (www.parallax.com/tabid/535/Default.aspx).
    You will also need a copy of the Stamp Basic Manual (www.parallax.com/tabid/440/Default.aspx).

    As with any programming problem, it's helpful to break down the problem into individual steps. You've stated that you have to be
    able to enter 2 numbers, then add all the numbers in-between. Those are your first individual steps. What would be the next
    breakdown of those into sub-steps? You need to thoroughly understand at least the DEBUG / DEBUGIN / FOR / IF / GOTO statements
    and the assignment statement (=) and arithmetic (+ - * /) and comparison (< = > <> <= >=) operators.
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2009-04-06 15:59
    wiiiiiiiiiii,


    SRLM's Pseudo code just set's up a for next loop to increment between the two numbers you want to add.
    Mike's suggestion on reading and understanding the DEBUG / DEBUGIN, etc. is also a good start.

    Once you have an understanding on how the DEBUGIN works you can enter your numbers that you want to test.
    The DEBUG command will allow you to view the results.


    A math trick to figure out the series addition of any set of numbers without the FOR/NEXT loop can then be applied...

    Total = (N*(N+1))/2

    where N = the maximum number in the series. If you want to find the difference between two series sets...

    Difference = (N1*(N1+1))/2 - (N2*(N2+1))/2

    Where N1 is larger than N2 in the series set.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 4/7/2009 3:55:09 AM GMT
  • GWJaxGWJax Posts: 267
    edited 2009-04-07 02:44
    Here is one that is simple for you with less math involved

    Enjoy

    Jax

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' Variables

    FirstNum VAR Word
    SecondNum VAR Word
    Total VAR Word
    i VAR Word

    Total=0

    Main:
    DEBUG CLS, "Enter First Number: "
    DEBUGIN DEC3 FirstNum
    DEBUG CR


    DEBUG "Enter Second Number: "
    DEBUGIN DEC3 SecondNum
    DEBUG CR


    ' Add Numbers
    FOR i = FirstNum TO SecondNum
    Total = Total + i
    NEXT
    DEBUG "All numbers added = ", DEC Total, CR
    DEBUG " Press ENTER to re-run the program "
    DEBUGIN i
    Total = 0
    GOTO Main

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If a robot has a screw then it must be romoved and hacked into..
Sign In or Register to comment.