140 likes | 283 Views
Programming. Structured Programming. Structured Programming. A disciplined approach to programming Top-down design Step-wise refinement using a restricted set* of program structures * The set of program structures used in structured programming is: Sequence Choice Loop.
E N D
Programming Structured Programming
Structured Programming • A disciplined approach to programming • Top-down design • Step-wise refinement using a restricted set* of program structures * The set of program structures used in structured programming is: • Sequence • Choice • Loop
Top-Down Design • A program is divided into a main module and its related modules. Each module is in turn divided into submodules until the resulting modules are understood without further division.
Stepwise Refinement initialization Any action can be another: • Sequence • Choice • Loop condition_x false condition false true true action_x action_y update
A Sequence It is natural to write a program as a sequence of program structures such as sequences, choices and loops. Action 1 Action 2 Action N
A Choice Statement • Syntax if(condition) action • if the condition is true then execute the action. • action is either a single statement or a group of statements within braces. condition false true action
Another Choice Statement • Syntax if(condition) Action_AelseAction_B • if the condition is true execute Action_A else execute Action_B. condition true false Action_A Action_B
A Loop Statement • Syntax while(condition) action • How it works: • if condition is true then execute action • repeat this process until condition evaluates to false • action is either a single statement or a group of statements within braces. condition false true action
Another Loop Statement • Syntax for (initialization; condition; update) action • How it works: • execute initialization statement • while condition is true • execute action • execute update initialization condition false true action update
Yet Another Loop Statement • Syntax doaction while(condition) • How it works: • execute action • if condition is true then execute action again • repeat this process until condition evaluates to false. • action is either a single statement or a group of statements within braces. action condition true false
Diamond Pattern • Print out the following diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Diamond Pattern • Sub-problem: • print out the upper half • print out the lower half • Print out upper half: • 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; * * * * * * * * * • Print out lower half: • row 4: print 1 space, 7 stars; * * * * * * * • row 3: print 2 spaces, 5 stars; * * * * * • row 2: print 3 spaces, 3 stars; * * * • row 1: print 4 spaces, 1 star; *
Diamond Pattern • Algorithm for upper half: • 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; * * * * * * * * * • Algorithm for lower half: • row 4: print (5-row)spaces, (2*row - 1) stars; * * * * * * * • row 3: print (5-row)spaces, (2*row - 1) stars; * * * * * • row 2: print (5-row)spaces, (2*row - 1) stars; * * * • row 1: print (5-row)spaces, (2*row - 1) stars; *
Diamond Pattern int row, space, star; for(row=1; row<=5; row++){ //top half for(space=1; space<=5-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } for(row=4; row>=1; row--){ //bottom half for(space=1; space<=5-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; }