Shop OBEX P1 Docs P2 Docs Learn Events
logic problem — Parallax Forums

logic problem

grkblood13grkblood13 Posts: 31
edited 2007-04-17 18:10 in BASIC Stamp
well, i have one day to finish this project and this is the last problem im getting. below is a code that will seperate a specific number into 3 different bins. the program is supposed to the number in the lowest bin. for some reason it doesnt want to read some of the statements though. i went ahead and debugged some values since running this program without a a/d convertor would be somewhat difficult.

debug:
debug.jpg

code:
IF ((total1 < total2) & (total1 < total3)) THEN add1       'add weight to pit1'
IF ((total2 < total1) & (total2 < total3)) THEN add2       'add weight to pit2'              'CODE SHOULD GO HERE!!!!
IF ((total3 < total1) & (total3 < total2)) THEN add3       'add weight to pit3'
IF (total1 = total2 = total3)              THEN add2       'they equal so add to pit2'
IF (total1 = total2)                       THEN add2       '(1&2 is less than 3)'
IF (total1 = total3)                       THEN add1       '(1&3 is less than 2)'
IF (total2 = total3)                       THEN add3       '(2&3 is less than 1


notice the last section is added to· bin one instead of· bin two. my only guess is that for some reason its running through each IF statement and going straight to add1 becuase thats directly below these statements.

can someone please help me?


·

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-17 15:01
    'States:
    1. total1 < total2 and total3 -> Add1
    2. total1 > total2, but total2 < total3 -> Add2
    3. total1 > total3, but total2 > total3 -> Add3

    1. -- total1 .... total2 .... total3

    2. -- total2 ... total1 ... total3

    3. -- total3 ... total2
    ... total1

    Conclusion: I think your logic is hosed.

    If you want to place 'total1', then your IF statements should ALWAYS use 'total1'.
    And is total2 ALWAYS less than total3?

    1. -- total1 ... total2 .... total3 -> IF total1 < total2 AND total1 < total3

    2. -- total2 ... total1 ... total3 -> IF total1 => total2 AND total1 < total3

    3. -- total2 ... total3 ... total1 -> IF total1 > total2 AND total1 => total3

    Oh, and checking in an "IF" if two quantities things are actually EQUAL to each other tends to be true very seldom, except in the special cases of '1' or '0'.

    I find keeping the variables in the same places, and changing the less-than and greater than symbols, much easier to get the logic correct.
  • grkblood13grkblood13 Posts: 31
    edited 2007-04-17 15:39
    i dont see how its hosed though. it messed up on the fourth number. so the logic is based on the values of total1, 2, and 3 after the 3rd number is placed.

    so we have...

    total1=220

    total2=121

    total3=122

    now to the code

    IF ((220 < 121) & (220 < 122)) THEN add1··· neither of these hold true so it should go to the next line

    IF ((121 < 220) & (121 < 122)) THEN add2····both of these are true so it should add to 'add2', but it doesnt, and I dont know why
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-17 16:32
    IF ((total1 < total2) & (total1 < total3)) THEN add1 'add weight to pit1'
    IF ((total2 < total1) & (total2 < total3)) THEN add2 'add weight to pit2' 'CODE SHOULD GO HERE!!!!

    Well, first of all, the "&" operator is a 'bitwise' 'and'. You probably want the 'logical' and, which is the keyword "AND".

    If total1 == 220, and total2 == 121, and total3 == 122

    if (220 < 121) and (220 < 122) THEN ... -- is false, since 220 > 121
    if (121 < 220) and (121 < 122) THEN ... -- you're right, this should be true. Convert your '&' to 'AND' and see what you get.

    Edit: And your logic is a little suspect, because your second clause says in effect:
    IF (B < A), and (B < C), THEN do something -- But this does not say anything about the relationship between A and C.· It could be B..A..C, or it could be B..C..A.· That's the logic flaw I was mentioning.·

    Post Edited (allanlane5) : 4/17/2007 4:42:55 PM GMT
  • grkblood13grkblood13 Posts: 31
    edited 2007-04-17 16:35
    i originally had AND, i just swapped it out though b/c with AND it was always placing it in section 2
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-17 17:00
    Are these WORD variables, or BYTE variables?

    If BYTE variables, anything above 127 might be considered 'negative' -- I hope that wouldn't be a problem...
  • grkblood13grkblood13 Posts: 31
    edited 2007-04-17 17:01
    they are words, and actually i hope that woulda been the problem b/c that woulda been a quick fix. ha
  • grkblood13grkblood13 Posts: 31
    edited 2007-04-17 17:21
    i just went through this writing numbers on a piece of paper and it works if you do it by hand, im beginning to think something is wrong with the way it is written in the code. is there any other way to write this?

    Post Edited (grkblood13) : 4/17/2007 5:42:20 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-17 17:50
    Well, note all you've given us are examples, in code, of what you're trying to do.
    So we don't really know the significance of total1, total2, and total3.

    It LOOKS like you're trying to use Total2 and Total3 to define the boundaries between 'buckets', and the value of 'Total1' needs to increment a count in bucket1, bucket2, or bucket3.

    But all that is surmise. All I really know is that you've tried a few IF statements, that apparently aren't doing what you want them to do.

    So what ARE you trying to do? What IS the purpose of Total1, Total2, and Total3?
  • grkblood13grkblood13 Posts: 31
    edited 2007-04-17 18:10
    im going to start a new thread with an entire example code lable "scale code"
Sign In or Register to comment.