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