![]() |
What's wrong with this code? - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16) +---- Thread: What's wrong with this code? (/showthread.php?tid=337) |
What's wrong with this code? - LTee - 03-24-2011 Okay, this is driving me mad. :-D I wrote a little routine to 'shuffle' the numbers 1 to 10 into an array, randomly. It's not exactly the best way of doing this but it was supposed to be a quick test. Regardless, it doesn't work. Or rather, it does work - the first time you run it. The second loop of the same code never completes and I can't decide if I found a bug or if I've made a mistake. Here's the code: Code: dim levelMap(10) as UBYTE I've stuck a load of debug info in there so that you can see what it's doing. Basically it does this: 1. Clear the array to all zeroes 2. For each number, 1 to 10.... 3. Look in a random array position to see if it's zero. 3a. if it isn't, loop around to 3 and try a different one 3b. if it is, write the number into that array position and move on to the next number 4. The result should be the numbers 1 to 10 in a random order in the array, after a variable number of iterations. First run, no problem. Second run, although the debug code shows that the array was cleared, the do...loop until loop never completes - by the time it's checking the array within that loop it contains the OLD values again, despite me having zeroed them out just a few seconds earlier. Am I missing an obvious goof? :-D Re: What's wrong with this code? - LCD - 03-24-2011 Code: for lm = 1 to 10 Maybe if you change it to: Quote:for lm = 1 to 10Then it will really put a zero in each position of the array. Look also here: Code: for lm = 1 to 10 Re: What's wrong with this code? - LTee - 03-24-2011 Ha ha, you're right! What a clown I am. :-D That'll teach me to copy/paste stuff from one file to another! Thanks, LCD! Re: What's wrong with this code? - boriel - 03-24-2011 Another pending feature: a command-line flag to enforce the user to *declare* (DIM) any used variable. This way, the levelMap(n) should complain about undeclared "n" var (or 2 Variable not Found) ;-) Free Basic also has this feature. I will put it, but need to re-re-check everything in the 1.2.8 version which now seems to be relatively stable at the moment... Re: What's wrong with this code? - LTee - 03-24-2011 I'd like that! That's not the first time I've tripped up because of a bad copy/paste, and occasionally I've forgotten to declare something entirely and ended up with a float when a ubyte would've done. And I'd agree on 1.2.8, by the way - it seems to be working great for me! |