How to get matrix size in Swift

1 Answer

0 votes
import Foundation

let matrix = [
    [2, 0, 5, 9],
    [0, 4, 0, 3],
    [0, 8, 0, 1]
]

// Get the number of rows
let rows = matrix.count

// Get the number of columns
let columns = matrix[0].count

let totalCells = rows * columns

print("Matrix size: \(rows) rows x \(columns) columns")
print("Total Cells: \(totalCells)")



/*
run:

Matrix size: 3 rows x 4 columns
Total Cells: 12

*/

 



answered Oct 2, 2025 by avibootz
...