Go 1.21 में द्वि-आयामी सारणी: एक विस्तृत मार्गदर्शिका
Two-Dimensional Arrays in Go
What is a Two-Dimensional Array?
Imagine a chessboard. It has rows and columns. Each cell on the board is a cell that has its own row number and column number. In programming, such a "table" is called a two-dimensional array.
A two-dimensional array is an array where each element is itself an array. Simply put, it's a table where data is stored in rows and columns.
Why is this needed? For example, to store:
- A cinema seating chart: rows are rows, columns are seats.
- A map of an island: rows are vertical coordinates, columns are horizontal coordinates.
- A warehouse inventory table: rows are shelves, columns are product types.
How to Declare a Two-Dimensional Array in Go?
The syntax is very similar to a regular array, except we specify two sizes: first the number of rows, then the number of columns.
// Declaring a 3x3 array (3 rows, 3 columns)
var cinema [3][3]int
// Declaring and immediately initializing
matrix := [2][3]int{
{1, 2, 3},
{4, 5, 6},
}
To access an element, you use two indices: array_name[row_number][column_number]. Important: indexing starts at 0!
package main
import "fmt"
func main() {
// Create a 2x2 array
var data [2][2]int
// Fill it manually
data[0][0] = 10
data[0][1] = 20
data[1][0] = 30
data[1][1] = 40
// Print the element from the first row, second column
fmt.Println("Element [0][1]:", data[0][1]) // Output: 20
}
How to Work with a Two-Dimensional Array?
Most often, you need to iterate through all elements. For this, nested loops are used — one loop iterates over rows, and another loop inside iterates over columns.
package main
import "fmt"
func main() {
// Create a 3x4 array and fill it with zeros
var grid [3][4]int
// Fill with values: row number + column number
for row := 0; row < 3; row++ {
for col := 0; col < 4; col++ {
grid[row][col] = row + col
}
}
// Print the table on the screen
for row := 0; row < 3; row++ {
for col := 0; col < 4; col++ {
fmt.Print(grid[row][col], " ")
}
fmt.Println() // Move to a new line
}
}
Program output:
0 1 2 3
1 2 3 4
2 3 4 5
How to Fill an Array from the Keyboard?
Often in tasks, you need to input data from the user. We use fmt.Scan inside nested loops.
package main
import "fmt"
func main() {
var rows, cols int
fmt.Print("Enter the number of rows and columns: ")
fmt.Scan(&rows, &cols)
// Create an array of the required size
arr := make([][]int, rows)
for i := 0; i < rows; i++ {
arr[i] = make([]int, cols)
}
// Fill from the keyboard
fmt.Println("Enter the elements row by row:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Scan(&arr[i][j])
}
}
// Print the table
fmt.Println("Your table:")
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(arr[i][j], "\t")
}
fmt.Println()
}
}
Analysis of Typical Problems
Problem "Cinema Hall"
You need to create a seating chart. Let free seats be 0, occupied seats be 1. We get the dimensions of the hall and fill it.
package main
import "fmt"
func main() {
var rows, cols int
fmt.Scan(&rows, &cols)
// Create the hall layout
hall := make([][]int, rows)
for i := 0; i < rows; i++ {
hall[i] = make([]int, cols)
// Fill the row: if i % 2 == 0, all seats are free, otherwise occupied
for j := 0; j < cols; j++ {
if i%2 == 0 {
hall[i][j] = 0 // Free
} else {
hall[i][j] = 1 // Occupied
}
}
}
// Print the layout
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
fmt.Print(hall[i][j], " ")
}
fmt.Println()
}
}
Problem "Post Popularity"
We have bloggers and their posts. We need to find which blogger has the most likes.
package main
import "fmt"
func main() {
var bloggers, posts int
fmt.Print("Number of bloggers and posts: ")
fmt.Scan(&bloggers, &posts)
// Create the likes table
likes := make([][]int, bloggers)
for i := 0; i < bloggers; i++ {
likes[i] = make([]int, posts)
fmt.Printf("Enter likes for blogger %d: ", i+1)
for j := 0; j < posts; j++ {
fmt.Scan(&likes[i][j])
}
}
// Find the blogger with the maximum sum of likes
maxSum := 0
maxBlogger := 0
for i := 0; i < bloggers; i++ {
sum := 0
for j := 0; j < posts; j++ {
sum += likes[i][j]
}
if sum > maxSum {
maxSum = sum
maxBlogger = i + 1
}
}
fmt.Printf("Most popular blogger: %d (total likes: %d)\n", maxBlogger, maxSum)
}
Common Beginner Mistakes
- Confusing indices: remember that the first index is the row, the second is the column.
arr[row][column]. - Going out of array bounds: if the array is 3x3, the indices can only be 0, 1, 2. Accessing index 3 will cause an error.
- Forgetting about nested loops: to iterate through all elements, you need two loops. One loop cannot iterate through all cells.
- Incorrect creation using make: first you need to create an array for the rows (
make([][]int, rows)), and then for each row create