need some help with code
wiiiiiiiiiii
Posts: 2
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.
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
for i = |(a -b)|
total += the smaller of (a,b) + i
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.
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
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..