How do I define a dynamic Matrix or

Hello Team,

On my quest to learn Ruby, I implemented a known algorithm to solve an odd
nXn magic square.
I tested my code using a 3x3 and a 5x5 magic squares.
The problem that I am having is that I hard-code the initialization matrix.
In other words, if it is a 3x3 I define:

mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

For a 5 x 5 I define :

mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0, 0],[0,0,0,0,0],[0,0,0,0,0]]

I want to be able to solve any size odd magic square. To do so, I need to be
able to define my matrix dynamically and on demand.
Can anyone tell me how to do that?

Attached is my current code. Please feel free to criticize it and make
recommendations. My disclaimer: I am a Ruby apprentice.

Thank you

Victor

#!/usr/local/bin/ruby -d -W0

def mag(size)
    r = 0 # row
    c = 0 # column
    index = 0 # pointer

   # mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0,
0],[0,0,0,0,0],[0,0,0,0,0]]

    maxLoop = size * size # Magic Square size

    1.upto(maxLoop) do |n|

      case
          when n == 1
              c = (size / 2).floor # Will keep the
integer part, giving us the center in row 0
              mm[c][r] = n # This is: mm[c][0]
          when index >= size
              index = 0
              r = r + 1
              mm[c][r] = n
              puts " "
          else
              c = c + 1
              if c >= size
                c = 0
              end

              r = r -1
              if r < 0
                r = size - 1
              end
              mm[c][r] = n
          end
          index = index + 1

      puts "[#{c}][#{r}] = #{mm[c][r]} "

   end # End upto

···

##############
    # Output the matrix #
    ##############
    puts
    0.upto(size -1) do |r|
      0.upto(size -1) do |c|
        print "#{mm[c][r]} "
      end
      puts " "
    end

end # End method mag

# We start here

p1 = ARGV[0].to_i

mag( p1 )

Hi --

···

On 28/10/2007, Victor Reyes <victor.reyes@gmail.com> wrote:

Hello Team,

On my quest to learn Ruby, I implemented a known algorithm to solve an odd
nXn magic square.
I tested my code using a 3x3 and a 5x5 magic squares.
The problem that I am having is that I hard-code the initialization matrix.
In other words, if it is a 3x3 I define:

mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

For a 5 x 5 I define :

mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0, 0],[0,0,0,0,0],[0,0,0,0,0]]

I want to be able to solve any size odd magic square. To do so, I need to be
able to define my matrix dynamically and on demand.
Can anyone tell me how to do that?

Attached is my current code. Please feel free to criticize it and make
recommendations. My disclaimer: I am a Ruby apprentice.

Thank you

Victor

#!/usr/local/bin/ruby -d -W0

def mag(size)
    r = 0 # row
    c = 0 # column
    index = 0 # pointer

   # mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0,
0],[0,0,0,0,0],[0,0,0,0,0]]

You can just do something like this:

mm =
size.to_i.times { mm << Array.new(size.to_i,0) }

Incidentally, have you looked into the numerous Matrix/math libs
already defined in Ruby?

-- Thomas Adam

Victor Reyes wrote:

..I need to be
able to define my matrix dynamically and on demand.
Can anyone tell me how to do that?

Try this:

#get input:
print 'rows: '
rows = Integer(gets)
print 'columns: '
cols = Integer(gets)

#create matrix:
matrix =
rows.times do |i|
  matrix << Array.new(cols, 0)
end

p matrix

Or this:

require 'matrix'

print "size: "
size = Integer(gets)

m = Matrix.zero(size)
p m

···

--
Posted via http://www.ruby-forum.com/\.

Array.new(odd_matrix_number) {0} }

I'm not sure exactly what is your assignment, but it might be good to
create a MagicSquare object.

My half a cent.

Todd

···

On 10/28/07, Victor Reyes <victor.reyes@gmail.com> wrote:

Hello Team,

