# Arrays? • ### Question

• I've been through the entire Small Basic tutorials and googled around but I still don't understand arrays :/. Could someone please explain how to use arrays in small basic and some examples of practical uses for arrays?

THANKS!

Wednesday, May 25, 2011 9:25 PM

• An array is an indexed list of variables.

In index is something that identifies one entry in the list, it is often just a number.

For example if we have 3 peoples names they could be:

name1 = "John"
name2 = "Mary"
name3 = "Fred"

Or we could put them in an array, the syntax is that the index is inside square brackets:

name = "John"
name = "Mary"
name = "Fred"

The indexes are 1, 2 and 3 and the corresponding values are "John", "Mary" and "Fred".

The advantage of this is that we can the do something on all the elements of the array inside a For loop:

name = "John"
name = "Mary"
name = "Fred"

For i = 1 To Array.GetItemCount(name)
TextWindow.WriteLine(name[i])
EndFor

For 3 names this is not a big deal, but we may have 50 sprites in a game and we can move them all inside a loop or perhaps we are plotting a graph of a function and need to store lots of values of X or Y.

For i = 1 To 100
x[i] = i/100*GraphicsWindow.Width
y[i] = 100+50*Math.Sin(x[i]/20)
If (i > 1) Then
GraphicsWindow.DrawLine(x[i-1],y[i-1],x[i],y[i])
EndIf
EndFor

Here is a sample program from a while back looking at arrays, import FCL390.  Feel free to ask on anything not clear.

• Marked as answer by Friday, June 10, 2011 7:40 PM
Wednesday, May 25, 2011 9:46 PM

### All replies

• An array is an indexed list of variables.

In index is something that identifies one entry in the list, it is often just a number.

For example if we have 3 peoples names they could be:

name1 = "John"
name2 = "Mary"
name3 = "Fred"

Or we could put them in an array, the syntax is that the index is inside square brackets:

name = "John"
name = "Mary"
name = "Fred"

The indexes are 1, 2 and 3 and the corresponding values are "John", "Mary" and "Fred".

The advantage of this is that we can the do something on all the elements of the array inside a For loop:

name = "John"
name = "Mary"
name = "Fred"

For i = 1 To Array.GetItemCount(name)
TextWindow.WriteLine(name[i])
EndFor

For 3 names this is not a big deal, but we may have 50 sprites in a game and we can move them all inside a loop or perhaps we are plotting a graph of a function and need to store lots of values of X or Y.

For i = 1 To 100
x[i] = i/100*GraphicsWindow.Width
y[i] = 100+50*Math.Sin(x[i]/20)
If (i > 1) Then
GraphicsWindow.DrawLine(x[i-1],y[i-1],x[i],y[i])
EndIf
EndFor

Here is a sample program from a while back looking at arrays, import FCL390.  Feel free to ask on anything not clear.

• Marked as answer by Friday, June 10, 2011 7:40 PM
Wednesday, May 25, 2011 9:46 PM
• Im sort of forumulating a project in my head which made me think of a few questions. Instead of asking a question ill just give you an example because i can't really explain it

So i have 50 arrays lets say each one is:

Number

Number

Etc... Ya ya ya you get the point (PS: any way to do that quicker than writing out each one?)

So can I have different terms to eliminate numbers... For ex.

If the number is an even number (divide by 2 and check remainder) eliminate it

So now i would have a list of only odd numbers so my arrays would be

Number

Number

Etc..

So I guess my question is can i set terms and if that part of the array does not fit the terms eliminate it

Pardon my misspellings - I am twelve :D

Wednesday, May 25, 2011 9:57 PM
• Yes, you can - from your question I am not sure that you follow what an index and value are - any array is like a table with 2 columns of indexes and values, for example:

array = 30

array = 40

array = 50

This is like a table with 2 columns, index and value

index   value

1         30

2         40

3         50

We can then get the value of index 2 as 40: array - this is equal to 40, so TextWindow.WriteLine(array) will print 40

We have an array of 50 random values, they are indexed from 1 to 50.  If the values are not a function of the index (i) in some way or random as below then it may not be possible to create the array in a loop.  For example we cannot use a loop to initialise an array of names - we can initialise an array by reading from a file though if the values are there.

