FAQ: Data Structures - Arrays of Arrays

This community-built FAQ covers the “Arrays of Arrays” exercise from the lesson “Data Structures”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Arrays of Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is the multidimensional array 2 dimensional? There are 4 arrays within the example multidimensional array no?

The first thng we need to appreciate about arrays is their linearity. Arrays are linear in nature.

When the elements of an array are themselves arrays, it has no bearing on the linearity of the parent array. It’s an array of arrays. Arrays are not intended to emulate tables so there is no x by y dimensioning in their definition.

Hi everyone,
what does the “#” sign job in the code,
and how come you can use x and n
without declaring them
regarding this code

multi_d_array.each { |x| puts “#{x}\n” }

3 Likes

The # when paired up with {} signifies expression interpolation. Notice in your code it is written,

puts "#{x}\n"

which literally means,

put string comprised of an expression containing the current value of x and a newline escape character.

3 Likes

thank you for your reply, and how did the “n” affected the output, and again without declaring it before

1 Like

Written as an escape sequence, \n inserts a newline character (line break) which when added to the one that puts inserts gives double line spacing.

3 Likes

multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

multi_d_array.each { |x| puts “#{x}\n” }

Hi, could somebody please explain why we use { |x| puts “#{x}\n” }. I am a little confused

The Array.each method takes a block, the { } which encloses the code that will run on each iteration of the array. |x| is called the block parameter, a variable local to the block which binds to one element in the array at a time. In the above, we puts that value to the display. "#{x}\n" is a string which employs interpolation of the variable’s value along with a newline character which when combined with the one Ruby inserts after every puts gives a blank line between each print out. puts is short for, put string.

Expected output

[0, 0, 0, 0]

[0, 0, 0, 0]

[0, 0, 0, 0]

[0, 0, 0, 0]

2 Likes

why do I need to type
puts x
instead of
puts “#{x}\n”

If the puts automatically adds a newline anyway, isn’t the extra code unnecessary? Does it mean something that I am not advanced enough to understand yet?

1 Like

Hi!

I still don’t understand what a two dimensional array is? Could anyone please explain in plans terms?

Thanks

A simple form of two dimensional array would be a table with two rows, and two columns. That means each row has two cells.

[
  [ cell_a, cell_b ],
  [ cell_c, cell_d ]
]

The outer brackets represent the single dimension array, and the inner brackets represent each a single dimension array. An array of single dimension arrays is a two dimension array. There are rows from top to bottom, and columns from left to right.

To access each element we must supply both a row and a column subscript. Let’s call our array, a:

a[0][0] -> cell_a
a[0][1] -> cell_b
a[1][0] -> cell_c
a[1][1] -> cell_d

Hello, thank you for your reply and sorry for the delay with mine.

I’m not sure what you mean by rows and columns. Do you mean each line of code is a row and each column is separated by the commas?

Thanks

Hi everyone.

I’m going over this lesson again, but I’m very confused by the language used.

It says that arrays of arrays are called multidimensional arrays and we have an array of arrays in the editor. Then it goes on to say the array in the editor is a two-dimensional array. This sounds like a contradiction to me. What am I missing and why don’t they explain what a two-dimensional array is?

Thanks.

Hi ! I am confused with the array situation.

I’ve wrote the following two-dimensional array which seems to be fine :
my_2d_array = [[1,2][1,2][1,2]]

Yet if I write this it says it’s wrong and gives me the error: wrong number of arguments (given 3, expected 1…2)
my_2d_array = [[1,2,3][1,2,3][1,2,3]]

Why can’t I have 3 items in the array? In the example they provide there are 4
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

You must separate the elements of the array by commas:

# You wrote:
my_2d_array = [[1,2][1,2][1,2]]
my_2d_array = [[1,2,3][1,2,3][1,2,3]]

# It should be:
my_2d_array = [[1,2], [1,2], [1,2]]
my_2d_array = [[1,2,3], [1,2,3], [1,2,3]]

In terms of the examples you wrote, the first example worked because of fortuitous circumstances even though you omitted the commas. Even though it “worked” (as in didn’t throw an error), the result is not what you intended.

First, have a look at the documentation for the different ways you can access the elements of an array:
https://ruby-doc.org/core-3.1.0/Array.html#class-Array-label-Accessing+Elements

x = [3, 55, 11, 4, 66, 48]

# Syntax: array[Index]
puts x[1]  
# Output: 55 -> Element at index 1

# Syntax: array[index, length]
puts x[2, 4] 
# [11, 4, 66, 48] -> Four elements starting at index 2

# Syntax: array[startIndex..endIndex]
puts x[2..4] 
# [11, 4, 66] -> Elements from index 2 through index 4 (inclusive)

With the above in mind, look at how the examples you wrote are evaluated step-by-step:

# EXAMPLE 1: my_2d_array = [[1,2][1,2][1,2]]

my_2d_array = []
# Outer brackets create an empty array 

my_2d_array = [[1, 2]]
# Single element of my_2d_array
# This element is an array itself with two numbers as elements

my_2d_array = [[1, 2][1, 2]]
# No comma between the square brackets, so it is interpreted as 
# an attempt to access elements of the first array.
# Starting at index 1 of the first array, a length of 2 elements is
# specified. Now, effectively my_2d_array is [[2]]

my_2d_array = [[1, 2][1, 2][1,2]]
# Again no comma, so it is interpreted as 
# an attempt to access elements of the array [2] (from previous step).
# Starting at index 1 of the array [2], a length of 2 elements is
# specified. But, there are no elements at index 1 or greater, so
# empty array is created.
# Now my_2d_array is [[]] i.e. an array whose only element is 
# an empty array.
# Not the result you intended, but still valid syntax by chance.

##############################################################

# EXAMPLE 2: my_2d_array = [[1,2,3][1,2,3][1,2,3]]

my_2d_array = []
# Outer brackets create an empty array 

my_2d_array = [[1, 2, 3]]
# Single element of my_2d_array
# This element is an array itself with three numbers as elements


my_2d_array = [[1, 2, 3][1, 2, 3]]
# No comma between the square brackets, so it is interpreted as 
# an attempt to access elements of the first array.
# ERROR: wrong number of arguments (given 3, expected 1..2)
# One argument:
# [1,2,3][1] would select element at index 1 and my_2d_array would be [2]
# Two arguments:
# [1,2,3][1, 2] would select two elements starting at index 1 and 
# my_2d_array would be [[2,3]]
# Three arguments:
# [1,2,3][1, 2, 3] INVALID SYNTAX 
# as 3 arguments weren't expected (only 1 or 2 arguments)
1 Like

A two-dimensional array is a multidimensional array. Three or four (or more) dimensional arrays are also multidimensional arrays.

Suppose, you recorded the ages of people and stored them in an array:

# 1-dimensional array
ages = [1, 4, 66, 33, 68, 53, 32, 22, 58, 11, 90]

# 1 dimension needed to access element
puts ages[2] 
# 66
puts ages[10]
# 90

Now, suppose we chose to record the names of the people along with their age in an array of arrays:

# 2-dimensional array
people = [["Jake", 5], ["Harry", 42], ["Jane", 15], ["Floyd", 53], ["Anna", 8]]
# For better readability, we can also write it as:
people = [
                ["Jake", 5], 
                ["Harry", 42], 
                ["Jane", 15], 
                ["Floyd", 53], 
                ["Anna", 8]
]

# 2 dimensions needed to access element
puts people[3][0] 
# "Floyd"
puts people[4][1]
# 8

For the above, you could think of each array as a row and each element in a row as being in a column i.e. you can think of each row being Person1, Person2, … Person5 and within each row, the elements can be thought of as being in two columns: Column 1: Name, Column 2: Age
Another 2D Array may be a tic-tac-toe with 3 rows and 3 columns:

board = [["-", "O", "X"], ["X", "-", "-"], ["O", "-", "X"]]
# or for better readability
board = [
               ["-", "O", "X"], 
               ["X", "-", "-"], 
               ["O", "-", "X"]
]

board[1][2] = "O"
# will put an O in the second row and third column.

Suppose we wanted to represent the 6 faces of a Rubik’s cube. We could use a 3-dimensional array i.e. an array of arrays of arrays (OR an array of 2D arrays):

rubik = [
            [
                ["r", "r", "r"], 
                ["r", "r", "r"], 
                ["r", "r", "r"]
            ], 

            [
                ["g", "g", "g"], 
                ["g", "g", "g"], 
                ["g", "g", "g"]
            ],

            [
                ["y", "y", "y"], 
                ["y", "y", "y"], 
                ["y", "y", "y"]
            ],

            [
                ["w", "w", "w"], 
                ["w", "w", "w"], 
                ["w", "w", "w"]
            ],
 
            [
                ["b", "b", "b"], 
                ["b", "b", "b"], 
                ["b", "b", "b"]
            ],

            [
                ["o", "o", "o"], 
                ["o", "o", "o"], 
                ["o", "o", "o"]
            ]
]

# Colors: red, blue, yellow, green, white, orange
# 3 dimensions needed to access element
puts rubik[1][2][0]
# "g"

puts rubik[3][1][2]
# "w"
3 Likes

Thank you SO much for this super in depth explanation!! I think I’ve become an expert on arrays now :smile:
It all makes so much more sense now!

1 Like

Hello mtrtmk and thank you for such a great and detailed explanation. I really wish they’d go into more detailed explanations like this in the lessons, but I still don’t fully understand.

  1. Just so I don’t get confused, should puts ages[2] output 66?

  2. Why doesn’t puts people[3][0] output “Floyd” 53? and why doesn’t puts people[4][1] output “Anna” 88?

  3. Why is the tic tac toe a 2-D array, but the Rubik’s cube is 3-D?

I know, I’m hopeless :face_with_spiral_eyes:

Thanks

Yes, my mistake. ages[2] will access the third element of the ages array. The third element is 66. I mistakenly wrote the value of the second element. I have edited the post to correct that.

The array in my example is

 people = [["Jake", 5], ["Harry", 42], ["Jane", 15], ["Floyd", 53], ["Anna", 8]]

It is an array of arrays i.e. an array whose every element is also an array.
people[3] will access the fourth element of the people array.

// Using print instead of puts so that all output is on one line.
print people[3]
# Output:  ["Floyd", 53]

people[3][0] will access the first element of the above array.

print people[3][0]
# Output:  "Floyd"

Similarly,

// 5th element (which is also an array) of people array
print people[4] 
# Output: ["Anna", 8]

// 2nd element of the 5th element (which is an array) of people array
print people[4][1] 
# Output: 8

The tic-tac-toe board is an array of arrays i.e. every element of the array is an array also.

board = [ [...], [...], [...] ]

scrnsh1

The Rubik’s cube is an array of arrays of arrays i.e. an array whose every element is an array. Every element of those arrays is also an array.

rubik = [ 
[ [...], [...], [...]  ], 
[ [...], [...], [...]  ], 
[ [...], [...], [...]  ], 
[ [...], [...], [...]  ], 
[ [...], [...], [...]  ], 
[ [...], [...], [...]  ] 
]

scrnsh2

In the Rubik’s cube image above, the blue-arrow array represents the Rubik cube as a whole. The red-arrow arrays represent the six faces of the cube. The green-arrow arrays represent the rows in each face. The strings (“red”, “blue”, “green”, “yellow”, “white”, “orange”) within the green arrays can be thought of as being in columns.


Image 1 Source: https://i.imgur.com/4547E52.png
Image 2 Source: “Rubik’s Cube” by SoheilK is licensed under CC BY-NC-ND 2.0.

1 Like