Qbasicnews.com

Full Version: Questions concerning arrays..
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone! I have some question concerning arrays..

hei = img%(n%)

The question is the value of n%, if you for example let n% be 1 you get the height of a array.. Now what will happend if i use other numbers is there somewhere i can lookup that?
What exactly does the n% do and how does it work? Can you clear this out with me..

And one more thing, if i wanna poke a value in a array.. I tried the way you poke on the screen but it won't work, when i try to put the array i get a error..

DEF SEG = VARSEG(img%(1))
POKE ((y*wid)+x),c

That didn't work.. And by the way that 1 in VARSEG(img%(1)) what function does it fulfil?

Peace
If you create an array like this:
Code:
DIM array%(100)
That creates 101 variables. array%(0), array%(1), and so on to array%(100)
So that 1 there tells it you're talking about #1 - the second variable in the array (zero is the first).

If you GET an image into the array, like this:
Code:
SCREEN 13
GET (1, 1)-(10, 10), array
And then take array%(0) and divide it by 8, you get the width of the image, like so:
Code:
PRINT "The width of the image is"; array%(0) / 8
And array%(1) is the height of the image.

Quote:And one more thing, if i wanna poke a value in a array.. I tried the way you poke on the screen but it won't work, when i try to put the array i get a error..

DEF SEG = VARSEG(img%(1))
POKE ((y*wid)+x),c

That didn't work.. And by the way that 1 in VARSEG(img%(1)) what function does it fulfil?
Every variable has a segment and an offset. Together, the segment and offset make up the variable's address in memory. The VARSEG(img%(1)) basically means this: get the segment of variable #1 (the second variable - remember the first is #0) in the img% array. Then it's making that the DEFault SEGment.

The reason it's not working is because you're getting the segment of the variable that holds the image's height, when you should be getting the segment of the one after that - because you don't want to overwrite the height. And second, you forgot to take the offset into account. Here's a fixed version:
Code:
DEF SEG = VARSEG(img%(2))
POKE VARPTR(img%(2)) + ((y*wid)+x),c
VARPTR gives you a variable's offset. Notice that it's using the segment (VARSEG) and offset (VARPTR) of the 3rd variable in the array, which is where the pixels start - the first 2 (img%(0) and img%(1)) are the width and height.
Instead of using DEF SEG, can't you just split the array entry into two numbers? PEEK is needed since it deals with bytes, and the array is of integers (two bytes)

So couldn't you logically do this?
colour = img(whateverValue)
HiByte = colour AND 65280
LoByte = colour AND 255
Yup, but writing to the array that way is a bit more complicated...
Code:
img%(whateverValue) = firstPixel% + secondPixel% * 256
...because if secondPixel% is greater than or equal to 128, you get an overflow error.
Quote:So couldn't you logically do this?
colour = img(whateverValue)
HiByte = colour AND 65280
LoByte = colour AND 255

It's actually:
Code:
HiByte = colour \ 256
LoByte = colour AND 255

or is it the other way around? Anyhow, that only works with unsigned integers, so it would only work compiled. QB's interpreter tends to convert one of the bits in the value to a +/- sign, so the limit is 32767 and your byte math becomes messed up.

The reason that turns out is because a short integer is two bytes (0-65535), while POKE only modifies one. If you want to edit a picture with PEEK and POKE exactly like you do on the screen, you need to a single byte per value, which can only be done in qbasic with a 1 character string:

Code:
DIM img%(width * height) as string * 1

But then, of course, you couldnt use GET and PUT with that.