第 3 课:循环、函数与数组
非 UNSW 官方材料。本文基于官方 functions、style、1D array、2D array 和 strings 相关 lecture PDF 做中文转述;所有练习模板为原创。[S011][S012][S013][S014][S015]
今天只做:写一个
for循环,能安全访问array[i]。 下一步:主控台 · 上一课 · 下一课:debugging、字符串与模拟考试 别乱跳:数组题先找下标,不先背一整章。
循环:重复做同一件事
Lecture 3/4 把 loop 放进 control flow,Lecture 6 进入 arrays;数组题其实就是“用循环访问很多格子”,所以你先把 for 读顺。[S009][S010][S012]
这段的读法是:i 从 0 开始;只要 i < size 就继续;每轮后 i++。这个解释是本站原创,但结构来自 C 的基础控制流和数组访问。[S006][S012]
函数题:先看题目让不让输出
Lecture 5 把 functions 和 style 放在同一课,Style Guide 也强调代码布局、函数和限制;Exam 练习里常见坑是题目要 return,你却写了 printf。[S011][S005]
| 题目说法 | 你该怎么做 |
|---|---|
| should return | 用 return 值; |
| should not print | 不要写 printf |
| should not scan | 不要写 scanf |
1D array 模板:找位置,再处理附近
Lecture 6 开始看 arrays,并把函数和数组放在一起复习;下面的模板不是官方题目,是用于练“找目标下标”的原创模式。[S012]
2D array 模板:外层行,内层列
Lecture 7/8/9 都围绕 strings、array of arrays 和 2D arrays 继续展开;二维数组先按“行 row + 列 col”读,不要一上来想复杂图形。[S013][S014][S015]
最容易错的是把 sum 放到外层循环外面;每一行都要重新计数,这个模式对应 2D array 题的基本循环结构。[S014][S015]
本课过关标准
- 我能写
for循环访问array[i]。 - 我会先看函数题能不能
printf/scanf。 - 我知道 2D array 常写
array[row][col]。 - 我知道每一行的临时计数要在外层循环里重置。
引用
- [S005] COMP1511 Style Guide: https://cgi.cse.unsw.edu.au/~cs1511/26T1/resources/style_guide.html
- [S006] COMP1511 C reference sheet PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/resources/c-reference-sheet.pdf
- [S009] Lecture 3 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_2/COMP1511_26T1_Lecture03.pdf
- [S010] Lecture 4 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_2/COMP1511_26T1_Lecture04.pdf
- [S011] Lecture 5 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_3/COMP1511_26T1_Lecture05.pdf
- [S012] Lecture 6 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_3/COMP1511_26T1_Lecture06.pdf
- [S013] Lecture 7 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_4/COMP1511_26T1_Lecture07.pdf
- [S014] Lecture 8 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_4/COMP1511_26T1_Lecture08.pdf
- [S015] Lecture 9 PDF: https://cgi.cse.unsw.edu.au/~cs1511/26T1/slides/week_5/COMP1511_26T1_Lecture09.pdf