Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i<5; i++) { for (int j=0; j<5; j++)

Similar presentations


Presentation on theme: "Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i<5; i++) { for (int j=0; j<5; j++)"— Presentation transcript:

1 Chapter 05 (Part V) Control Statements: Part II

2 Nested For-Structures Consider the following codes: for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { cout << ‘*’; } cout << endl; } –Question: what shape does the program print? ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4

3 Print a Right Triangle Print a right triangle with height of 5 as follows: ﹡﹡ ﹡﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡﹡﹡ ﹡﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 1234512345 = i + 1

4 Print a Right Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<i+1; j++) { cout << ‘*’; } cout << endl; }

5 Print an Inverted Triangle Print a inverted triangle with height of 5 as follows: ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡﹡ ﹡﹡﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡﹡ ﹡﹡ i=0 i=1 i=2 i=3 i=4 5432154321 = 5 - i

6 Print an Inverted Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<5-i; j++) { cout << ‘*’; } cout << endl; }

7 Print a Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 ﹡ 5 5 5 5 5

8 Print a Parallelogram Implementation for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { cout << ‘*’; } cout << endl; } ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4

9 Print a Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 □﹡ 45 35 25 15 05 =– i + 4

10 Print a Parallelogram Implementation for (int i=0; i<5; i++) { for (int j=0; j<-1*i+4; j++) { cout << ‘ ’; } for (int j=0; j<5; j++) { cout << ‘*’; } cout << endl; }

11 Print an Isosceles Triangle Print an isosceles triangle with height of 5 as follows: i=0 i=1 i=2 i=3 i=4 = 2i + 1 □ □ □ □﹡ □ □ □ ﹡ ﹡ ﹡ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ 1 3 5 7 9

12 Print an Isosceles Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<2*i+1; j++) { cout << ‘*’; } cout << endl; } ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4

13 Print an Isosceles Triangle Print an isosceles triangle with height of 5 as follows: ﹡ 1 3 5 7 9 □ □ □ □﹡ □ □ □ ﹡ ﹡ ﹡ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 □ 4 3 2 1 0 2i+1 -i+4

14 Print an Isosceles Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<-1*i+4; j++) { cout << ‘ ’; } for (int j=0; j<2*i+1; j++) { cout << ‘*’; } cout << endl; }

15 Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ 5 2 2 2 5 i=0 i=1 i=2 i=3 i=4

16 Print an Hallow Parallelogram for (int i=0; i<5; i++) { if (i==0) { for (int i=0; i<5; i++) cout << ‘*’; } else if (i >= 1 && i <= 3) { cout << ‘*’; } else if (i == 4) { for (int i=0; i<5; i++) cout << ‘*’; } cout << endl; } ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4

17 Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: ﹡ 5 2 2 2 5 □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ □ 3 3 3 i=0 i=1 i=2 i=3 i=4

18 Print an Hallow Parallelogram for (int i=0; i<5; i++) { if (i==0) { for (int i=0; i<5; i++) cout << ‘*’; } else if (i >= 1 && i <= 3) { cout << ‘*’; for (int j=0; j<3; j++) { cout << ‘ ‘; } cout << ‘*’; } else if (i == 4) { for (int i=0; i<5; i++) cout << ‘*’; } cout << endl; } ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ □ □ □ ﹡﹡ □ □ □ ﹡ ﹡ □ □ □ ﹡﹡ □ □ □ ﹡ ﹡ □ □ □ ﹡﹡ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4

19 Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: ﹡ 5 2 2 2 5 □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ □ 3 3 3 i=0 i=1 i=2 i=3 i=4 □ 4 3 2 1 0 -i+4

20 Print an Hallow Parallelogram for (int i=0; i<5; i++) { for (int j=0; j<-1*i+4; j++) cout << ‘ ‘; if (i==0) { for (int j=0; j<5; j++) cout << ‘*’; } else if (i >= 1 && i <= 3) { cout << ‘*’; for (int j=0; j<3; j++) { cout << ‘ ‘; } cout << ‘*’; } else if (i == 4) { for (int j=0; j<5; j++) cout << ‘*’; } cout << endl; } □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ □ □ □ ﹡ □ □ □ ﹡□ □ □ ﹡ □ □ □ ﹡ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4


Download ppt "Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i<5; i++) { for (int j=0; j<5; j++)"

Similar presentations


Ads by Google