How to convert a specific row of a decimal matrix to a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func convertRowToString(matrix [][]int, row int) string {
    var b strings.Builder

    cols := len(matrix[row]) // FIX: determine number of columns

    for j := 0; j < cols; j++ {
        fmt.Fprintf(&b, "%d ", matrix[row][j])
    }

    return b.String()
}

func main() {
    matrix := [][]int{
        {4, 7, 9, 18, 29, 0},
        {1, 9, 18, 99, 4, 3},
        {9, 17, 89, 2, 7, 5},
        {19, 49, 6, 1, 9, 8},
        {29, 4, 7, 9, 18, 6},
    }

    row := 2
    str := convertRowToString(matrix, row)

    fmt.Println(str)
}

 
 
/*
run:
 
9 17 89 2 7 5 
 
*/

 



answered Jan 9 by avibootz
edited Jan 9 by avibootz

Related questions

...