1 / 23

886201 หลักการ โปรแกรม 1

886201 หลักการ โปรแกรม 1. Lecture 10: การวนซ้ำแบบมีโครงสร้างการวนซ้ำซ้อนกัน. Nested Loops. Nested loop มักจะถูกใช้กับข้อ มูลที่มีลักษณะเป็นตาราง มีแถวและคอลัมน์ การประมวลผลข้อมูลแต่ละแถวแต่ละหลักจะเป็นลักษณะ loop ซ้อน loop

Download Presentation

886201 หลักการ โปรแกรม 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 886201 หลักการโปรแกรม 1 Lecture 10: การวนซ้ำแบบมีโครงสร้างการวนซ้ำซ้อนกัน

  2. Nested Loops • Nested loop มักจะถูกใช้กับข้อมูลที่มีลักษณะเป็นตาราง มีแถวและคอลัมน์ • การประมวลผลข้อมูลแต่ละแถวแต่ละหลักจะเป็นลักษณะ loop ซ้อน loop • มี loop นอก และ loop ใน สำหรับวนประมวลผลข้อมูลแต่ละแถวแต่ละหลัก

  3. Nested Loops ลองเขียนโปรแกรมเพื่อแสดงข้อมูลดังตารางต่อไปนี้

  4. Nested Loops • จากโจทย์ข้างต้น จะเห็นว่าข้อมูลมี 4 คอลัมน์ สำหรับแสดงค่า x1 ถึง x4 • ข้อมูลแต่ละแถวในตาราง เป็นค่า x ตั้งแต่ 1 ถึง 10

  5. Nested Loops • ข้อมูลแต่ละแถวในตาราง เป็นค่า x ตั้งแต่ 1 ถึง 10

  6. Nested Loops

  7. The Complete Program for Table of Powers #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { const int NMAX = 4; const double XMAX = 10; // Print table header for (int n = 1; n <= NMAX; n++) { cout << setw(10) << n; } cout << endl; for (int n = 1; n <= NMAX; n++) { cout << setw(10) << "x "; } cout << endl << endl;

  8. // Print table body for (double x = 1; x <= XMAX; x++) { // Print table row for (int n = 1; n <= NMAX; n++) { cout << setw(10) << pow(x, n); } cout << endl; } return 0; } The program run would be:

  9. ตัวอย่าง 1 • เขียนโปรแกรมรับค่า n และแสดงผลลัพธ์ ดังนี้ จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ n for i from 1 to n for j from 1 to n print “A” end for end for for ( inti = 1; i <= n; i++ ) { for ( int j = 1; j <= n; j++ ) { cout << “A”; } cout << endl; }

  10. ตัวอย่าง 2 • เขียนโปรแกรมรับค่า n และแสดงผลลัพธ์ ดังนี้ จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ i for i from 1 to n for j from 1 to i print “A” end for end for for ( inti = 1; i <= n; i++ ) { for ( int j = 1; j <= i; j++ ) { cout << “A”; } cout << endl; }

  11. แบบฝึกหัดที่ 1 • ลองเขียนโปรแกรมเพื่อรับค่า n และแสดงผลลัพธ์ ดังนี้ จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ i

  12. แบบฝึกหัดที่ 2 • ลองเขียนโปรแกรมเพื่อรับค่า n และแสดงผลลัพธ์ ดังนี้ จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ i

  13. แบบฝึกหัดที่ 3 • ลองเขียนโปรแกรมเพื่อรับค่า n และแสดงผลลัพธ์ ดังนี้ จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ n

  14. ตัวอย่าง 3 • เขียนโปรแกรมรับค่า n จากนั้นแสดงผลทั้งหมด n บรรทัด • ถ้าเป็นบรรทัดแรก หรือ บรรทัดสุดท้าย แสดง * จำนวน n ตัว • บรรทัดอื่น ๆ แสดง A จำนวน n ตัว for i from 1 to n for j from 1 to n if i = 1 or i = n print * else print A end if end for end for จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j)ขึ้นอยู่กับ n

  15. ตัวอย่าง 4 • เขียนโปรแกรมรับค่า n จากนั้นแสดงผลทั้งหมด n บรรทัด • ถ้าเป็นบรรทัดคี่ แสดง ^ จำนวน n ตัว • ถ้าเป็นบรรทัดคู่ แสดง v จำนวน n ตัว for i from 1 to n for j from 1 to n if i is odd print ^ else print v end if end for end for n จำนวนแถว (i) ขึ้นอยู่กับ จำนวนคอลัมน์ (j)ขึ้นอยู่กับ n

  16. แบบฝึกหัดที่ 4 • ลองเขียนโปรแกรมเพื่อรับค่า n จากนั้นแสดงผลทั้งหมด n บรรทัด • ถ้าเป็นบรรทัดคี่ แสดง [ ] จำนวนเท่ากับ หมายเลขบรรทัด • ถ้าเป็นบรรทัดคู่ แสดง ( ) จำนวนเท่ากับ หมายเลขบรรทัด จำนวนแถว (i) ขึ้นอยู่กับ n จำนวนคอลัมน์ (j) ขึ้นอยู่กับ i

  17. ตัวอย่าง 5 • เขียนโปรแกรมรับค่า n และแสดงผลลัพธ์ ดังนี้ int j; for ( inti = 1; i <= n; i++ ) { for ( j=1; j<=n-i; j++ ) { cout << “ ”; } for( ; j <= n; j++) { cout << i; } cout << endl; }

  18. ตัวอย่าง 6 • เขียนโปรแกรมรับค่า n และแสดงผลลัพธ์ ดังนี้ for ( inti = 0; i < n; i++ ) { for ( int j=0; j < i; j++ ) { cout << “ ”; } for ( int j = i ; j < n ; j++ ) { cout << n - i; } cout << endl; }

  19. แบบฝึกหัดที่ 5 • ลองเขียนโปรแกรมเพื่อรับค่า n และแสดงผลลัพธ์ ดังนี้

  20. แบบฝึกหัดที่ 6 โปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไร for (i = 1; i <= 4; i++) for (j = 1; j <= i; j++) cout<< "*"; cout << endl;

  21. โปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไรโปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไร for (i = 1; i <= 4; i++) for (j = 1; j <= i; j++) cout<< "*"; cout << endl; * * * * * * * * * *

  22. แบบฝึกหัดที่ 7 โปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไร • for (i = 1; i <= 3; i++) { • for (j = 1; j <= 5; j++) { • if (i + j % 2 == 0) { • cout<< "*"; • } else { • cout<< " "; • } • } • cout<< endl; • }

  23. โปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไรโปรแกรมต่อไปนี้ให้ผลลัพธ์อย่างไร • for (i = 1; i <= 3; i++) { • for (j = 1; j <= 5; j++) { • if (i + j % 2 == 0) { • cout<< "*"; • } else { • cout<< " "; • } • } • cout<< endl; • } The output will be: * * * * * * * *

More Related