Multi-dimensional array: Difference between revisions

Added FreeBASIC
No edit summary
(Added FreeBASIC)
Line 1,198:
 
For large arrays when memory is not truly random-access (on-chip memory versus main memory, main memory versus slower disc storage) a calculation that works along a row will be accessing widely-separated elements in storage, perhaps with only one "hit" per virtual memory page before leaping to another, while an equivalent calculation working down a column will enjoy multiple hits per page. The difference can be large enough to promote usages such as swapping the order of the DO-loops for portions of a calculation, or swapping the order of the indices as by writing <code>A(J,I)</code> when it would be more natural to use <code>A(I,J)</code>.
 
=={{header|FreeBASIC}}==
Multidimensional arrays can be declared as well, and are stored in this
definite order: values differing only in the last index are contiguous
(row-major order).
The maximum number of dimensions of a multidimensional array is 8.
 
<syntaxhighlight lang="vbnet">
'' declare a three-dimensional array of single
'' precision floating-point numbers.
Dim array(1 To 2, 6, 3 To 5) As Single
 
'' The first dimension of the declared array
'' has indices from 1 to 2, the second, 0 to 6,
'' and the third, 3 to 5.
</syntaxhighlight>
 
The total number of elements and the total size (in bytes) of a multi-dimensional
array can be directly obtained using Arraylen and Arraysize, respectively.
 
<syntaxhighlight lang="vbnet">
' Take Care while initializing multi-dimensional array
Dim As Integer multidim(1 To 2, 1 To 5) = {{0,0,0,0,0}, {0,0,0,0,0}}
</syntaxhighlight>
 
<syntaxhighlight lang="vbnet">Dim As Integer A(5,4,3,2)
A(3,1,0,1) = 3100
A(3,1,0,1) = A(3,1,0,1)+1
Print A(3,1,0,1)</syntaxhighlight>
{{out}}
<pre>3101</pre>
 
=={{header|Go}}==
2,122

edits