PDA

View Full Version : How are arrays used?



NO2K
- 28th May 2007, 17:28
I am clueless as to how one uses arrays. My idea is to program a pic to do the Bingo game. Does anyone have any ideas on this?

gandora
- 29th May 2007, 00:32
I am clueless as to how one uses arrays. My idea is to program a pic to do the Bingo game. Does anyone have any ideas on this?


Alright thats a general question but I'll try and answer.

first off choose a length of array and type of data.

nameofarray VAR unitsize[numberofunits]

lets say you want row one to be 12 bytes wide.

rowone var byte[12]

Now okay thats all fine and dandy right? Now you want to populate it.

There are two ways you can go one segment at a time like this.

rowone[0] = "b"

Or you can populate an array serially.

serin pin,baud,[ STR rowone\1]

this would wait until one byte is recieved and put it in the first position in the array. Lets say you wanted to put it in the second position.


serin pin,baud,[ STR rowone(1)\1]

This would take one byte serially, and transfer it into the second unit of the array.

ill give one more example this time using more then one byte.

serin pin,baud,[ STR rowone(6)\6]

Now this will take the first six bytes it recieves, and will put it in the array starting at the 7th byte, basically byte one will go in rowone[5], byte two rowone[6], all the way up to rowone[11]

Hopefully that helps.

NO2K
- 30th May 2007, 22:03
Hi Gandora: Thanks for the reply. I was thinking I could make an array large enough to hold all the bingo numbers, then use the random number command to pick one of those spots, but after reading the number in a specific location in the array, store the number someplace else, rewrite it to zero (because it has already been used), then go to the next number. When bingo is reached, then need to go back and verify all the numbers that were used. Not real sure how to do that, but guess I'll have to experiment.
regards, NO2K

skimask
- 31st May 2007, 00:24
Hi Gandora: Thanks for the reply. I was thinking I could make an array large enough to hold all the bingo numbers, then use the random number command to pick one of those spots, but after reading the number in a specific location in the array, store the number someplace else, rewrite it to zero (because it has already been used), then go to the next number. When bingo is reached, then need to go back and verify all the numbers that were used. Not real sure how to do that, but guess I'll have to experiment.
regards, NO2K

Use a bit array instead of whole bytes...(warning, not 'REAL' code here)...using the bits as 'flags' as to whether or not a number has been called...

b var bit[10]
i var bit[10]
n var bit[10]
g var bit[10]
o var bit[10]
'all initially set to zero's

'b1 is called
b[1] = 1
'b1 is set to 1, therefore it's used

if b[2] = 1 then goto it's_used
if b[2] = 0 then goto it's_not_used

The only problem with this is you'll have to use 2 random numbers, one to pick which letter, and one to pick which number 'inside' that letter...

SteveB
- 31st May 2007, 01:07
Use a bit array instead of whole bytes

Nice idea


The only problem with this is you'll have to use 2 random numbers, one to pick which letter, and one to pick which number 'inside' that letter...

Not really, if there are 10 possible numbers for each letter. Use random number between 0 and 59. Then "tens" place becomes B·I·N·G·O (0·1·2·3·4·5), and the "ones" place becomes 1·2·3·...9·10. The algorithm would just need to add 1 to the the value. If there are more than 10 numbers for each letter, then it changes things. I just can't remember how many numbers each letter has. If it is a multiple of 10, you can extend the concept, so 0-19 is B1-B20, 20-39 is I1-I20, etc.

Extending your idea further, you could use an array of bytes, that will encompass all possible numbers. If there are 6*30=180 possibilites, use:

Total var BYTE[23] '180+4

This puts all the possibilities in one continious block.
Then, with one random number you can access an individual bit with

Total.0(bit) 'Where bit is a number between 0 and 179, for 180 possible options. Here's the explaination for those who need it. (http://www.picbasic.co.uk/forum/showpost.php?p=2017&postcount=1)

Then code the algorithm to 'interpert' the bit as B5, I2, N7, etc.

SteveB

NO2K
- 1st June 2007, 15:33
My thanks to Skimask and Steve B for their advice. For Bingo, there really is no need to use the letters. Each letter has 15 places, i.e. B goes from 1 to 15, I goes from 16 to 30, N 31 to 45, G 46 to 60, and finally O from 61 to 75. Of course, as with any other problem, there are probably a lot of ways to do the program, but I like the idea of bit arrays and will try to experiment with it.