1-D arrays: store, index, and traverse · 1-D arrays: การเก็บข้อมูล, ดึงใช้ด้วย index, และการ遍历 (traverse)
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.
แถวของกล่อง
- Array (อาเรย์) คือแถวของตัวแปรชนิดเดียวกัน วางเรียงข้างกันในหน่วยความจำ
int a[5];จัดสรรพื้นที่สำหรับintจำนวนห้าตัว它们是同一块区域通过数字访问。- คุณสามารถเติมค่าได้ทันทีที่สร้าง:
int a[] = {10, 20, 30};สร้าง array ขนาดสาม
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]คือรายการสุดท้ายใน array ขนาดnรายการ- การอ่านหรือเขียนนอกเหนือขอบเขต array (เช่น
a[5]ใน array 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.
การ traversing ด้วยลูป
- เพื่อเข้าถึงทุกรายการ ให้รัน index จาก
0ถึงn - 1ด้วยลูปfor - ภายในลูป ใช้
a[i]เพื่ออ่านหรือเปลี่ยนรายการที่ตำแหน่งi - นี่คือวิธีรวมผลนับ หรือค้นหาใน array
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.
Array ไม่รู้ความยาวของตัวเอง
- Array ไม่ เก็บขนาดของมันเอง ฟังก์ชันไม่สามารถถามได้ว่า "คุณยาวเท่าไหร่?"
- ดังนั้นคุณต้องส่งความยาวมาพร้อม array เสมอ:
sum(a, n) - ใช้
const int a[]ใน parameter เพื่อบอกว่าจะ อ่าน archive เท่านั้น ไม่แก้ไข
#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 ไม่ ตรวจสอบขอบเขต array:
a[n]ใน array ขนาดnเป็น undefined behavior - Index ที่ถูกต้องวิ่งจาก
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.
ลองดูเลย
- ส่งทั้ง array และความยาว
nมา และลูป index จาก0ถึงn - 1 - สำหรับโจทย์เกี่ยวกับฟังก์ชัน อย่าเขียน
main— ตัวตรวจสอบจะจัดเตรียมให้เอง
Arrays are indexed · Array ถูกอ้างอิงด้วยดัชนี
An array stores values in order; reach one with a[i]. · Array เก็บค่าเรียงลำดับ; เข้าถึงแต่ละตัวด้วย 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. · array a และความยาว n được cung cấp. Trong main, cộng tất cả các phần tử n và in tổng ra một dòng riêng.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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 รายการใน array (0 เมื่อ n เป็น 0). ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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) ให้คูณสองทุกรายการของ array, แบบใน-place (ไม่มีค่าส่งกลับ). ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่