logic problem
grkblood13
Posts: 31
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:
code:
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?
·
debug:
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
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.
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
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
If BYTE variables, anything above 127 might be considered 'negative' -- I hope that wouldn't be a problem...
Post Edited (grkblood13) : 4/17/2007 5:42:20 PM GMT
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?