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

m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|S-BASIC}}: added comment)
 
(20 intermediate revisions by 11 users not shown)
Line 97:
DBRA D6,loop_destroyArray ;decrement this, D7 is $FFFF each time execution gets here so this acts as a "carry" of sorts.
;if this value was 0 prior to the loop, the loop ends immediately.</syntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program createarray264.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ BUFFERSIZE, 64
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRead1: .asciz "Input size level 1 : "
szMessRead2: .asciz "Input size level 2 : "
szMessIndice1: .asciz "Indice 1 ="
szMessIndice2: .asciz " Indice 2 ="
szMessResult: .asciz " Item = "
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip BUFFERSIZE // conversion buffer
sZoneConv1: .skip BUFFERSIZE // conversion buffer
sZoneConv2: .skip BUFFERSIZE // conversion buffer
sBuffer: .skip BUFFERSIZE
 
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
ldr x0,qAdrszMessRead1
bl affichageMess
mov x0,#STDIN // Linux input console
ldr x1,qAdrsBuffer // buffer address
mov x2,#BUFFERSIZE // buffer size
mov x8,READ
svc 0 // call system
ldr x0,qAdrsBuffer // buffer address
bl conversionAtoD
mov x9,x0
ldr x0,qAdrszMessRead2
bl affichageMess
mov x0,#STDIN // Linux input console
ldr x1,qAdrsBuffer // buffer address
mov x2,#BUFFERSIZE // buffer size
mov x8,READ
svc 0 // call system
ldr x0,qAdrsBuffer // buffer address
bl conversionAtoD
mov x10,x0
// create array
lsl x12,x10,#3 // compute size level 2
mul x8,x12,x9 // compute size array
tst x8,0xF // multiple of 16 ?
add x11,x8,8 // if no add 8 octets
csel x8,x8,x11,eq // the stack must always be aligned on 16 bytes
// in 64 assembly arm
sub sp,sp,x8 // reserve place on stack
mov fp,sp // save array address
mov x0,#0 // init all items array
1: // begin loop1
mov x1,#0
2: // begin loop2
mul x2,x0,x12
add x2,x2,x1, lsl #3
str x2,[fp,x2] // store shift in array item
add x1,x1,#1
cmp x1,x10
blt 2b
add x0,x0,#1
cmp x0,x9
blt 1b
mov x0,fp
mov x1,#1 // second indice level 1
mov x2,#0 // first indice level 2
mov x3,x12 // level 2 size
bl displayItem
mov x0,fp
sub x1,x9,#1 // last level 1
sub x2,x10,#1 // last level 2
mov x3,x12 // level 2 size
bl displayItem
 
add sp,sp,x8 // release space on stack
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrsZoneConv1: .quad sZoneConv1
qAdrsZoneConv2: .quad sZoneConv2
qAdrszMessRead1: .quad szMessRead1
qAdrszMessRead2: .quad szMessRead2
qAdrsBuffer: .quad sBuffer
qAdrszMessResult: .quad szMessResult
qAdrszMessStart: .quad szMessStart
qAdrszMessIndice1: .quad szMessIndice1
qAdrszMessIndice2: .quad szMessIndice2
/***************************************************/
/* display array item */
/***************************************************/
/* x0 array address */
/* x1 indice 1 */
/* x2 indice 2 */
/* x3 level 2 size */
displayItem:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,fp,[sp,-16]! // save registers
mov x5,x0
mov x6,x1
mov x0,x6
ldr x1,qAdrsZoneConv
bl conversion10 // conversion indice 1
mov x0,x2
ldr x1,qAdrsZoneConv1
bl conversion10 // conversion indice 2
mul x4,x6,x3 // multiply indice level 1 by level 2 size
add x4,x4,x2, lsl #3 // add indice level 2 * 8 (8 bytes)
ldr x0,[x5,x4] // load array item
ldr x1,qAdrsZoneConv2
bl conversion10
mov x0,#7 // string number to display
ldr x1,qAdrszMessIndice1
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszMessIndice2
ldr x4,qAdrsZoneConv1 // insert conversion in message
ldr x5,qAdrszMessResult
ldr x6,qAdrsZoneConv2 // insert conversion in message
ldr x7,qAdrszCarriageReturn
bl displayStrings // display message
100:
ldp x6,fp,[sp],16 // restaur registers
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* display multi strings */
/* new version 24/05/2023 */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* x4 address string4 */
/* x5 address string5 */
/* x6 address string5 */
/* x7 address string6 */
displayStrings: // INFO: displayStrings
stp x8,lr,[sp,-16]! // save registers
stp x2,fp,[sp,-16]! // save registers
add fp,sp,#32 // save paraméters address (4 registers saved * 8 bytes)
mov x8,x0 // save strings number
cmp x8,#0 // 0 string -> end
ble 100f
mov x0,x1 // string 1
bl affichageMess
cmp x8,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x8,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x8,#3
ble 100f
mov x0,x4
bl affichageMess
cmp x8,#4
ble 100f
mov x0,x5
bl affichageMess
cmp x8,#5
ble 100f
mov x0,x6
bl affichageMess
cmp x8,#6
ble 100f
mov x0,x7
bl affichageMess
100:
ldp x2,fp,[sp],16 // restaur registers
ldp x8,lr,[sp],16 // restaur registers
ret
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Input size level 1 : 10
Input size level 2 : 10
Indice 1 =1 Indice 2 =0 Item = 80
Indice 1 =9 Indice 2 =9 Item = 792
</pre>
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
Line 277 ⟶ 495:
print (array[1,1])
)</syntaxhighlight>
 
