Functions
Functions
Slide 2 Where are we now? now Midterm: structured programming Simple types of variables Array 3 program structures Functions(subprograms) cin(>)/cout(<< (File lO)
Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(<<) Array Functions (subprograms) (File I/O) Midterm: structured programming now
Slide 3 Review Problem Solving process le Define and analyze the problem What is the input output? What constraints must be satisfied? 0Eo What information is essential? Design an algorithm What steps must be done? Wirte down the solution steps in detail o05 Implement a program -programming or coding Compile, test, and debug the program -testing Document and maintain the program
Slide 3 Review: Problem Solving Process Define and analyze the problem What is the input & output? What constraints must be satisfied? What information is essential? Design an algorithm What steps must be done? Wirte down the solution steps in detail Implement a program - programming or coding. Compile, test, and debug the program - testing. Document and maintain the program
Slide 4 Top-down analysis A complex problem is often easier to solve by dividing it into several smaller parts ( subproblems), each of which can be solved by itself This is called top-down programming Let's take the example of Diamand drawing
Slide 4 Top-down analysis A complex problem is often easier to solve by dividing it into several smaller parts (subproblems), each of which can be solved by itself. This is called top-down programming. Let’s take the example of Diamand drawing!
Slide 5 Example: Diamond Pattern Input: nothing Output: print out a diamond pattern 大大大 大大大大大 大大大大大大大 大大大大大大大 大大大大大
Slide 5 Example: Diamond Pattern Input: nothing Output: print out a diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Slide 6 Example: top-down analysis Output: print out a diamond pattern print out the upper half > upper triangular form print out the lower half lower triangular form
Slide 6 Example: top-down analysis Output: print out a diamond pattern print out the upper half → upper triangular form print out the lower half → lower triangular form
Slide 7 Example: upper triangular pattern Upper triangular pattern row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars, row 3: print 2 spaces, 5 stars row 4: print 1 space, 7 stars; ******** row 5: print 0 spaces, 9 stars
Slide 7 Example: upper triangular pattern Upper triangular pattern: row 1: print 4 spaces, 1 star; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 5 stars; row 4: print 1 space, 7 stars; row 5: print 0 spaces, 9 stars; * * * * * * * * * * * * * * * * * * * * * * * * *
Slide 8 Example: top-down refinement Refinement = row 1: print (5-row) spaces, (2*row-1)stars row 2: print (5-row) spaces, (2*row-1)stars row 3: print (5-row) spaces, (2*row-1)stars, 大大大大大 row 4: print (5-row) spaces, (2* row-1)stars row 5: print (5-row) spaces, (2*row-1)stars, For each row(more exactly from row 1 to 5), always do Print 5-row spaces Print 2 *row-1 stars int rowi It's a loop: pseudo-code while (row<=5)t print 5-row spaces; p
Slide 8 Example: top-down refinement Refinement: row 1: print (5-row) spaces, (2*row - 1) stars; row 2: print (5-row) spaces, (2*row - 1) stars; row 3: print (5-row) spaces, (2*row - 1) stars; row 4: print (5-row) spaces, (2*row - 1) stars; row 5: print (5-row) spaces, (2*row - 1) stars; For each ‘row’ (more exactly from row 1 to 5), always do Print 5-row spaces Print 2*row-1 stars It’s a loop: pseudo-code * * * * * * * * * * * * * * * * * * * * * * * * * int row; row=1; while(row<=5){ print 5-row spaces; print 2*row-1 stars; row=row+1; }
Slide 9 Example: algorithm int row, space, stari int row i row=l; row=1 while(row=5)i while(row<=5) space=li while(space<=5-row)( 5-row spaces cout < 2*row-1 starsi space=space+li row=row+l; star= while(star<=2*row-1) cout < l* star=star+1 cout < endl row=row+1
Slide 9 Example: algorithm int row, space, star; row=1; while(row<=5){ space=1; while(space<=5-row) { cout << " "; space=space+1; } star=1; while(star<=2*row-1) { cout << "*"; star=star+1; } cout << endl ; row=row+1; } int row; row=1; while(row<=5){ 5-row spaces; 2*row-1 stars; row=row+1; }
Slide 10 Introduction to functions The subproblems are solved by subprograms, called functions in C++ main( is the first function, then executes these functions so that the original problem is solved
Slide 10 Introduction to Functions The subproblems are solved by subprograms, called functions in C++. main() is the first function, then executes these functions so that the original problem is solved