2 dimentional array in ruby - niewbie

Please help
I want to create a multiplication table in ruby.
I want a 2 dimentional array
and the do something like that

2darray.new
for row in 1…10
for col in 1…10
2darray[row,col]=row*col
end
end

How do I do it in Ruby

···


Jacek Podkanski

How do I do it in Ruby

How about this:

ruby xxyymm.rb
rows-first: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12], [0, 4, 8, 12, 16], [0, 5, 10, 15, 20], [0, 6, 12, 18, 24], [0, 7, 14, 21, 28]]
cols-first: [[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7], [0, 2, 4, 6, 8, 10, 12, 14], [0, 3, 6, 9, 12, 15, 18, 21], [0, 4, 8, 12, 16, 20, 24, 28]]
expand -t2 xxyymm.rb
x=5
y=8

data = Array.new(y) { Array.new(x) }
for j in (0…y-1)
for i in (0…x-1)
data[j][i] = i * j
end
end

puts “rows-first: #{data.inspect}”
puts “cols-first: #{data.transpose.inspect}”

···

On Sun, 22 Jun 2003 01:14:38 +0100, Jacek Podkanski wrote:


Simon Strandgaard

There’s some information here.

http://www.rubygarden.org/ruby?MultiDimensionalArrays

Unfortunately, this page is not returned when one searches the Wiki
for “multi array”.

Gavin

···

On Sunday, June 22, 2003, 9:24:51 AM, Jacek wrote:

I want a 2 dimentional array

Jacek Podkanski wrote:

Please help
I want to create a multiplication table in ruby.
I want a 2 dimentional array
and the do something like that

2darray.new
for row in 1…10
for col in 1…10
2darray[row,col]=row*col
end
end

How do I do it in Ruby

I think I worked out how to do it, but still I don’t understand why the 2
outouts are different, and only second is correct.

#! /usr/bin/ruby

#table acces function
def ta(table,row,column,value)
r=table[row]; r[column]=value
end

create 2 dimensional array

t=Array.new
for x in 0…9
row=Array.new
for y in 0…9
row[y]=x+1*y+1
end#for
t<<row
end#for

···

#######################################
puts t.inspect

#create multiplication table
for x in 0…9
for y in 0…9
ta(t,x,y,x+1*y+1)
end
end

puts t.inspect


Jacek Podkanski

This is my way of doing it. It’s different from other’s suggestions, but it
suits my needs. I anyody comes up with shorter solution please let me know.

#! /usr/bin/ruby

#table acces function
def ta(table,row,column,value)
r=table[row]; r[column]=value
end

create 2 dimensional array

t=Array.new
for x in 0…9
row=Array.new
for y in 0…9
row[y]=(x+1)*(y+1)
end#for
t<<row
end#for

···

#######################################
puts t.inspect

#create multiplication table
for x in 0…9
for y in 0…9
ta(t,x,y,(x+1)*(y+1) )
end
end

puts t.inspect


Jacek Podkanski

How about an OO solution:

class MultTable
attr_reader :dim_x, :dim_y

def initialize(dim_x, dim_y)
@dim_x, @dim_y = dim_x, dim_y
end

def ; x*y; end

def row(x)
a=
dim_y.times{|y| a << self[x,y]}
a
end

def col(y)
a=
dim_x.times{|x| a << self[x,y]}
a
end
end

mt = MultTable.new(10, 10)

mt.dim_x.times do |x|
p mt.row(x)
end

Cheers

robert

“Jacek Podkanski”
<jacekpodkanski.at.supanet.dot.com@somewhere.where.you.should.npt.spam.com

schrieb im Newsbeitrag
news:NF5Ja.3024$425.126690@newsfep2-gui.server.ntli.net

···

Please help
I want to create a multiplication table in ruby.
I want a 2 dimentional array
and the do something like that

2darray.new
for row in 1…10
for col in 1…10
2darray[row,col]=row*col
end
end

How do I do it in Ruby

Jacek Podkanski

“Jacek Podkanski” wrote:

This is my way of doing it. It’s different from other’s suggestions, but it
suits my needs. I anyody comes up with shorter solution please let me know.

#! /usr/bin/ruby

#table acces function
def ta(table,row,column,value)
r=table[row]; r[column]=value
end

create 2 dimensional array

t=Array.new
for x in 0…9
row=Array.new
for y in 0…9
row[y]=(x+1)*(y+1)
end#for
t<<row
end#for
#######################################
puts t.inspect


Jacek Podkanski

Hi Jacek,

map (same as collect) returns an array, so you can
create an array of 10 arrays, each having 10 elements
like this …

Create multiplication table

Mult = (1…10).map{|x| (1…10).map{|y| x * y }}

Table access function

def ta(row, column)
Mult[row-1][column-1]
end

Test it

p ta(1, 1) #=> 1
p ta(7, 6) #=> 42
p ta(6, 7) #=> 42
p ta(10, 10) #=> 100

Display table

Mult.each{|r| r.each{|c| printf(“%4d”, c)}; puts}

daz