2-D arrays: working with a grid · 2-D arrays: การทำงานกับตาราง
A grid of boxes
- A 2-D array is a grid: rows and columns of the same type.
int g[2][3];makes 2 rows and 3 columns — 6 ints in all.- You reach one cell with two indexes:
g[row][col].
ตารางกล่อง
- 2-D array คือตาราง: แถวและคอลัมน์ที่มีชนิดเดียวกัน
int g[2][3];สร้างแถว 2 และคอลัมน์ 3 — รวมทั้งหมด 6 ints- เข้าถึงเซลล์หนึ่งด้วยสองดัชนี:
g[row][col]
Rows then columns (row-major)
- C stores a grid row by row: all of row 0, then all of row 1.
- To visit every cell, use a
forloop for the rows, and aforloop inside it for the columns. g[r][c]is the cell in rowr, columnc. Both start at0.
แถวก่อน คอลัมน์ (row-major)
- C เก็บตาราง ตามแถว: แถว 0 ทั้งหมด แล้วแถว 1 ทั้งหมด
- เพื่อเข้าถึงทุกเซลล์ ให้ใช้
forloop สำหรับแถว และมีforloop ข้างในสำหรับคอลัมน์ g[r][c]คือเซลล์ในแถวrคอลัมน์c. ทั้งสองเริ่มต้นที่0
Passing a 2-D array
- When you pass a grid to a function, you must give the column count:
int g[][3]. - C needs the number of columns to find each row in memory; the row count can stay blank.
- So a function takes
int g[][3], int rows— the columns are fixed at3, the rows are passed in.
การส่ง 2-D array
- เมื่อส่งตารางไปยังฟังก์ชัน คุณต้องระบุ จำนวนคอลัมน์:
int g[][3] - C ต้องการจำนวนคอลัมน์เพื่อหาแต่ละแถวในหน่วยความจำ; จำนวนแถวสามารถเว้นว่างได้
- ดังนั้นฟังก์ชันจึงรับ
int g[][3], int rows— คอลัมน์ถูกกำหนดไว้ที่3, แถวจะถูกส่งเข้ามา
#include <stdio.h>
int main(void) {
int g[2][3] = {{1, 2, 3}, {4, 5, 6}};
int total = 0;
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
total += g[r][c];
}
}
printf("%d\n", total); // 21
return 0;
}
Working one row at a time
- You can also work on a single row: loop only the columns for a fixed row
r. - This is how you sum one row, or find the biggest value in a row.
- Mixing the two — an outer row loop with an inner column loop — handles the whole grid.
ทำงานทีละแถว
- คุณสามารถทำงานกับแถวเดียวได้: วนลูปเฉพาะคอลัมน์สำหรับแถวคงที่
r - นี่คือวิธีบวกผลรวมแถวเดียว หรือหาค่าที่ใหญ่ที่สุดในแถว
- การผสมทั้งสองอย่าง — ลูปแถวภายนอกพร้อมลูปคอลัมน์ภายใน — จะจัดการทั้งตาราง
Common mistakes
- Index a grid
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
ข้อผิดพลาดที่พบบ่อย
- Access เกริด
grid[row][col] - ลูปภายนอกเดินผ่านแถว ลูปภายในเดินผ่านคอลัมน์
Now you try
- For the function tasks, the grid has 3 columns, so the parameter is
int g[][3]. - Do not write a
main— the checker provides one.
ลองดูเลย
- สำหรับโจทย์ฟังก์ชัน ตารางมี 3 คอลัมน์ ดังนั้นพารามิเตอร์คือ
int g[][3] - อย่า เขียน
main— ตัวตรวจสอบจะจัดเตรียมให้
A 2-D array is a grid · 2-D array คือตาราง
Reach a cell with a[row][col] — row first, then column. · เข้าถึงเซลล์ด้วย a[row][col] — แถวก่อน แล้วตามด้วยหลัก
Complete int sum_grid(int g[][3], int rows) so it returns the total of every cell in a grid with rows rows and 3 columns. Do not write a main. · เติม int sum_grid(int g[][3], int rows) ส่งผลรวมของทุกเซลล์ในตารางที่มี rows แถวและ 3 คอลัมน์ ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int row_sum(int g[][3], int r) so it returns the total of the three cells in row r. Do not write a main. · เติม int row_sum(int g[][3], int r) ส่งผลรวมของสามเซลล์ในแถว r ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The 2×3 grid g is given. In main, print each row with the three numbers separated by single spaces, one row per line. · ตาราง 2×3 g ให้มา ใน main พิมพ์แต่ละแถวพร้อมตัวเลข 3 ตัวคั่นด้วยช่องว่างหนึ่งตัว แยกบรรทัดต่อบรรทัด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่