Arrays: Difference between revisions

Content added Content deleted
imported>Brie
(Add Nu)
imported>J7M
(add example for SmallBASIC)
Line 8,450: Line 8,450:
slate[7]> x at: 0.
slate[7]> x at: 0.
1</syntaxhighlight>
1</syntaxhighlight>

=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
' One dimensional arrays
DIM A ' empty array
DIM B(3) ' empty array with 4 elements
DIM C(2 TO 4) ' empty array with elements 2,3 and 4
D = [1,2,3,4] ' assign array in one statement
E = ["one", "two", "three"] ' string array
F = [1, "two", [1,2,3]] ' arrays can contain mixed data types

B[0] = 1 ' use [] or () to assign value to
B(1) = 2 ' element or access elements

A << 2 ' append element to an array

print F ' print whole array -> Output: [1,two,[1,2,3]]
print F[0] ' print first element -> Output: 1
print F(1) ' print second element -> Output: two

' Multi dimensional arrays
DIM A(2,0) ' column array (vector) with 3 elements
DIM B(2,2) ' empty 2D array (matrix) with 3x3 elements
DIM C(2,2,2) ' empty 3D array with 3x3x3 elements

A[0,0] = 1
A[1,0] = 2
A[2,0] = 3

' Math with arrays

A = [1,2,3]
B = [4,5,6]

print A + B ' Output: [5,7,9]
print 3 * A ' Output: [3,6,9]
print A * B ' Output: [4,10,18]

C = [1;2;3] ' vector
D = [1,2,3;4,5,6;7,8,9] ' 2D matrix

print D * C ' matrix * vector -> Output [14;32;50]
print D * D ' matrix * matrix -> Output [30,36,42;66,81,96;102,126,150]
</syntaxhighlight>



=={{header|Smalltalk}}==
=={{header|Smalltalk}}==