Create a two-dimensional array at runtime: Difference between revisions

Content added Content deleted
(Regrouped BASIC examples, added Commodore BASIC example)
Line 106: Line 106:
set array to {}
set array to {}
</lang>
</lang>

=={{header|Applesoft BASIC}}==
<lang ApplesoftBasic>10 INPUT "ENTER TWO INTEGERS:"; X%, Y%
20 DIM A%(X% - 1, Y% - 1)
30 X% = RND(1) * X%
40 Y% = RND(1) * Y%
50 A%(X%, Y%) = -32767
60 PRINT A%(X%, Y%)
70 CLEAR</lang>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
Line 158: Line 149:


=={{header|BASIC}}==
=={{header|BASIC}}==

==={{header|Applesoft BASIC}}===
<lang ApplesoftBasic>10 INPUT "ENTER TWO INTEGERS:"; X%, Y%
20 DIM A%(X% - 1, Y% - 1)
30 X% = RND(1) * X%
40 Y% = RND(1) * Y%
50 A%(X%, Y%) = -32767
60 PRINT A%(X%, Y%)
70 CLEAR</lang>

==={{header|BBC BASIC}}===
<lang bbcbasic> INPUT "Enter array dimensions separated by a comma: " a%, b%
DIM array(a%, b%)
array(1, 1) = PI
PRINT array(1, 1)</lang>

==={{header|Commodore BASIC}}===
Note: Size of array may be limited by RAM availability in some Commodore machines.
<lang FreeBasic>10 print chr$(147);chr$(14);
15 print "Size of array:"
20 print "Columns (1-20)";:input x%
25 if x%<1 or x%>20 then print "Try again.":goto 20
30 print "Rows (1-20)";:input y%
35 if y%<1 or y%>20 then print "Try again.":goto 30
40 x%=x%-1:y%=y%-1:dim a$(x%,y%)
50 nx=int(rnd(1)*x%):ny=int(rnd(1)*y%)
60 a$(nx,ny)="X"
70 print "Element";nx;",";ny;"= '";a$(nx,ny);"'"
80 clr:rem clear variables from ram
</lang>

{{out}}
<pre>
Size of array:
Columns (1-20)? 10
Rows (1-20)? 10
Element 6 , 3 = 'X'

ready.
print a$(6,3)


ready.
</pre>
==={{header|FreeBASIC}}===
<lang freebasic>' FB 1.05.0 Win64

Dim As Integer i, j
Input "Enter two positive integers, separated by a comma"; i, j
Dim a(1 To i, 1 To j) As Integer
a(i, j) = i * j
Print "a("; Str(i); ","; Str(j); ") ="; a(i, j)
Erase a
Print
Print "Press any key to quit"
Sleep</lang>

{{out}}
<pre>
Enter two positive integers, separated by a comma? 4, 7
a(4,7) = 28
</pre>

==={{header|IS-BASIC}}===
<lang IS-BASIC>100 INPUT PROMPT "Enter array dimensions separated by a coma: ":A,B
110 NUMERIC ARRAY(1 TO A,1 TO B)
120 LET ARRAY(1,1)=PI
130 PRINT ARRAY(1,1)</lang>

