使用多維陣列的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