On my quest to learn Ruby, I implemented a known algorithm to solve an odd
nXn magic square.
I tested my code using a 3x3 and a 5x5 magic squares.
The problem that I am having is that I hard-code the initialization matrix.
In other words, if it is a 3x3 I define:

mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

For a 5 x 5 I define :

mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0, 0],[0,0,0,0,0],[0,0,0,0,0]]

I want to be able to solve any size odd magic square. To do so, I need to be
able to define my matrix dynamically and on demand.
Can anyone tell me how to do that?

Attached is my current code. Please feel free to criticize it and make
recommendations. My disclaimer: I am a Ruby apprentice.

Thank you

Victor

#!/usr/local/bin/ruby -d -W0

def mag(size)
    r = 0 # row
    c = 0 # column
    index = 0 # pointer

   # mm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    mm = [[0,0,0, 0, 0], [0,0,0, 0, 0], [0,0,0, 0,
0],[0,0,0,0,0],[0,0,0,0,0]]

    maxLoop = size * size # Magic Square size

    1.upto(maxLoop) do |n|

      case
          when n == 1
              c = (size / 2).floor # Will keep the
integer part, giving us the center in row 0
              mm[c][r] = n # This is: mm[c][0]
          when index >= size
              index = 0
              r = r + 1
              mm[c][r] = n
              puts " "
          else
              c = c + 1
              if c >= size
                c = 0
              end

              r = r -1
              if r < 0
                r = size - 1
              end
              mm[c][r] = n
          end
          index = index + 1

      puts "[#{c}][#{r}] = #{mm[c][r]} "

   end # End upto

    ##############
    # Output the matrix #
    ##############
    puts
    0.upto(size -1) do |r|
      0.upto(size -1) do |c|
        print "#{mm[c][r]} "
      end
      puts " "
    end

end # End method mag

# We start here

p1 = ARGV[0].to_i

mag( p1 )

odd_matrix_number = 7
my_matrix - Array.new(odd_matrix_number) {

Many thanks to Thomas and 7stud!
I will try your suggestions!

Thanks again

Victor

···

On 10/28/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Victor Reyes wrote:
> ..I need to be
> able to define my matrix dynamically and on demand.
> Can anyone tell me how to do that?
>

Try this:

#get input:
print 'rows: '
rows = Integer(gets)
print 'columns: '
cols = Integer(gets)

#create matrix:
matrix =
rows.times do |i|
  matrix << Array.new(cols, 0)
end

p matrix

Or this:

require 'matrix'

print "size: "
size = Integer(gets)

m = Matrix.zero(size)
p m
--
Posted via http://www.ruby-forum.com/\.

> odd_matrix_number = 7
> my_matrix - Array.new(odd_matrix_number) {
Array.new(odd_matrix_number) {0} }

I'm not sure exactly what is your assignment, but it might be good to
create a MagicSquare object.

My half a cent.

Todd

That is supposed to be...

my_matrix = Array.new(odd_matrix_number) {

Array.new(odd_matrix_number) {0} }

("=" in place of the original "-")

Somebody get me away from this particular keyboard :slight_smile:

Todd

···

On 10/28/07, Todd Benson <caduceass@gmail.com> wrote:

Thank you gents, for your suggestions and help.

Victor

···

On 10/28/07, Todd Benson <caduceass@gmail.com> wrote:

On 10/28/07, Todd Benson <caduceass@gmail.com> wrote:

> > odd_matrix_number = 7
> > my_matrix - Array.new(odd_matrix_number) {
> Array.new(odd_matrix_number) {0} }
>
> I'm not sure exactly what is your assignment, but it might be good to
> create a MagicSquare object.
>
> My half a cent.
>
> Todd

That is supposed to be...

> my_matrix = Array.new(odd_matrix_number) {
Array.new(odd_matrix_number) {0} }

("=" in place of the original "-")

Somebody get me away from this particular keyboard :slight_smile:

Todd