Go 程序执行标量矩阵乘法

编写一个 Go 程序来执行标量矩阵乘法,这意味着将每个矩阵元素与给定的数字相乘。在嵌套的 for 循环中,我们将每个矩阵元素与用户给定的数字相乘。

package main

import "fmt"

func main() {
    var i, j, rows, columns, num int

    var orgMat [10][10]int
    var scalarMultiMat [10][10]int

    fmt.Print("Enter the Matrix rows and Columns = ")
    fmt.Scan(&rows, &columns)

    fmt.Println("Enter the Matrix Items to find the Columns Sum = ")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            fmt.Scan(&orgMat[i][j])
        }
    }
    fmt.Print("Enter the Scalar Matrix Multiplication Value = ")
    fmt.Scan(&num)

    fmt.Println("*** The Result of Scalar Matrix Multiplication ***")
    for i = 0; i < rows; i++ {
        for j = 0; j < columns; j++ {
            scalarMultiMat[i][j] = num * orgMat[i][j]
            fmt.Print(scalarMultiMat[i][j], " ")
        }
        fmt.Println()
    }
}
Enter the Matrix rows and Columns = 2 2
Enter the Matrix Items to find the Columns Sum = 
10 20
30 55
Enter the Scalar Matrix Multiplication Value = 5   
*** The Result of Scalar Matrix Multiplication ***
50 100 
150 275 

使用 For 循环范围执行标量矩阵乘法的 Golang 程序。

package main

import "fmt"

func main() {

    var num int
    var orgMat [3][3]int
    var scalarMultiMat [3][3]int

    fmt.Println("Enter the Matrix Items to find the Columns Sum = ")
    for i, rows := range orgMat {
        for j := range rows {
            fmt.Scan(&orgMat[i][j])
        }
    }
    fmt.Print("Enter the Scalar Matrix Multiplication Value = ")
    fmt.Scan(&num)

    fmt.Println("*** The Result of Scalar Matrix Multiplication ***")
    for i, rows := range orgMat {
        for j := range rows {
            scalarMultiMat[i][j] = num * orgMat[i][j]
            fmt.Print(scalarMultiMat[i][j], " ")
        }
        fmt.Println()
    }
}
Golang Program to Perform Scalar Matrix Multiplication