1-D arrays: store, index, and traverse · מערכים 1D: אחסון, אינדקס וטיול
A row of boxes
- An array is a row of variables of the same type, kept side by side in memory.
int a[5];makes room for fiveints. They are one block, reached by number.- You can fill them when you create them:
int a[] = {10, 20, 30};makes an array of three.
שורה של קופסאות
- מערך הוא שורה של משתנים מאותו סוג, שמורים צמודים זה לזה בזיכרון.
int a[5];יוצר מקום לחמישהints. הם בלוק אחד, הניתן לגישה לפי מספר.- ניתן למלא אותם בעת יצירתם:
int a[] = {10, 20, 30};יוצר מערך של שלושה.
Indexing from 0
- Each item has an index (a position). The first index is
0, not1. a[0]is the first item,a[1]the second, anda[n - 1]is the last in an array ofnitems.- Reading or writing outside the array (like
a[5]in a 5-item array) is a serious bug.
אינדקסינג מ-0
- לכל פריט יש אינדקס (מיקום). האינדקס הראשון הוא
0, לא1. a[0]הוא הפריט הראשון,a[1]השני, ו-a[n - 1]האחרון במערך שלnפריטים.- קריאה או כתיבה מחוץ לגבולות המערך (כמו
a[5]במערך בעל 5 פריטים) היא שגיאה קריטית.
Traversing with a loop
- To visit every item, run an index from
0up ton - 1with aforloop. - Inside the loop, use
a[i]to read or change the item at positioni. - This is how you sum, count, or search an array.
עיבור עם לולאה
- כדי לבקר בכל פריט, רץ אינדקס מ-
0עדn - 1בתוך לולאתfor. - בתוך הלולאה, השתמש ב-
a[i]לקריאה או שינוי של הפריט במיקוםi. - כך סוכמים, סופרים או מחפשים במעריך.
Arrays don't know their own length
- An array does not carry its own size. A function cannot ask "how long are you?".
- So you always pass the length next to the array:
sum(a, n). - Use
const int a[]in a parameter to promise you will only read the array, not change it.
מערכים אינם יודעים את אורכם
- למערך אין גודל משלו. פונקציה לא יכולה לשאול "מה האורך שלך?".
- לכן תמיד מעבירים את האורך לצד המעריך:
sum(a, n). - השתמש ב-
const int a[]בפארמטר כדי להבטיח שתקרא רק במעריך ולא תשנה אותו.
#include <stdio.h>
int main(void) {
int a[] = {10, 20, 30, 40};
int n = 4, total = 0;
for (int i = 0; i < n; i++) {
total += a[i];
}
printf("%d\n", total); // 100
return 0;
}
Common mistakes
- C does NOT check array bounds:
a[n]in a size-narray is undefined. - Valid indexes run
0ton - 1.
טעויות נפוצות
- C אינה בודקת גבולות מערכים: גישה ל-
a[n]במעריך בגודלnהיא התנהגות לא מוגדרת. - אינדקסים תקפים נעים מ-
0עדn - 1.
Now you try
- Pass both the array and its length
n, and loop the index from0ton - 1. - For the function tasks, do not write a
main— the checker provides one.
כעת תנסו בעצמכם
- העבר גם את המעריך וגם את האורך שלו
n, ולולאת האינדקס מ-0עדn - 1. - לעבודות פונקציה, אל תכתב
main— הבוחן מספק אותו.
Arrays are indexed · מערכות ממוספרות באינדקסים
An array stores values in order; reach one with a[i]. · מערך אוסף ערכים בסדר; ניתן לגשת לאחד באמצעות a[i].
The array a and its length n are given. In main, add up all n items and print the total on its own line. · נתונה המערך a ואורכה n. בmain, סכום את כל פריטי הn והדפס את הסך הכול בשורה נפרדת.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete int sum(const int a[], int n) so it returns the total of the array's n items (0 when n is 0). Do not write a main. · השלם את int sum(const int a[], int n) כך שתחזיר את הסך של פריטי המערך הn (0 כאשר n הוא 0). אל תכתוב פקודת main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete void double_all(int a[], int n) so it doubles every item of the array, in place (no return value). Do not write a main. · השלם את void double_all(int a[], int n) כדי להכפיל פי שניים כל פריט במערך, במקור (ללא ערך החזרה). אל תכתוב פקודת main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.