2-D arrays: working with a grid · מערכים בעלי ממד 2-D: עבודה עם רשת
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 הוא רשת: שורות ועמודות מאותו סוג.
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.
שורות ולאחר מכן עמודות (סידור בשורה)
- C מאחסנת רשת שורה אחר שורה: את כל שורה 0, ולאחר מכן את כל שורה 1.
- כדי לבקר בכל תא, השתמש בלולאת
forעבור השורות, ולולאה פנימיתforעבור העמודות. 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
- כאשר מעבירים רשת לפונקציה, יש לספק את מספר העמודות:
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.
טעויות נפוצות
- מיינדקס רשת
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— הבוקר מספק אותו already.
A 2-D array is a grid · מערך בעל ממד 2-D הוא רשת
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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, הדפס כל שורה עם שלושת המספרים המופרדים במרווח יחיד, שורה אחת בכל שורה.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.