gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
N-square matrix in kotlin
- User
N-square matrix in kotlin
- Assistant
In Kotlin, you can represent an N x N matrix as a two-dimensional array. Here's an example:
fun main() { val n = 3 // size of the matrix // initialize the matrix val matrix = Array(n) {IntArray(n) } // populate the matrix with values for (i in 0 until n) { for (j in 0 until n) { matrix[i][j] = i * n + j + 1 } } // print the matrix for (row in matrix) { for (element in row) { print("$element ") } println() } }The above code will create a 3 x 3 matrix and populate it with numbers from 1 to 9. The matrix will be printed as:
1 2 3 4 5 6 7 8 9You can modify the value of
nto create a different sized matrix.