Go 語言程式新增兩個矩陣


在本教程中,我們將編寫一個 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"
)
func main() {
   var i, j int
   var matrixC [3][3]int
   matrixA := [3][3]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   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 < 3; j++ {
         fmt.Print(matrixA[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
   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	24	5	6	
8	9	10	

The second matrix is:
10	11	12	
13	14	15	
16	17	18	

The results of addition of matrix A & B: 
10	12	14	
17	19	21	
24	26	28

使用外部函式的 Go 語言程式新增兩個矩陣

在此示例中,我們將使用使用者定義函式來新增兩個矩陣。

語法

func make ([] type, size, capacity)

Go 語言中的 make 函式用於建立陣列/對映,它接受要建立的變數的型別、其大小和容量作為引數

演算法

步驟 1 − 匯入 fmt 包。

步驟 2 − 建立一個函式來新增兩個矩陣。

步驟 3 − 在此函式中,使用 make() 函式建立矩陣的切片,並使用 range 函式迭代矩陣以找到總和

步驟 4 − 開始 main 函式。

步驟 5 − 初始化兩個矩陣並在其中儲存元素,並在螢幕上列印矩陣。

步驟 6 − 透過將兩個矩陣作為引數傳遞給函式來呼叫 AddMatrices() 函式。

步驟 7 − 儲存獲得的結果並在螢幕上列印它。

示例

package main
import (
   "fmt"
)
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, 12},
      {13, 14, 15},
      {16, 17, 18},
   }
   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()
   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()
   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	12	
13	14	15	
16	17	18	

The results of addition of matrix A & B: 
10	12	14	
17	19	21	
24	26	28	

結論

我們已成功編譯並執行了一個 Go 語言程式,用於新增兩個矩陣以及示例。

更新於: 2022-12-28

779 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.