Jump to content

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

no edit summary
No edit summary
Line 4:
 
=={{header|Ada}}==
<lang ada>
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
Line 26:
-- The variable Matrix is popped off the stack automatically
end Two_Dimensional_Arrays;
</adalang>
 
=={{header|ALGOL 68}}==
Line 361:
{{works with|Python| 2.5}}
 
<lang python> width = int(raw_input("Width of myarray: "))
height = int(raw_input("Height of Array: "))
myarray = [[0] * width for i in xrange(height)]
myarray[0][0] = 3.5
print myarray[0][0]</pythonlang>
 
'''Note:''' Some people may instinctively try to write myarray as [[0] * width] * height, but the * operator creates ''n'' references to [[0] * width]
Line 371:
You can also use a two element tuple to index a dictionary like so:
 
<lang python> myarray = dict(((w,h), 0) for w in range(width) for h in range(height))
myarray[(0,0)] = 3.5
print myarray[(0,0)]</pythonlang>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.