使用多维数组的Go语言程序来相加两个矩阵
在本教程中,我们将编写一个Go语言程序来相加两个矩阵。一维数组和多维数组的区别在于,前者保存一个属性,而后者在索引处保存另一个数组。此外,多维数组的每个元素都将具有相同的数据类型。
使用循环相加两个矩阵
现在让我们来看一个使用循环相加两个矩阵的Go语言程序。
上述程序的算法
步骤1 - 导入fmt包。
步骤2 - 现在我们需要开始main()函数。
步骤3 - 然后我们创建两个名为matrixA和matrixB的矩阵,并在其中存储值。
步骤4 - 使用fmt.Println()函数在屏幕上打印数组。
步骤5 - 初始化一个新的int类型矩阵来保存结果。
步骤6 - 要相加两个矩阵,使用for循环迭代这两个矩阵
步骤7 - 使用第一个for循环获取矩阵的行,而第二个for循环则给出矩阵的列。
步骤8 - 循环结束后,新矩阵包含两个矩阵的和。
步骤9 - 使用for循环和fmt.Println()函数打印新矩阵的元素。
示例
package main
import (
"fmt"
)
// calling the main() function.
func main() {
var i, j int
var matrixC [3][3]int
matrixA := [3][3]int{
{0, 1},
{4, 5},
{8, 9},
}
matrixB := [3][3]int{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18},
}
fmt.Println("The first matrix is:")
for i = 0; i < 3; i++ {
for j = 0; j < 2; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
// printing the second matrix on the screen
fmt.Println("The second matrix is:")
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
fmt.Print(matrixB[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
fmt.Println("The results of addition of matrix A & B: ")
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
matrixC[i][j] = matrixA[i][j] + matrixB[i][j]
}
}
for i = 0; i < 3; i++ {
for j = 0; j < 3; j++ {
fmt.Print(matrixC[i][j], "\t")
}
fmt.Println()
}
}
输出
The first matrix is: 0 1 4 5 8 9 The second matrix is: 10 11 12 13 14 15 16 17 18 The results of addition of matrix A & B: 1012 12 17 19 15 24 26 18
使用外部函数相加两个矩阵
在这个例子中,我们将使用用户自定义函数来相加两个矩阵。
上述程序的算法
步骤1 - 导入fmt包。
步骤2 - 创建一个函数来相加两个矩阵。
步骤3 - 在此函数中,使用make()函数创建一个矩阵切片,并使用range函数迭代矩阵以找到总和
步骤4 - 启动主函数。
步骤5 - 初始化两个矩阵并向其中存储元素,在屏幕上打印矩阵。
步骤6 - 通过将两个矩阵作为参数传递给函数来调用AddMatrices()函数。
步骤7 - 存储获得的结果并在屏幕上打印。
语法
func make ([] type, size, capacity)
Go语言中的make函数用于创建数组/映射,它接受要创建的变量类型、大小和容量作为参数。
func append(slice, element_1, element_2…, element_N) []T
append函数用于向数组切片添加值。它接受多个参数。第一个参数是要向其添加值的数组,后跟要添加的值。然后,该函数返回包含所有值的最终数组切片。
示例
package main
import (
"fmt"
)
// creating a function to add matrices
func AddMatrix(matrix1 [3][3]int, matrix2 [3][3]int) [][]int {
result := make([][]int, len(matrix1))
for i, a := range matrix1 {
for j, _ := range a {
result[i] = append(result[i], matrix1[i][j]+matrix2[i][j])
}
}
return result
}
func main() {
matrixA := [3][3]int{
{0, 1, 2},
{4, 5, 6},
{8, 9, 10},
}
matrixB := [3][3]int{
{10, 11},
{13, 14},
{16, 17},
}
fmt.Println("The first matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(matrixA[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
// printing the second matrix on the screen
fmt.Println("The second matrix is:")
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
fmt.Print(matrixB[i][j], "\t")
}
fmt.Println()
}
fmt.Println()
// calling the AddMatrix() function
result := AddMatrix(matrixA, matrixB)
fmt.Println("The results of addition of matrix A & B: ")
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(result[i][j], "\t")
}
fmt.Println()
}
}
输出
The first matrix is: 0 1 2 4 5 6 8 9 10 The second matrix is: 10 11 13 14 16 17 The results of addition of matrix A & B: 10 12 2 17 19 6 24 26 10
结论
我们已经成功地编译并执行了一个Go语言程序来相加两个矩阵以及示例。在第一个示例中,我们在main()函数中实现了逻辑,而在第二个示例中,我们使用了外部函数来实现上述逻辑。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP