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

m
(add task to arm assembly raspberry pi)
m (→‎{{header|S-BASIC}}: added comment)
 
(19 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 393 ⟶ 642:
.equ READ, 3 @ Linux syscall
.equ BUFFERSIZE, 64
 
//.include "../../ficmacros32.inc"
 
/*********************************/
Line 475 ⟶ 722:
bl displayItem
 
add sp,sp,r8 @ release space on stack
100: @ standard end of the program
Line 644 ⟶ 891:
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic">10 INPUT "ENTER TWO INTEGERS:"; X%, Y%
Line 662 ⟶ 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 680 ⟶ 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,411 ⟶ 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,423 ⟶ 1,737:
var m := new Integer();
console.write:("Enter two space delimited integers:");
console.loadLine(n,m);
Line 1,441 ⟶ 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,628 ⟶ 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,641 ⟶ 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 2,118 ⟶ 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,578 ⟶ 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,819 ⟶ 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,843 ⟶ 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,339 ⟶ 3,711:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin, Stdout
 
var x
18

edits