Transposing a matrix

Report a typo

Suppose you have a Go program that takes as an input various int numbers and generates a 3x3 matrix A using the hidden generateMatrix() function.

Your task is to transpose the A matrix. To do this, you need to declare and initialize a new 3x3 matrix variable transposedA with the transposed version of matrix A and then print each row of the transposedA matrix.

The transpose of a matrix is an operation that flips a matrix over its diagonal; it switches the row and column indices of the matrix. Below is an example of declaring and initializing the transposed version of the 2x2 matrix B:
var B = [2][2]int{
    {1, 2},
    {3, 4},
}

var transposedB = [2][2]int{
    {B[0][0], B[1][0]},
    {B[0][1], B[1][1]},
}

fmt.Println(transposedB[0])
fmt.Println(transposedB[1])

// Output:
// [1 3]
// [2 4]

Sample Input 1:

1 2 3
4 5 6
7 8 9

Sample Output 1:

[1 4 7]
[2 5 8]
[3 6 9]

Sample Input 2:

9 8 7
6 5 4
3 2 1

Sample Output 2:

[9 6 3]
[8 5 2]
[7 4 1]
Write a program in Go
package main

import "fmt"

// nolint: revive // DO NOT delete this comment!
func main() {
// DO NOT delete this line! It takes the input numbers and generates the 3x3 matrix `A`.
var A = generateMatrix()

// Declare and initialize the transposed version of the 3x3 matrix `A` below:
var transposedA = ?

// Print each row of the 3x3 `transposedA` matrix below:
fmt.Println(transposedA[0])
fmt.Println(?)
fmt.Println(?)
}
___

Create a free account to access the full topic