2-D arrays: working with a grid · Array 2-D: bekerja dengan grid
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].
Grid kotak-kotak
- Array 2-D adalah grid: baris dan kolom dari tipe yang sama.
int g[2][3];membuat 2 baris dan 3 kolom — total 6 integer.- Anda mencapai satu sel dengan dua index:
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.
Baris kemudian kolom (baris-major)
- C menyimpan grid baris per baris: semua baris 0, lalu semua baris 1.
- Untuk mengunjungi setiap sel, gunakan loop
foruntuk baris, dan loopfordi dalamnya untuk kolom. g[r][c]adalah sel di barisr, kolomc. Keduanya dimulai dari0.
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.
Mengirim array 2-D
- Saat Anda mengirimkan grid ke fungsi, Anda harus memberikan jumlah kolom:
int g[][3]. - C membutuhkan jumlah kolom untuk menemukan setiap baris dalam memori; jumlah baris bisa dibiarkan kosong.
- Jadi fungsi menerima
int g[][3], int rows— kolomnya tetap di3, barisnya dikirimkan.
#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.
Bekerja satu baris pada satu waktu
- Anda juga bisa bekerja pada satu baris: loop hanya kolom untuk baris tetap
r. - Inilah cara Anda menjumlahkan satu baris, atau menemukan nilai terbesar dalam baris.
- Mencampur keduanya — loop baris luar dengan loop kolom dalam — menangani seluruh grid.
Common mistakes
- Index a grid
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
Kesalahan umum
- Indeksasi grid
grid[row][col]. - Loop luar melintasi baris, loop dalam melintasi kolom.
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.
Sekarang Anda coba
- Untuk tugas fungsi, grid memiliki 3 kolom, sehingga parameternya adalah
int g[][3]. - Jangan tulis
main— checker menyediakannya.
A 2-D array is a grid · Array 2-D adalah sebuah grid
Reach a cell with a[row][col] — row first, then column. · Akses sel dengan a[row][col] — baris terlebih dahulu, kemudian kolom.
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. · Lengkapi int sum_grid(int g[][3], int rows) agar mengembalikan total seluruh sel dalam grid dengan rows baris dan 3 kolom. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
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. · Lengkapi int row_sum(int g[][3], int r) agar mengembalikan total tiga sel pada baris r. Jangan tulis main.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
The 2×3 grid g is given. In main, print each row with the three numbers separated by single spaces, one row per line. · Grid 2×3 g diberikan. Di dalam main, cetak setiap baris dengan tiga angka yang dipisahkan oleh spasi tunggal, satu baris per baris baru.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.