# Learning Ruby with AtCoder 13 How to make a two-dimensional array

## Introduction

Participating in competitive programming as part of learning Ruby and algorithms. Here, we output what we learned during learning.

This time about how to make a two-dimensional array. In the 3rd algorithm test 2nd question Dynamic scoring There is a scene where I want to use a two-dimensional array. I've had a little trouble here, so I'll summarize it for review.

## Thing you want to do

I wanted to do something like the following, so I decided to make a two-dimensional array.

For example Three participants (1,2,3) participate in an exam. The exam will have three questions. Manage whether each participant was able to answer each question.

The image looks like the one below.

#### `image`

``````
#① Make a two-dimensional array like this
[[0, 0, 0],[0, 0, 0],[0, 0, 0]]

#② If Participant 1 answers the first question correctly, put 1 in the corresponding element.
[[1, 0, 0],[0, 0, 0],[0, 0, 0]] #1 in the first element of the array of first elements

#③ Next, if Participant 3 answers the second question correctly, put 1 in the corresponding element.
[[1, 0, 0],[0, 0, 0],[0, 1, 0]] #1 in the second element of the third element array
``````

## ① What you did first (wrong)

``````ary = Array.new(3, Array.new(3, 0))
print ary
# => [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
``````

As for the appearance, I have made what I want to make, When I try to record the answer

``````ary[0][0] = 1
print ary
# => [[1, 0, 0],[1, 0, 0],[1, 0, 0]]
``````

I intended to make changes only to the array of the first element, but it affected all the arrays.

## ② Use the map method (correct answer!)

``````ary = Array.new(3).map{Array.new(3,0)}
print ary
# => [[0, 0, 0],[0, 0, 0],[0, 0, 0]]

ary[0][0] = 1
print ary
# => [[1, 0, 0],[0, 0, 0],[0, 0, 0]]
``````

As a result, by entwining the map method, I was able to safely create the array as I imagined.

## What's the difference

The two-dimensional arrays (1) and (2) above look exactly the same, but they seem to have completely different properties. I will think about what is different.

### About new method ① of Array class

In Reference, it is written as follows.

new(size = 0, val = nil) -> Array

Generates an array of length size, initializes each element with val and returns it. Note that val is not duplicated element by element. All elements refer to the same object val.

#### `Example`

``````
ary = Array.new(3, "foo")
p ary                     #=> ["foo", "foo", "foo"]
ary[0].capitalize!
p ary                     #=> ["Foo", "Foo", "Foo"]
``````

In this example, the "foo" in each element is shown to be the same object. As the description says, all elements are the same object val.

Then, is the two-dimensional array created in ① the following image?

Even if the elements appear to be divided into three parts in the array, they only refer to the same object in the end. It seems that no matter which element you change, the same object will change.

Is it such an image?

In Reference, it is written as follows.

map -> Enumerator map {|item| ... } -> [object]

Returns an array containing all the results of evaluating the block for each element. If you omit the block, perform the iteration described above and Returns an Enumerator object that returns the resulting array.

#### `Example`

``````
#All triple
p [1, 2, 3].map {|n| n * 3 }  # => [3, 6, 9]
``````

The map method, like the each method, extracts elements one by one from the array object and Returns the result of processing in the block {} as an array.

So, does the two-dimensional array created in ② have the following image?

Since an array is generated separately for each element, they exist as independent objects. So it seems that you can make changes separately.

## Finally

So far, I have summarized the points that I stumbled upon in how to create a two-dimensional array. I intend to summarize it while reading the reference etc. If you have any mistakes, I would be grateful if you could point them out.