Golang Program to read and print two-dimensional array


What is a 2D array?

A two-dimensional array is a collection of data that are arranged in rows and columns. In go we can use the for loops to iterate and print elements of the 2d arrays.

Here is an example of the same below −

0  1  2
4  5  6
8  9  10

Method 1: Using For Loops

In this method, we will use for loops in golang to iterate over the arrays and catch each element then we will print that element unless the whole array is iterated upon.

Algorithm

  • Step 1 − Import the fmt package.

  • Step 2 − Now we need to start the main() function.

  • Step 3 − Then create a two-dimensional matrix naming matrix and store data to it.

  • Step 4 − Now, use two for loops to iterate over the array elements. Using the first for loop will get the row of the multi-dimensional array while the second for loop gives us the column of the two-dimensional array.

  • Step 5 − Once a particular matrix element is obtained, print that element on the screen and move on to the next element until the loop gets completed.

Example

In this example, we will use for loops to read and print the elements of two-dimensional arrays by using for loops.

package main
import "fmt"
func main() {

   // initializing variables
   var array [][]int
   var row int
   var col int
   array = make([][]int, row)
   for i := range array {
      array[i] = make([]int, col)
   }
   
   // assigning data to matrix
   array = [][]int{
      {0, 1, 2},
      {4, 5, 6},
      {8, 9, 10},
   }
   
   // printing the matrix elements
   fmt.Println("The given matrix is:")
   for i := 0; i < 3; i++ {
      for j := 0; j < 3; j++ {
         fmt.Print(array[i][j], "\t")
      }
      fmt.Println()
   }
   fmt.Println()
}

Output

The given matrix is:
0	1	2	
4	5	6	
8	9	10	

Method 2: Using Internal Function

In this method, we are going to use the range function in the first example and array slicing in the second example.

Algorithm

  • Step 1 − Import the fmt package.

  • Step 2 − Now we need to start the main() function.

  • Step 3 − Then we are creating a matrix naming matrix and assign data to it.

  • Step 4 − Now use the range function to iterate over the matrix elements and print each element of the matrix on the screen.

Example 1

In this example, we will use the range function of go Programming to get the elements of two-dimensional arrays with the combination of range function and for loop.

package main
import "fmt"
func main() {

   // initializing variables
   var array [][]int
   var row int
   var col int
   array = make([][]int, row)
   for i := range array {
      array[i] = make([]int, col)
   }
   
   // assigning data to matrix
   array = [][]int{
      {10, 13, 21},
      {47, 54, 63},
      {82, 91, 0},
   }
   
   // printing the matrix elements
   fmt.Println("The required array is:")
   for _, row := range array {
      for _, val := range row {
         fmt.Print(val, "\t")
      }
      fmt.Println()
   }
}

Output

The required array is:
10	13	21	
47	54	63	
82	91	0

Example 2

In this program, we will print a 2-D array by using the concept of array slicing property of go language.

package main
import "fmt"
func main() {

   // initializing variables
   var array [][]int
   var row int
   var col int
   array = make([][]int, row)
   for i := range array {
      array[i] = make([]int, col)
   }

   // assigning data to matrix
   array = [][]int{
      {10, 13, 21},
      {47, 54, 63},
      {82, 91, 0},
   }

   // printing the matrix elements
   fmt.Println("The required array is:")
   for _, j := range array {
      fmt.Print(j, "\t")
      fmt.Println()
   }
   fmt.Println()
}

Output

The required array is:
[10 13 21]	
[47 54 63]	
[82 91 0]

Method 3: Using Recursion

In this method, we will use the concept of recursion to read and print the elements of two-dimensional arrays on the screen.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Now, create a recursive function called printMatrix() that accepts the multidimensional array as argument to it along with the current row index that should be printed.

  • Step 3 − Now, if the current row variable is equal to the length of the multidimensional array then the program will end.

  • Step 4 − A for loop is used to iterate over the array and print the current element. Once the current row is printed the function calls itself again by incrementing the row variable.

  • Step 5 − In this way all the three rows of the array are printed on the screen.

  • Step 6 − Now, start the main() function. Inside the main() initialize a 2-D array and assign value to it.

  • Step 7 − Now, call the printMatrix() function by passing the matrix along with the current row position as argument to the function.

Example

The following code uses recursion method to to read and print two-dimensional array

package main
import "fmt"
func printMatrix(matrix [][]int, row int) {
   if row == len(matrix) {
      return
   }
   for _, element := range matrix[row] {
      fmt.Print(element, "\t")
   }
   fmt.Println()
   printMatrix(matrix, row+1)
}
func main() {

   // initializing variables
   var array [][]int
   var row int
   var col int
   array = make([][]int, row)
   for i := range array {
      array[i] = make([]int, col)
   }
   
   // assigning data to matrix
   array = [][]int{
      {12, 13, 21},
      {47, 54, 23},
      {28, 19, 61},
   }
   fmt.Println("The given matrix is:")
   printMatrix(array, 0)
}

Output

The given matrix is:
12	13	21	
47	54	23	
28	19	61

Conclusion

We have successfully compiled and executed a go language program to read and print the elements of multi-dimensional arrays. In the first example we have only used the for loop while in the second example we have used the combination of range function and for loops. In the third example we have used the concept of recursion where we call the function from itself until the execution is completed.

Updated on: 16-Feb-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements