Nested loops and patterns · ลูปซ้อนและรูปแบบ
A loop inside a loop
- You can put one loop inside another. The inner loop runs fully for each step of the outer loop.
- This is how you work with rows and columns, or compare every pair of items.
- If the outer loop runs
Rtimes and the inner runsCtimes, the inner body runsR × Ctimes.
ลูปในลูป
- คุณสามารถวางลูปหนึ่ง อยู่ภายใน ลูปอีกวงหนึ่ง ลูปภายในจะรันเต็มรูปแบบสำหรับ ทุก ขั้นตอนของลูปภายนอก
- นี่คือวิธีการทำงานกับแถวและหลัก หรือเปรียบเทียบคู่ของรายการทุกอย่าง
- ถ้าลูปภายนอกรัน
Rครั้งและลูปภายในรันCครั้ง ตัวลูปภายในจะรันR × Cครั้ง
Rows and columns
- Think of the outer loop as the row and the inner loop as the column.
- For a block of
*, the outer loop picks a row; the inner loop prints the stars in that row. - After the inner loop finishes a row, print a newline
\nto start the next row.
แถวและคอลัมน์
- คิดถึงลูปภายนอกว่าเป็น แถว และลูปภายในเป็น หลัก
- สำหรับบล็อกขนาด
*ลูปภายนอกเลือกแถวหนึ่ง ลูปภายในพิมพ์ดาวในแถวนั้น - หลังจากรุ่นลูปภายในพิมพ์แถวเสร็จ ให้พิมพ์บรรทัดใหม่
\nเพื่อเริ่มแถวถัดไป
Building a line, then a block
printf("*")prints one star without moving to a new line.- Print all the stars for one row first, then one
printf("\n")to end the row. - Repeat for every row, and the rows stack into a block.
สร้างเส้น แล้วสร้างบล็อก
printf("*")พิมพ์ดาวหนึ่งดวง โดยไม่ ย้ายไปบรรทัดใหม่- พิมพ์ดาวทั้งหมดสำหรับแถวหนึ่งก่อน แล้วตามด้วย
printf("\n")เดียวเพื่อจบแถว - ทำซ้ำสำหรับทุกแถว แถวจะซ้อนกันเป็นบล็อก
#include <stdio.h>
int main(void) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
printf("*");
}
printf("\n"); // end the row
}
return 0;
}
Counting with nested loops
- Nested loops also count pairs. To count pairs
(i, j)withi < j, start the inner loop ati + 1. - A counter inside the inner loop adds up the matches.
- Watch the start and end values — off-by-one mistakes are common here.
การนับด้วยลูปซ้อน
- ลูปซ้อนยังช่วยนับคู่ได้ ในการนับคู่
(i, j)กับi < jให้เริ่มลูปภายในที่i + 1 - ตัวนับภายในลูปภายในจะรวมจำนวนจับคู่
- สังเกตค่าเริ่มต้นและค่าสิ้นสุด — ข้อผิดพลาด off-by-one พบบ่อยที่นี่
Common mistakes
- The inner loop runs fully for each single turn of the outer loop.
- Check the bounds so a pattern has no extra or missing row.
ข้อผิดพลาดที่พบบ่อย
- ลูปภายในจะรันเต็มรูปแบบสำหรับทุกรอบของลูปภายนอก
- ตรวจสอบขอบเขตเพื่อให้แพทเทิร์นไม่มีแถวเกินหรือขาดหาย
Now you try
- Use an inner
printfwithout\nto build a row, then aprintf("\n")to end it. - For the function task, do not write a
main— the checker provides one.
ลองดูเลย
- ใช้ inner
printfโดยไม่มี\nเพื่อสร้างแถว แล้วตามด้วยprintf("\n")เพื่อปิดท้าย - สำหรับโจทย์ฟังก์ชัน อย่าเขียน
main— เครื่องตรวจสอบจะจัดเตรียมให้
In main, use nested loops to print a 3×3 block of * — three stars on each of three lines. · ใน main, ใช้ลูปซ้อนเพื่อพิมพ์บล็อก 3×3 ของ * — ดาวสามดวงบนแต่ละบรรทัดสามบรรทัด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
In main, print a left-aligned triangle of * that is 4 rows tall: row 1 has one *, row 2 has two, up to row 4 with four. · ใน main, พิมพ์สามเหลี่ยมแนวซ้ายของ * ที่สูง 4 บรรทัด: บรรทัดที่ 1 มี * หนึ่งดวง, บรรทัดที่ 2 มีสองดวง, ไปจนถึงบรรทัดที่ 4 ที่มีสี่ดวง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete int count_pairs(int n) so it returns how many pairs (i, j) satisfy 1 <= i < j <= n. Use nested loops (start the inner loop at i + 1). Do not write a main. · เติมเต็ม int count_pairs(int n) ให้ส่งกลับจำนวนคู่ (i, j) ที่เป็นไปตาม 1 <= i < j <= n. ใช้ลูปซ้อน (เริ่มลูปภายในที่ i + 1). ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่