GLBasic forum

Main forum => GLBasic - en => Topic started by: phil on 2009-Nov-05

Title: Lame noob struggling with storing data!!!
Post by: phil on 2009-Nov-05
Hi, I expect im being a dunce having to ask this but i just dont understand...

Ive spent 2 days (dont laugh) trying to find the best way to store a large amount of random numbers (say 100 random numbers or lets go wild 1000 random numbers to be used as x co-ordinates 1000 for y co-ordinates).
All the bits of code ive looked at and tried to pull apart seem to be using arrays but i cant for the life of me grasp arrays, is there a simpler way to do this? do i need arrays? can anyone explain how i do this and give a simple explination of whats happening and why with an example etc. please?

I realise this is a bit lame!  =D Many thanks in advance!
Title: Re: Lame noob struggling with storing data!!!
Post by: kaotiklabs on 2009-Nov-05
Well, I can´t sleep so here you got it...
You will need arrays, try to learn how they work, is easy and suprisingly powerful because they work superb with loops.
Without arrays you would need thousands of variables and has no sense.

I didn´t totally undestand what you were trying to do but I have asumed that you want to draw 1 thousand points on screen or something similar. I´ll post you my example wich creates a type, declares the array, fill it with random values and draws it on the screen.

It could be coded without using types, using two arrays, one for X coordinates and the other for Y (like DIM ArrayX[numelements];DIM ArrayY[numelements]) but it´s harder to understand.

Think about element type like a point type, so it needs two values: X, Y.
So, you only need to declare an array[1000] of elements type.

Don´t care to ask anything about the example.
Hope it helps!  =D

Code (glbasic) Select
SETSCREEN 1024, 768, FALSE

// set a constant element size
CONSTANT numelements = 1000

//declare a element type, with an integer X, Y
TYPE tELEMENT
PosX%
PosY%
ENDTYPE

//declare Element array eich eill contain tELEMENT type
GLOBAL Element[] AS tELEMENT
//declare de size of the array
DIM Element[numelements]

//fill all the array with random numbers
FOR i = 0 TO numelements - 1
Element[i].PosX=RND(1024)
Element[i].PosY=RND(768)
NEXT

WHILE TRUE
//draw the points on screen
FOR i = 0 TO numelements - 1
SETPIXEL Element[i].PosX, Element[i].PosY, RGB(255,255,255)
NEXT
SHOWSCREEN
WEND
Title: Re: Lame noob struggling with storing data!!!
Post by: okee on 2009-Nov-05
Yeah arrays are probably what you want to use.
A variable can hold 1 piece of information whereas
an array can hold more than one piece of information.

so if you want to store the letters of the alphabet you'd
define the array like so
Dim LettersOfTheAlphabet[26]

// an Arrays address starts at 0 so to add the letters
LettersOfTheAlphabet[0] = a
LettersOfTheAlphabet[1] = b
LettersOfTheAlphabet[2] = c
.......
all the way up to
LettersOfTheAlphabet[25] = z

If you use a 2 dimensional array it's like a grid or
if you imagine an excel spread sheet
Dim Map[1000][1000]
Map[0][0] = 1 //the top left square
Map[0][1] = 5 // second square in from the top
Map[0][2] = 7 //3rd square in

Map[1][0] is the 2nd square down on the left

map[999][999] is the last square bottom right

Have a look in the help file under Tutorials\ General

If it's a map and you need to store more than one piece of information at each address then you can use an array of Types but i'd suggest becoming familiar with arrays first.

[edit] kaotiklabs got there before me
Title: Re: Lame noob struggling with storing data!!!
Post by: FutureCow on 2009-Nov-05
My explaination for you
Values in programs are stored in variables (think of it like a container). If you want to store the value 10 for example, you write it on a piece of paper and put it in the container.
If you want to know what value the variable holds, you look in the container.

But the container can only hold one item and has a specific name (eg. the RED container, or the BLUE container).
Now, if you want to store 10 (for the sake of the argument, could be 1000000) values, you would have to create lots of containers (GREEN, PURPLE, and you would quickly run out of colours - besides which, you'd have to write every single colour out individually and it would be painful if you had 1000000 of them).

Along come arrays. They are like sticking 10 blue containers together.  You can then say, what's in the 8th blue container (ie. Blue[8]).

To populate all of your containers you write a loop which will in turn deal with blue container 1, then 2 all the way to 10.
You do it like this

Code (glbasic) Select
DIM Blue[10]        // I want 10 Blue containers - they will be numbered 0-9 rather than 1-10

for loop=0 to 9             // I want a counter to count from 0 to 9 - ie every container number
   Blue[loop]=RND(100)     // Blue[loop]=   means, for the Blue container numbered "loop" let it contain the value
                                       // RND(100) - i.e I want it to hold a random value from 0-99
next                            // Increment our loop value so it now references the next container


The first time that code runs, loop will be 0, so the RND line will be
Blue[0]=RND(100)        (Put a random value in the first container - numbered 0)
The next time it loops around, loop is incremented so the RND line will be
Blue[1]=RND(100)        (Put a random value in the second container - which is numbered 1)


Arrays can be thought of as a row of containers, or letterboxes, or pidgeon holes, or anything similar that makes sense to you. From there you go to a 2 dimensional array (like a crossword puzzle where instead of one row of boxes, you've got a grid). You then reference the particular pidgeon hole (letterbox/grid location) with a row and column number.  So Blue[Row number][Column Number]

Make more sense now?
Title: Re: Lame noob struggling with storing data!!!
Post by: Moru on 2009-Nov-05
Wow, three full examples/tutorials on arrays instead of the usual "RTFM" of other forums, no names mentioned. :-) Good job :-)
Title: Re: Lame noob struggling with storing data!!!
Post by: phil on 2009-Nov-05
 :good: WOW! Big thanks to kaotiklabs, okee & FutureCow for for your time spent explaining and by the looks of it some great examples and advice! Ive just logged back on after a day from hell to find all this great stuff and I`ll be having a go at it again tomorrow. Like Ocean and Moru said, this is a pleasent place to ask noob questions, just as it should be in a forum on basic! Im glad i bought GLB, top notch purchase!  :booze:

[edit] Thanks again People... Ive made a nice 3 layer parallax scrolling starfield using the help i got here, its just 23 lines of code, very simply made and simple to follow and ive fully commented each line for noobs like me... it looks great so ive added it to the code snippets section "Scrolling starfeld with parallax" topic...  =D
I wanted to give something back!!!