==={{header|Liberty BASIC}}===
Arrays can hold numbers ( eg age( 100)( or strings ( eg name$( 100))
LB arrays can only be one or two dimensioned.
If an array is not DIMensioned explicitly, then the array will be limited to 11 elements, 0 to 10.
Non DIMensioned double subscript arrays will be limited to 100 elements 0 to 9 by 0 to 9.
The DIM statement can be followed by a list of arrays to be dimensioned, separated by commas.
REDIM redimensions an already dimensioned array and clears all elements to zero (or to an empty string in the case of string arrays).
This can be very useful for writing applications that have data sets of unknown size.
If you dimension arrays that are extra large to make sure you can hold data, but only have a small set of data, then all the space you reserved is wasted.
This hurts performance, because memory is set aside for the number of elements in the DIM statement.
<lang lb>
input "Enter first array dimension "; a
input "Enter second array dimension "; b

dim array( a, b)

array( 1, 1) = 123.456
print array( 1, 1)

end
</lang>

==={{header|PureBasic}}===

<lang PureBasic>If OpenConsole()
Define x, y

Print("Input X-Size: ")
x = Val(Input())

Print("Input Y-Size: ")
y = Val(Input())

Dim a(x,y) ; Should really check if x & y are larger then 1, but that would be less fun....
a(1,1)=Random(1000)
PrintN("a(1,1)= " + Str(a(1,1)) )
PrintN("Press ENTER to exit"):Input()
End ; Close down and let PureBasic delete the Console and all variables.
EndIf</lang>

==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang qbasic> CLS
<lang qbasic> CLS
Line 166: Line 270:
ERASE array</lang>
ERASE array</lang>


=={{header|BBC BASIC}}==
==={{header|Run BASIC}}===
<lang bbcbasic> INPUT "Enter array dimensions separated by a comma: " a%, b%
<lang RunBasic>print "Enter array 1 greater than 0"; : input a1
print "Enter array 2 greater than 0"; : input a2
DIM array(a%, b%)
dim chrArray$(max(a1,1),max(a2,1))
array(1, 1) = PI
dim numArray(max(a1,1),max(a2,1))
PRINT array(1, 1)</lang>

chrArray$(1,1) = "Hello"
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)</lang>

==={{header|Sinclair ZX81 BASIC}}===
Arrays are indexed from 1; the only limit on their size (which may be an exigent limit) is the available memory. We create an array, write to a randomly selected element and then print it out, and finally use <code>CLEAR</code> to destroy the array (and all the other variables in the program).
<lang basic> 10 PRINT "1ST DIMENSION: ";
20 INPUT D1
30 PRINT D1
40 PRINT "2ND DIMENSION: ";
50 INPUT D2
60 PRINT D2
70 DIM A(D1,D1)
80 PRINT "ARRAY CREATED"
90 LET X=1+INT (D1*RND)
100 LET Y=1+INT (D2*RND)
110 LET A(X,Y)=37
120 PRINT "ITEM ";X;", ";Y;" = ";A(X,Y)
130 CLEAR
140 PRINT "ARRAY DESTROYED"</lang>
{{out}}
<pre>1ST DIMENSION: 11
2ND DIMENSION: 6
ARRAY CREATED
ITEM 7, 4 = 37
ARRAY DESTROYED</pre>

==={{header|Sinclair ZX Spectrum BASIC}}===
<lang zxbasic>10 INPUT "Size? ";rows;"*";columns
20 DIM a(rows,columns): REM defines a numeric array
30 LET a=INT (RND*rows)+1: LET c=INT (RND*columns+1): REM the array is labelled a, but the letter a is still available for variable assignment
40 LET a(a,c)=1
50 PRINT a(a,c)
60 DIM a(1): REM arrays cannot be removed without CLEARing the entire variable space, but redimensioning them to 1 will save most of the space they used</lang>

==={{header|TI-83 BASIC}}===
<lang ti83b>Input "ROWS? ",R
Input "COLS? ",C
{R,C}→dim([A])
42→[A](1,1)
Disp [A](1,1)
DelVar [A]</lang>

==={{header|Visual Basic .NET}}===

<lang vbnet>Module Program
Sub Main()
Console.WriteLine("Enter two space-delimited integers:")
Dim input = Console.ReadLine().Split()
Dim rows = Integer.Parse(input(0))
Dim cols = Integer.Parse(input(1))

' VB uses max-index for array creation.
Dim arr(rows - 1, cols - 1) As Integer

arr(0, 0) = 2
Console.WriteLine(arr(0, 0))
End Sub
End Module</lang>

{{out}}
<pre>Enter two space-delimited integers:
5 42
2</pre>


=={{header|C}}==
=={{header|C}}==
Line 828: Line 997:


END PROGRAM Example</lang>
END PROGRAM Example</lang>

=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64

Dim As Integer i, j
Input "Enter two positive integers, separated by a comma"; i, j
Dim a(1 To i, 1 To j) As Integer
a(i, j) = i * j
Print "a("; Str(i); ","; Str(j); ") ="; a(i, j)
Erase a
Print
Print "Press any key to quit"
Sleep</lang>

{{out}}
<pre>
Enter two positive integers, separated by a comma? 4, 7
a(4,7) = 28
</pre>


=={{header|Frink}}==
=={{header|Frink}}==
Line 1,017: Line 1,167:


delvar, d</lang>
delvar, d</lang>

=={{header|IS-BASIC}}==
<lang IS-BASIC>100 INPUT PROMPT "Enter array dimensions separated by a coma: ":A,B
110 NUMERIC ARRAY(1 TO A,1 TO B)
120 LET ARRAY(1,1)=PI
130 PRINT ARRAY(1,1)</lang>


=={{header|J}}==
=={{header|J}}==
Line 1,185: Line 1,329:
[3, 4, 5, 6, 7]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]</pre>
[4, 5, 6, 7, 8]</pre>