=={{header|ALGOL-M}}==
<syntaxhighlight lang="ALGOL">
begin
 
integer first, second;
 
write("Two Dimensional Array Exercise");
write("Length of first dimension:");
read(first);
write("Length of second dimension:");
read(second);
 
begin % we need to start a new block %
integer array test[1:first, 1:second];
test[1,1] := 99;
write("Stored value at 1,1 =",test[1,1]);
end; % array is now out of scope %
 
end
</syntaxhighlight>
{{out}}
<pre>
Two Dimensional Array Exercise
Length of first dimension:
->6
Length of second dimension:
->7
Stored value at 1,1 = 99
</pre>
 
 
=={{header|ALGOL W}}==
Line 374 ⟶ 623:
 
To correct the last comment in the script above: when a script's run, the values of its properties, globals, and run-handler (''ie.'' top level) variables are saved back to the script file when the execution finishes. So variables containing bulky values like lists ideally ''should'' be set to something smaller before the end in order to prevent file bloat. Or local variables could be used, which would be the better option above as the <code>return</code> statement prevents the last line from being executed anyway.
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program createarray2.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
.equ STDIN, 0 @ Linux input console
.equ READ, 3 @ Linux syscall
.equ BUFFERSIZE, 64
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessRead1: .asciz "Input size level 1 : "
szMessRead2: .asciz "Input size level 2 : "
szMessIndice1: .asciz "Indice 1 ="
szMessIndice2: .asciz "Indice 2 ="
szMessResult: .asciz "Item = "
szMessStart: .asciz "Program 32 bits start.\n"
szCarriageReturn: .asciz "\n"
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip BUFFERSIZE // conversion buffer
sZoneConv1: .skip BUFFERSIZE // conversion buffer
sZoneConv2: .skip BUFFERSIZE // conversion buffer
sBuffer: .skip BUFFERSIZE
 
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
ldr r0,iAdrszMessRead1
bl affichageMess
mov r0,#STDIN @ Linux input console
ldr r1,iAdrsBuffer @ buffer address
mov r2,#BUFFERSIZE @ buffer size
mov r7,#READ @ request to read datas
svc 0 @ call system
ldr r0,iAdrsBuffer @ buffer address
bl conversionAtoD
mov r9,r0
ldr r0,iAdrszMessRead2
bl affichageMess
mov r0,#STDIN @ Linux input console
ldr r1,iAdrsBuffer @ buffer address
mov r2,#BUFFERSIZE @ buffer size
mov r7,#READ @ request to read datas
svc 0 @ call system
ldr r0,iAdrsBuffer @ buffer address
bl conversionAtoD
mov r10,r0
@ create array
lsl r12,r10,#2 @ compute size level 2
mul r8,r12,r9 @ compute size array
sub sp,sp,r8 @ reserve place on stack
mov fp,sp
mov r0,#0 @ init all items array
1: @ begin loop1
mov r1,#0
2: @ begin loop2
mul r2,r0,r12
add r2,r2,r1, lsl #2
str r2,[fp,r2] @ store shift in array item
add r1,r1,#1
cmp r1,r10
blt 2b
add r0,r0,#1
cmp r0,r9
blt 1b
mov r0,fp
mov r1,#1 @ second indice level 1
mov r2,#0 @ first indice level 2
mov r3,r12 @ level 2 size
bl displayItem
mov r0,fp
sub r1,r9,#1 @ last level 1
sub r2,r10,#1 @ last level 2
mov r3,r12 @ level 2 size
bl displayItem
 
