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++)

Slides:



Advertisements
Similar presentations
Space and Shape. rectangle Shape ? trapezium.
Advertisements

For(int i = 1; i
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Functions Prototypes, parameter passing, return values, activation frams.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Revision.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Chapter 4 Summation.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Examples from “c++ how to program” book. SELECTIONS WITH IF-ELSE Example: if ( grade >= 60) cout = 60) cout
8cm 5cm Area = 8 x 5 = 40cm 2 A parallelogram can be split up into a rectangle and 2 triangles – each with the same area. 10cm 5cm.
Geometric Formulas RectangleSquare Parallelogram Triangle Ch. 5: 2 – D Measurement Trapezoid.
Extension. Calculate the area and perimeter of each of these shapes. 5cm 9cm 3cm 2cm 10cm 7cm 1cm 7cm 2cm 12cm 13cm
An Introduction to 2-D Shape Slideshow 15, Mathematics Mr Richard Sasaki, Room 307.
The distance around an enclosed shape is called the perimeter. The amount of space enclosed inside a shape is called the area.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 10 Inheritance and Polymorphism
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Perimeter of Rectangles
Area of Plane Shapes Area of Compound Shapes 8 m 2 m 5 m 2 m Not to scale 4 m 3 m ? ? 16 m 2 20 m 2 6 m 2 Area = = 42 m 2.
Lesson 11.2 Area of Parallelograms and Triangles.
Instructor - C. BoyleFall Semester
10-3 Surface Areas of Prisms
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Name this shape Objective: Pupils can name 2D shapes Rectangle Square
Area Chapter 10. O In your own words, write a definition for area. O Create a list of the area formulas that you know. O Rectangle O Triangle O Parallelogram.
Form 1 Mathematics Area of plane shapes Chapter 16.
for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;
Tangrams Step by Step.
Warm up Give the formulas for how to find the area of a: Rectangle Triangle Circle Kite.
Area of a Rectangle = base x height
Section 3 Review Mr. Crone.
Two Dimensional Array Mr. Jacobs.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Area of Shapes.
CS150 Introduction to Computer Science 1
2-D Measurement Chapter 5.
UNIT 8: 2-D MEASUREMENTS PERIMETER AREA SQUARE RECTANGLE PARALLELOGRAM
Chapter 4 Loops Case Studies
Areas of Triangles and Special Quadrilaterals
Chapter 5 Function Basics
How to find the area of a parallelogram.
Calculating the Area of a Right Triangle
Starting Out with C++: From Control Structures through Objects
Self study.
Areas on a Grid © T Madas.
If Statements.
Area Formula of a Square?
Modular Programming with Functions
CS150 Introduction to Computer Science 1
Rectangle.
Aim I can calculate the area of a triangle and a parallelogram. Success Criteria I know the formula for calculating the area of triangles and parallelograms.
Parallelogram: Sides opposite are parallel
CS149D Elements of Computer Science
Print the following triangle, using nested loops
Geometry: Areas of Triangles
10.1 Areas of Parallelograms and Triangles
Presentation transcript:

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