This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,14 @@
USING: io kernel math.matrices math.parser prettyprint
sequences ;
IN: rosettacode.runtime2darray
: set-Mi,j ( elt {i,j} matrix -- )
[ first2 swap ] dip nth set-nth ;
: Mi,j ( {i,j} matrix -- elt )
[ first2 swap ] dip nth nth ;
: example ( -- )
readln readln [ string>number ] bi@ zero-matrix ! create the array
[ [ 42 { 0 0 } ] dip set-Mi,j ] ! set the { 0 0 } element to 42
[ [ { 0 0 } ] dip Mi,j . ] ! read the { 0 0 } element
bi ;

View file

@ -0,0 +1,15 @@
# Creating an array of 0
a := NullMat(2, 2);
# [ [ 0, 0 ], [ 0, 0 ] ]
# Some assignments
a[1][1] := 4;
a[1][2] := 5;
a[2][1] := 3;
a[2][2] := 4;
a
# [ [ 4, 5 ], [ 3, 4 ] ]
Determinant(a);
# 1

View file

@ -0,0 +1,3 @@
def make2d = { nrows, ncols ->
(0..<nrows).collect { [0]*ncols }
}

View file

@ -0,0 +1,19 @@
def r = new Random()
System.in.splitEachLine(/,\s*/) { dim ->
def nrows = dim[0] as int
def ncols = dim[1] as int
def a2d = make2d(nrows, ncols)
def row = r.nextInt(nrows)
def col = r.nextInt(ncols)
def val = r.nextInt(nrows*ncols)
a2d[row][col] = val
println "a2d[${row}][${col}] == ${a2d[row][col]}"
a2d.each { println it }
println()
}

View file

@ -0,0 +1,7 @@
REAL :: array(1)
DLG(NameEdit=rows, NameEdit=cols, Button='OK', TItle='Enter array dimensions')
ALLOCATE(array, cols, rows)
array(1,1) = 1.234
WRITE(Messagebox, Name) array(1,1)

View file

@ -0,0 +1,9 @@
read, x, prompt='Enter x size:'
read, y, prompt='Enter y size:'
d = fltarr(x,y)
d[3,4] = 5.6
print,d[3,4]
;==> outputs 5.6
delvar, d

View file

@ -0,0 +1,13 @@
procedure main(args)
nr := integer(args[1]) | 3 # Default to 3x3
nc := integer(args[2]) | 3
A := list(nr)
every !A := list(nc)
x := ?nr # Select a random element
y := ?nc
A[x][y] := &pi
write("A[",x,"][",y,"] -> ",A[x][y])
end

View file

@ -0,0 +1,2 @@
array1=:i. 3 4 NB. a 3 by 4 array with arbitrary values
array2=: 5 6 $ 2 NB. a 5 by 6 array where every value is the number 2

View file

@ -0,0 +1 @@
array1=: 99 (<0 0)} array1

View file

@ -0,0 +1,9 @@
task=: verb define
assert. y -: 0 0 + , y NB. error except when 2 dimensions are specified
INIT=. 0 NB. array will be populated with this value
NEW=. 1 NB. we will later update one location with this value
ARRAY=. y $ INIT NB. here, we create our 2-dimensional array
INDEX=. < ? $ ARRAY NB. pick an arbitrary location within our array
ARRAY=. NEW INDEX} ARRAY NB. use our new value at that location
INDEX { ARRAY NB. and return the value from that location
)

View file

@ -0,0 +1,3 @@
'init new' =. ' ';'x' NB. literals
'init new' =. 1r2;2r3 NB. fractions
'init new' =. a: ; <<'Rosetta' NB. boxes

View file

@ -0,0 +1,9 @@
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

View file

@ -0,0 +1,3 @@
make "a2 mdarray [5 5]
mdsetitem [1 1] :a2 0 ; by default, arrays are indexed starting at 1
print mditem [1 1] :a2 ; 0

View file

@ -0,0 +1,11 @@
a = getKBValue prompt:"Enter first dimension:"
b = getKBValue prompt:"Enter second dimension:"
arr1 = #()
arr2 = #()
arr2[b] = undefined
for i in 1 to a do
(
append arr1 (deepCopy arr2)
)
arr1[a][b] = 1
print arr1[a][b]

View file

@ -0,0 +1,11 @@
ARA2D
NEW X,Y,A,I,J
REARA
WRITE !,"Please enter two positive integers"
READ:10 !,"First: ",X
READ:10 !,"Second: ",Y
GOTO:(X\1'=X)!(X<0)!(Y\1'=Y)!(Y<0) REARA
FOR I=1:1:X FOR J=1:1:Y SET A(I,J)=I+J
WRITE !,"The corner of X and Y is ",A(X,Y)
KILL X,Y,A,I,J
QUIT

View file

@ -0,0 +1,12 @@
> a := Array( 1 .. 3, 1 .. 4 ): # initialised to 0s
> a[1,1] := 1: # assign an element
> a[2,3] := 4: # assign an element
> a; # display the array
[1 0 0 0]
[ ]
[0 0 4 0]
[ ]
[0 0 0 0]
> a := 'a': # unassign the name
> gc(); # force a garbage collection; may or may not actually collect the array, but it will be eventually

View file

@ -0,0 +1,4 @@
arrayFun[m_Integer,n_Integer]:=Module[{array=ConstantArray[0,{m,n}]},
array[[1,1]]=RandomReal[];
array[[1,1]]
]