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

Content added Content deleted
(Added solution for Action!)
Line 97: Line 97:
DBRA D6,loop_destroyArray ;decrement this, D7 is $FFFF each time execution gets here so this acts as a "carry" of sorts.
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.</lang>
;if this value was 0 prior to the loop, the loop ends immediately.</lang>

=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<lang Action!>CARD EndProg ;required for ALLOCATE.ACT

INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!

DEFINE INT_SIZE="2"
DEFINE CARD_SIZE="2"
TYPE IntArray2D=[BYTE rows,cols CARD ptr]

BYTE FUNC GetNumber(CHAR ARRAY s)
BYTE n,min=[1],max=[100]

DO
PrintF("Get number of %S (%B..%B): ",s,min,max)
n=InputB()
IF n>=min AND n<=max THEN
EXIT
FI
OD
RETURN (n)

PROC Create(IntArray2D POINTER a)
CARD ARRAY rowArray
BYTE row

IF a.ptr#0 THEN Break() FI
rowArray=Alloc(a.rows*CARD_SIZE)
a.ptr=rowArray
FOR row=0 TO a.rows-1
DO
rowArray(row)=Alloc(a.cols*INT_SIZE)
OD
RETURN

PROC Destroy(IntArray2D POINTER a)
CARD ARRAY rowArray
BYTE row

IF a.ptr=0 THEN Break() FI
rowArray=a.ptr
FOR row=0 TO a.rows-1
DO
Free(rowArray(row),a.cols*INT_SIZE)
OD
Free(a.ptr,a.rows*CARD_SIZE)
a.ptr=0
RETURN

PROC SetValue(IntArray2D POINTER a BYTE row,col INT v)
CARD ARRAY rowArray
INT ARRAY colArray

IF a.ptr=0 OR row>=a.rows OR col>=a.cols THEN
Break()
FI
rowArray=a.ptr
colArray=rowArray(row)
colArray(col)=v
RETURN

INT FUNC GetValue(IntArray2D POINTER a BYTE row,col)
CARD ARRAY rowArray
INT ARRAY colArray

IF a.ptr=0 OR row>=a.rows OR col>=a.cols THEN
Break()
FI
rowArray=a.ptr
colArray=rowArray(row)
RETURN (colArray(col))

PROC TestCreate(IntArray2D POINTER a)
PrintF("Create array of %B rows and %B cols%E",a.rows,a.cols)
Create(a)
RETURN

PROC TestDestroy(IntArray2D POINTER a)
PrintF("Destroy array of %B rows and %B cols%E",a.rows,a.cols)
Destroy(a)
RETURN

PROC TestSetValue(IntArray2D POINTER a BYTE row,col INT v)
PrintF("Write %I to row %B and col %B%E",v,row,col)
SetValue(a,row,col,v)
RETURN

PROC TestGetValue(IntArray2D POINTER a BYTE row,col)
INT v

v=GetValue(a,row,col)
PrintF("Read at row %B and col %B: %I%E",row,col,v)
RETURN

PROC Main()
IntArray2D a

Put(125) PutE() ;clear screen
AllocInit(0)

a.rows=GetNumber("rows")
a.cols=GetNumber("cols")
a.ptr=0

TestCreate(a)
TestSetValue(a,a.rows/2,a.cols/2,6502)
TestGetValue(a,a.rows/2,a.cols/2)
TestDestroy(a)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Create_a_two-dimensional_array_at_runtime.png Screenshot from Atari 8-bit computer]
<pre>
Get number of rows (1..100): 80
Get number of cols (1..100): 90
Create array of 80 rows and 90 cols
Write 6502 to row 40 and col 45
Read at row 40 and col 45: 6502
Destroy array of 80 rows and 90 cols
</pre>


=={{header|Ada}}==
=={{header|Ada}}==