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.
الصفوف والأعمدة
- اعتبر الحلقة الخارجية sebagai صف والحلقة الداخلية sebagai عمود.
- لمجموعة
*، تختار الحلقة الخارجية صفاً، وتطبع الحلقة الداخلية نجوم ذلك الصف. - بعد انتهاء الحلقة الداخلية من صف، اطبع سطر جديد
\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. - عداد داخل الحلقة الداخلية يجمع المطابقات.
- راقب قيم البداية والنهاية — أخطاء الفارق الواحد شائعة هنا.
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.
الآن جرب بنفسك
- استخدم حلقة داخلية
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. · اضغط تشغيل لرؤية المخرجات هنا.
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. · اضغط تشغيل لرؤية المخرجات هنا.
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. · اضغط تشغيل لرؤية المخرجات هنا.