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++) { cout << ‘*’; } cout << endl; } –Question: what shape does the program print? ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i = 0 i = 1 i = 2 i = 3 i = 4
Print a Right Triangle Print a right triangle with height of 5 as follows: ﹡﹡ ﹡﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡﹡﹡ ﹡﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i= = i + 1
Print a Right Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<i+1; j++) { cout << ‘*’; } cout << endl; }
Print an Inverted Triangle Print a inverted triangle with height of 5 as follows: ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡﹡ ﹡﹡﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡﹡ ﹡﹡ i=0 i=1 i=2 i=3 i= = 5 - i
Print an Inverted Triangle Implementation for (int i=0; i<5; i++) { for (int j=0; j<5-i; j++) { cout << ‘*’; } cout << endl; }
Print a Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 ﹡
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
Print a Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ ﹡ ﹡ ﹡ ﹡ ﹡□ ﹡ ﹡ ﹡ ﹡ ﹡﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 □﹡ =– i + 4
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; }
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 □ □ □ □﹡ □ □ □ ﹡ ﹡ ﹡ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡
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
Print an Isosceles Triangle Print an isosceles triangle with height of 5 as follows: ﹡ □ □ □ □﹡ □ □ □ ﹡ ﹡ ﹡ □ □ ﹡ ﹡ ﹡ ﹡ ﹡ □ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4 □ i+1 -i+4
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; }
Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ ﹡ i=0 i=1 i=2 i=3 i=4
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
Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: ﹡ □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ □ i=0 i=1 i=2 i=3 i=4
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
Print a Hallow Parallelogram Print a inverted triangle with edge of 5 as follows: ﹡ □ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡□ □ □ □ ﹡ ﹡ ﹡ ﹡ ﹡□ □ □ ﹡ □ □ □ ﹡□ □ ﹡ □ □ □ ﹡□ ﹡ □ □ □ ﹡﹡ ﹡ ﹡ ﹡ ﹡ □ i=0 i=1 i=2 i=3 i=4 □ i+4
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