For i = 1 To 50
Number[i] = Math.GetRandomNumber(100)
EndFor

So having created the array, we can print the even, odd or any other test on the number inside a loop:

For i = 1 To 50
Number[i] = Math.GetRandomNumber(100)
EndFor

sumEven = 0
sumOdd = 0
For i = 1 To 50
If (Math.Remainder(Number[i],2) = 0) Then
TextWindow.WriteLine(Number[i]+" is even")
sumEven = sumEven+Number[i]
Else
TextWindow.WriteLine(Number[i]+" is odd")
sumOdd = sumOdd+Number[i]
EndIf
EndFor
TextWindow.WriteLine("The sum of even numbers is "+sumEven)
TextWindow.WriteLine("The sum of odd numbers is "+sumOdd)

To delete elements (index and value) from an array can be done by setting the array index = "", such as Number[i] = ""

There are complications about deleting elements from an array in that the indexes are not changed for the non-deleted elements so looping over the indexes of the remaining array is more complex (it can be done, but is more complicated).  While starting out playing with arrays I would leave deleting elements till you are sure about creating and using arrays.

Good luck and post with any questions.

Wednesday, May 25, 2011 10:24 PM
• I wrote out this thing but still diddnt get the result i wanted for deleted the array. What i want is in the very end of this program is that there is no longer an array number 1,3,5,7,9 etc (odd numbers)

But heres the program

```For i = 1 To 50
Number[i] = i
EndFor

For a = 1 To 50
If Math.Remainder(Number[a],2) = 0 Then
TextWindow.WriteLine(Number[a])
Else
Number[a] = ""
EndIf

EndFor

Program.Delay(1000)
For j = 1 To 50
TextWindow.WriteLine(Number[j])
EndFor
```

Wednesday, May 25, 2011 10:38 PM
• As suggested this is not so straight forward - here is how it can be done though.  It means creating a new array with just he remaining values indexed sequentially, then here I copied the new array to the original and deleted the new temporary array (moved it).

For i = 1 To 50
Number[i] = i
EndFor

For a = 1 To 50
If Math.Remainder(Number[a],2) = 0 Then
TextWindow.WriteLine(Number[a])
Else
Number[a] = ""
EndIf
EndFor

'Remove unset idexes - create in a new array
index = Array.GetAllIndices(Number) 'array containing remaining valid indexes in Number
For i = 1 To Array.GetItemCount(index) 'Loop over these remaining indexes creating a new sequentially indexed array
NumberNew[i] = Number[index[i]]
EndFor
'Copy new array to Number
Number = NumberNew
'Delete temporay array
NumberNew = ""

Program.Delay(1000)
For j = 1 To Array.GetItemCount(Number)
TextWindow.WriteLine(Number[j])
EndFor

Or we could just print the remaining valid indexes in Number without updating it:

For i = 1 To 50
Number[i] = i
EndFor

For a = 1 To 50
If Math.Remainder(Number[a],2) = 0 Then
TextWindow.WriteLine(Number[a])
Else
Number[a] = ""
EndIf
EndFor

Program.Delay(1000)
index = Array.GetAllIndices(Number) 'Array of remaining set indices in Number
For j = 1 To Array.GetItemCount(index)
TextWindow.WriteLine(Number[index[j]])
EndFor

Wednesday, May 25, 2011 10:52 PM
• FCL390 brilliant code on arrays, will use this to demo to class.
Sunday, May 29, 2011 9:36 PM
• If you are not trying to learn how to take out data from an array, and only worried about getting the output you want, see if this works for you:

For

i = 1 To 50
Number[i] = i
EndFor

For

a = 1 To 50
If Math.Remainder(Number[a],2) = 0 Then
TextWindow.WriteLine(Number[a])
Else
Number[a] = ""
EndIf
EndFor

Program

.Delay(1000)
For j = 1 To 50
If Number[j] <> "" Then
TextWindow.Write(Number[j])
TextWindow.Write(", ")
EndIf
EndFor

TextWindow

.WriteLine("") 'This is so the "Press any key..." outputs on it's own line

If you would prefer to take out the data, then... well, do what litdev says.  Well, now I notice that he even has a different way to do what I coded above - "just print the remaining valid indexes in Number without updating it".

Wednesday, June 1, 2011 6:35 PM