add sp,sp,r8 @ release space on stack
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsZoneConv: .int sZoneConv
iAdrsZoneConv1: .int sZoneConv1
iAdrsZoneConv2: .int sZoneConv2
iAdrszMessRead1: .int szMessRead1
iAdrszMessRead2: .int szMessRead2
iAdrsBuffer: .int sBuffer
iAdrszMessResult: .int szMessResult
iAdrszMessStart: .int szMessStart
iAdrszMessIndice1: .int szMessIndice1
iAdrszMessIndice2: .int szMessIndice2
/***************************************************/
/* display array item */
/***************************************************/
/* r0 array address */
/* r1 indice 1 */
/* r2 indice 2 */
/* r3 level 2 size */
displayItem:
push {r1-r6,lr} @ save des registres
mov r5,r0
mov r6,r1
mov r0,r6
ldr r1,iAdrsZoneConv
bl conversion10 @ conversion indice 1
mov r0,r2
ldr r1,iAdrsZoneConv1
bl conversion10 @ conversion indice 2
mul r4,r6,r3 @ multiply indice level 1 by level 2 size
add r4,r4,r2, lsl #2 @ add indice level 2 * 4 (4 bytes)
ldr r0,[r5,r4] @ load array item
ldr r1,iAdrsZoneConv2
bl conversion10
mov r0,#7 @ string number to display
ldr r1,iAdrszMessIndice1
ldr r2,iAdrsZoneConv @ insert conversion in message
ldr r3,iAdrszMessIndice2
ldr r4,iAdrsZoneConv1 @ insert conversion in message
push {r4}
ldr r4,iAdrszMessResult
push {r4}
ldr r4,iAdrsZoneConv2 @ insert conversion in message
push {r4}
ldr r4,iAdrszCarriageReturn
push {r4}
bl displayStrings @ display message
add sp,sp,#16
100:
pop {r1-r6,pc}
/***************************************************/
/* display multi strings */
/***************************************************/
/* r0 contains number strings address */
/* r1 address string1 */
/* r2 address string2 */
/* r3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: @ INFO: displayStrings
push {r1-r4,fp,lr} @ save des registres
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes)
mov r4,r0 @ save strings number
cmp r4,#0 @ 0 string -> end
ble 100f
mov r0,r1 @ string 1
bl affichageMess
cmp r4,#1 @ number > 1
ble 100f
mov r0,r2
bl affichageMess
cmp r4,#2
ble 100f
mov r0,r3
bl affichageMess
cmp r4,#3
ble 100f
mov r3,#3
sub r2,r4,#4
1: @ loop extract address string on stack
ldr r0,[fp,r2,lsl #2]
bl affichageMess
subs r2,#1
bge 1b
100:
pop {r1-r4,fp,pc}
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
Input size level 1 : 10
Input size level 2 : 5
Indice 1 =1 Indice 2 =0 Item = 20
Indice 1 =9 Indice 2 =4 Item = 196
</pre>
 
=={{header|Arturo}}==
Line 437 ⟶ 891:
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic">10 INPUT "ENTER TWO INTEGERS:"; X%, Y%
Line 455 ⟶ 908:
print "a("; string(i); ","; string(j); ") = "; a[i, j]
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">10 input "Enter two positive integers, separated by a comma? ";i,j
20 dim array(i,j)
30 array(i,j) = i*j
40 print "a(";str$(i);",";str$(j);") = ";array(i,j)
50 erase array</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">10 INPUT "Enter two positive integers, separated by a comma"; I, J
20 DIM ARRAY(I, J)
30 ARRAY(I, J) = I * J
40 PRINT "a("; STR$(I); ","; STR$(J); " ) ="; ARRAY(I, J)
50 ERASE ARRAY</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
<syntaxhighlight lang="qbasic">10 PRINT "ENTER ONE POSITIVE INTEGER: "
20 INPUT I
30 PRINT "ENTER OTHER POSITIVE INTEGER: "
40 INPUT J
50 LET A(I,J) = I*J
60 PRINT "A(";I;",";J;") = ";A(I,J)
70 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|GW-BASIC}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
Line 473 ⟶ 959:
MAT REDIM array(0,0)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Create a two-dimensional array at runtime"
VERSION "0.0001"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
i$ = INLINE$("Enter one positive integer: ")
j$ = INLINE$("Enter other positive integer: ")
i = SSHORT(i$)
j = SSHORT(j$)
DIM a[i, j]
a[i, j] = i * j
PRINT "a("; STRING(i); ", "; STRING(j); ") ="; a[i, j]
END FUNCTION</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 1,204 ⟶ 1,707:
Column 08 = 10 rows
Column 09 = 02 rows</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
write "Number of rows: "
nrows = number input
print nrows
write "Number of columns: "
ncols = number input
print ncols
#
len a[][] nrows
for i to nrows
len a[i][] ncols
.
a[1][1] = 11
print a[1][1]
len a[][] 0
</syntaxhighlight>
 
=={{header|Elena}}==
ELENA 56.0x :
 
Typified array
Line 1,216 ⟶ 1,737:
var m := new Integer();
console.write:("Enter two space delimited integers:");
console.loadLine(n,m);
Line 1,234 ⟶ 1,755:
auto m := new Integer();
console.write:("Enter two space delimited integers:");
console.loadLine(n,m);
auto myArray2 := new object[][](n.Value).populate::(int i => (new object[](m.Value)) );
myArray2[0][0] := 2;
myArray2[1][0] := "Hello";
Line 1,421 ⟶ 1,942:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Create_a_two-dimensional_array_at_runtime}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Create a two-dimensional array at runtime 01.png]]
In '''[https://formulae.org/?example=Create_a_two-dimensional_array_at_runtime this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Create a two-dimensional array at runtime 02.png]]
 
There is no need to "release" the array, once the result is delivered, all intermediate elements are destroyed.
 
=={{header|Frink}}==
Line 1,434 ⟶ 1,959:
println[a@(rows-1)@(cols-1)]
</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
CFStringRef iStr, jStr
long i, j
 
iStr = input @"Enter one positive integer: "
jStr = input @"Enter other positive integer: "
i = fn StringIntegerValue(iStr)
j = fn StringIntegerValue(jStr)
mda (0, 0) = {i, j}
mda (i, j) = i * j
printf @"mda(%ld, %ld) = %ld", i, j, mda_integer (i, j)
 
HandleEvents
</syntaxhighlight>
{{output}}
With inputs of 5 and 7:
<pre>
mda(5, 7) = 35
</pre>
 
=={{header|GAP}}==
Line 1,911 ⟶ 2,458:
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">
class TwoDimArray {
use IO;
function : Main(args : String[]) ~ Nil {
 
rows := Standard->ReadLine()->ToInt();
bundle Default {
cols := Standard->ReadLine()->ToInt();
class TwoDee {
function : Main(args : System.String[]) ~ Nil {
if(rows > DoIt(0 & cols > 0); {
array := Float->New[rows, cols];
}
array[0,0] := 42.0;
Standard->Print("The number at place [0,] is: ")->PrintLine(array[0,0]);
function : native : DoIt() ~ Nil {
Console->GetInstance()->Print("Enter x: ");
x := Console->GetInstance()->ReadString()->ToInt();
Console->GetInstance()->Print("Enter y: ");
y := Console->GetInstance()->ReadString()->ToInt();
if(x > 0 & y > 0) {
array : Int[,] := Int->New[x, y];
array[0, 0] := 2;
array[0, 0]->PrintLine();
};
}
}
Line 2,371 ⟶ 2,907:
33
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">:- dynamic array/2.
 
run :-
write('Enter two positive integers, separated by a comma: '),
read((I,J)),
assert(array(I,J)),
Value is I * J,
format('a(~w,~w) = ~w', [I, J, Value]),
retractall(array(_,_)).</syntaxhighlight>
 
=={{header|Python}}==
Line 2,612 ⟶ 3,160:
aList[1][2] = 10 See aList[1][2] + nl
</syntaxhighlight>
 
 
« "# rows?" "3" INPUT "# columns?" "5" INPUT <span style="color:grey">''@ ask for size, with 3 and 5 as default values''</span>
2 →LIST STR→ 0 CON <span style="color:grey">''@ create array in stack''</span>
{ 1 2 } TIME PUT <span style="color:grey">''@ put current time at array[1,2] ''</span>
{ 1 2 } GET <span style="color:grey">''@ read array[1,2] and remove array from the stack ''</span>
» '<span style="color:blue">TASK' STO</span>
 
=={{header|Ruby}}==
Line 2,636 ⟶ 3,191:
println!("{}", v[0][0]);
}</syntaxhighlight>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="BASIC">
var a, b = integer
 
print "Two-Dimensional Array Example"
input "Size of first dimension"; a
input "Size of second dimension"; b
 
dim integer test_array(a, b)
 
test_array(1,1) = 99 rem S-BASIC arrays are indexed from 1
 
print "Value stored at 1,1 ="; test_array(1,1)
 
end
</syntaxhighlight>
{{out}}
<pre>
Two-Dimensional Array Example
Size of first dimension? 7
Size of second dimension? 7
Value stored at 1,1 = 99
</pre>
 
=={{header|Scala}}==
Line 3,132 ⟶ 3,711:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
var x
13

edits