=={{header|Liberty BASIC}}==
Arrays can hold numbers ( eg age( 100)( or strings ( eg name$( 100))
LB arrays can only be one or two dimensioned.
If an array is not DIMensioned explicitly, then the array will be limited to 11 elements, 0 to 10.
Non DIMensioned double subscript arrays will be limited to 100 elements 0 to 9 by 0 to 9.
The DIM statement can be followed by a list of arrays to be dimensioned, separated by commas.
REDIM redimensions an already dimensioned array and clears all elements to zero (or to an empty string in the case of string arrays).
This can be very useful for writing applications that have data sets of unknown size.
If you dimension arrays that are extra large to make sure you can hold data, but only have a small set of data, then all the space you reserved is wasted.
This hurts performance, because memory is set aside for the number of elements in the DIM statement.
<lang lb>
input "Enter first array dimension "; a
input "Enter second array dimension "; b

dim array( a, b)

array( 1, 1) = 123.456
print array( 1, 1)

end
</lang>


=={{header|Logo}}==
=={{header|Logo}}==
Line 1,776: Line 1,897:
33
33
</pre>
</pre>

=={{header|PureBasic}}==

<lang PureBasic>If OpenConsole()
Define x, y

Print("Input X-Size: ")
x = Val(Input())

Print("Input Y-Size: ")
y = Val(Input())

Dim a(x,y) ; Should really check if x & y are larger then 1, but that would be less fun....
a(1,1)=Random(1000)
PrintN("a(1,1)= " + Str(a(1,1)) )
PrintN("Press ENTER to exit"):Input()
End ; Close down and let PureBasic delete the Console and all variables.
EndIf</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 1,935: Line 2,036:
arr[1][3] = 5
arr[1][3] = 5
p arr[1][3]</lang>
p arr[1][3]</lang>
=={{header|Run BASIC}}==
<lang RunBasic>print "Enter array 1 greater than 0"; : input a1
print "Enter array 2 greater than 0"; : input a2
dim chrArray$(max(a1,1),max(a2,1))
dim numArray(max(a1,1),max(a2,1))


chrArray$(1,1) = "Hello"
numArray(1,1) = 987.2
print chrArray$(1,1);" ";numArray(1,1)</lang>


=={{header|Rust}}==
=={{header|Rust}}==
Line 2,094: Line 2,186:
[[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
[[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]
</pre>
</pre>

=={{header|Sinclair ZX81 BASIC}}==
Arrays are indexed from 1; the only limit on their size (which may be an exigent limit) is the available memory. We create an array, write to a randomly selected element and then print it out, and finally use <code>CLEAR</code> to destroy the array (and all the other variables in the program).
<lang basic> 10 PRINT "1ST DIMENSION: ";
20 INPUT D1
30 PRINT D1
40 PRINT "2ND DIMENSION: ";
50 INPUT D2
60 PRINT D2
70 DIM A(D1,D1)
80 PRINT "ARRAY CREATED"
90 LET X=1+INT (D1*RND)
100 LET Y=1+INT (D2*RND)
110 LET A(X,Y)=37
120 PRINT "ITEM ";X;", ";Y;" = ";A(X,Y)
130 CLEAR
140 PRINT "ARRAY DESTROYED"</lang>
{{out}}
<pre>1ST DIMENSION: 11
2ND DIMENSION: 6
ARRAY CREATED
ITEM 7, 4 = 37
ARRAY DESTROYED</pre>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Line 2,319: Line 2,388:
</lang>
</lang>



=={{header|TI-83 BASIC}}==
<lang ti83b>Input "ROWS? ",R
Input "COLS? ",C
{R,C}→dim([A])
42→[A](1,1)
Disp [A](1,1)
DelVar [A]</lang>


=={{header|Toka}}==
=={{header|Toka}}==
Line 2,396: Line 2,459:
End Sub
End Sub
</lang>
</lang>

=={{header|Visual Basic .NET}}==

<lang vbnet>Module Program
Sub Main()
Console.WriteLine("Enter two space-delimited integers:")
Dim input = Console.ReadLine().Split()
Dim rows = Integer.Parse(input(0))
Dim cols = Integer.Parse(input(1))

' VB uses max-index for array creation.
Dim arr(rows - 1, cols - 1) As Integer

arr(0, 0) = 2
Console.WriteLine(arr(0, 0))
End Sub
End Module</lang>

{{out}}
<pre>Enter two space-delimited integers:
5 42
2</pre>


=={{header|Vim Script}}==
=={{header|Vim Script}}==
Line 2,534: Line 2,575:
m[0,1].> 0
m[0,1].> 0
</pre>
</pre>
=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 INPUT "Size? ";rows;"*";columns
20 DIM a(rows,columns): REM defines a numeric array
30 LET a=INT (RND*rows)+1: LET c=INT (RND*columns+1): REM the array is labelled a, but the letter a is still available for variable assignment
40 LET a(a,c)=1
50 PRINT a(a,c)
60 DIM a(1): REM arrays cannot be removed without CLEARing the entire variable space, but redimensioning them to 1 will save most of the space they used</lang>