Menu
Coddy logo textTech

Recap - Matrix Operations

Part of the Logic & Flow section of Coddy's Ruby journey — lesson 14 of 56.

Pull together creation, access, iteration, and the common patterns in one challenge.

challenge icon

Challenge

Medium

Read n from input, the size of a square matrix. Build an n × n matrix where each cell at row r, column c equals r + c.

Then print, on separate lines:

  1. The matrix using inspect on the outer array
  2. The sum of all cells
  3. The maximum value on the main diagonal
  4. The transpose using inspect

For input 3, the output is:

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
18
4
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

(For this construction, the matrix happens to equal its own transpose.)

Try it yourself

n = gets.to_i

# TODO: build the n×n matrix and print the four required lines

All lessons in Logic & Flow