编写一个 Go 程序来查找矩阵每行的总和。它使用嵌套的 for 循环来查找给定矩阵中每一行的总和。
package main
import "fmt"
func main() {
var i, j, rows, columns int
var rowSumMat [10][10]int
fmt.Print("Enter the Matrix rows and Columns = ")
fmt.Scan(&rows, &columns)
fmt.Println("Enter the Matrix Items to find the Row Sum = ")
for i = 0; i < rows; i++ {
for j = 0; j < columns; j++ {
fmt.Scan(&rowSumMat[i][j])
}
}
for i = 0; i < rows; i++ {
sum := 0
for j = 0; j < columns; j++ {
sum = sum + rowSumMat[i][j]
}
fmt.Println("The Sum of Matrix ", i+1, " Row Elements = ", sum)
}
}
Enter the Matrix rows and Columns =
2 2
Enter the Matrix Items to find the Row Sum =
10 20
30 80
The Sum of Matrix 1 Row Elements = 30
The Sum of Matrix 2 Row Elements = 110
Golang 程序使用 For 循环范围查找矩阵每行的总和
package main
import "fmt"
func main() {
var rowSumMat [3][3]int
fmt.Println("Enter the Matrix Items to find the Row Sum = ")
for i, rows := range rowSumMat {
for j := range rows {
fmt.Scan(&rowSumMat[i][j])
}
}
for i, rows := range rowSumMat {
sum := 0
for j := range rows {
sum = sum + rowSumMat[i][j]
}
fmt.Println("The Sum of Matrix ", i+1, " Row Elements = ", sum)
}
}
