Welcome, guest | Sign In | My Account | Store | Cart

The next example shows how to create rectangular and jagged arrays.

Ruby, 39 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Console = System::Console

puts 'A rectangular array:'
#wake up, Neo! matrix has you :)
matrix = Array.new(6), Array.new(6)
i = 0
while i < 6
  j = 0
  while j < 6
    Console.Write((matrix[i, j] = i * j).ToString() + "\t")
    j += 1
  end
  puts
  i += 1
end

puts

puts 'A jagged array:'
#it is not a jagged array yet!
jagged = Array.new(5)
#creating jagged array
i = 0
while i < jagged.length
  jagged[i] = Array.new(i + 7)
  i += 1
end
#print contents
i = 0
while i < 5
  Console.Write("Length of row {0} is {1}:\t", i, jagged[i].length)
  j = 0
  while j < jagged[i].length
    Console.Write((jagged[i][j]).ToString() + " ")
    j += 1
  end
  puts
  i +=1
end