Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Quickly Read a Program? A/Prof Anthony Tung (邓锦浩) Department of Computer Science School of Computing NUS.

Similar presentations


Presentation on theme: "How to Quickly Read a Program? A/Prof Anthony Tung (邓锦浩) Department of Computer Science School of Computing NUS."— Presentation transcript:

1 How to Quickly Read a Program? A/Prof Anthony Tung (邓锦浩) Department of Computer Science School of Computing NUS

2 Agenda How do computer imitate human’s computation? Scopes and States Variable Dependency, Boundary Condition and Use of Test Data

3 Shopping Behavior Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping Memory (记忆) %Energy80% $InPocket$100 State Diagram (状态图) Items Input The whole shopping behavior can be modeled as transition between three states: Init, Item Selection/Buying and Exit. Transitions between the states ( 状态转移 ) are determined by (1)The State Diagram (2)Content of Memory(which is in term dependent on the state diagram) (3)Input of Items

4 Shopping Behavior (Sub-states) Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 4 Memory (记忆) %Energy80% $InPocket$100 State Diagram (状态图) Items Input 3 1 2 1 2 3 4

5 Programming involves… 1.Defining the correct set of states (sub- states) 2.Setting the correct transition conditions given the input values and values from the memory 3.Writing and reading the correct values from the correct memory locations 4 Memory (记忆) %Energy80% $InPocket$100 State Diagram (状态图) Items Input 3 1 2

6 Dependency between three components State Diagram Values in Memory Inputs State diagrams change memory values memory values affect transitions affect what to input input affect transitions Input values go into memory

7 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state

8 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 --- Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1

9 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 --- 380%$100toy$20Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2

10 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 --- 380%$100toy$20Yes 480%$80toy$20Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3

11 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 --- 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4

12 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes 667.7%$80toy$20Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4 1

13 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes 667.7%$80toy$20Yes 767.7%$80ring$100Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4 1 2

14 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes 667.7%$80toy$20Yes 767.7%$80ring$100Yes 849.9%$80ring$100Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4 1 2 4

15 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes 667.7%$80toy$20Yes 767.7%$80ring$100Yes 849.9%$80ring$100Yes 949.9%$80ring$100Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4 1 2 4 1

16 A correct program will… 4 State Diagram (状态图) 3 1 2 General States State%Energy$InPocketItemCost(Item)Like(Item) 180%$100--- 280%$100 380%$100toy$20Yes 480%$80toy$20Yes 567.7%$80toy$20Yes 667.7%$80toy$20Yes 767.7%$80ring$100Yes 849.9%$80ring$100Yes 949.9%$80ring$100Yes 1049.9%$80ring$100Yes Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping 1 2 3 4 Go through a set of “general states” ( 大状况 ) and reach the exit condition with the correct output, The correctness of each general state is dependent on the correctness of the previous general state 1 2 3 4 1 2 4 1

17 From Human to Computer Programs Values in RAM Input/ Outputs Program change values in memory memory values affect program flow affect what to input/output input affect program flow Input values go into memory

18 Agenda How do computer imitate human’s computation? Scopes and States Variable Dependency, Boundary Condition and Generation of Test Data

19 Scopes(I) The scope of a control statement refers to the regions which are control by the statement: if () then { } else { } for () { } while () { } Scope_if Scope_else Scope_forScope_while Disclaimer: We won’t be faithfully following the syntax of C here. Most programming languages apply here anyway.

20 Scopes(II) By looking at the scopes of these control statements, you derived the skeleton of the program. main() { if () then { } else { } for () { } if () { } Scope_for Scope_if main() { for () { } for () { } if () { } Scope_for Scope_if Example 1Example 2

21 Init %Energy Init $InPocket; while ($InPocket>0) and (%Energy > 50) { get next Item; if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) %Energy-= Random } Exit Shopping Scopes and States Scopes and states can be seen to be related in several ways: – The control statement of the scope decide the state transitions 4 State Diagram (状态图) 3 1 2

22 Scopes and States Scopes and states can be seen to be related in several ways: – The control statement of the scope decide the state transitions – You can see the whole scope itself as 1 state and derive sub-states within the scope Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { /* Shopping State} } Exit Shopping State Diagram (状态图) 4 3 1 2

23 Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { /* Shopping State} } Exit Shopping Scopes and States Scopes and states can be seen to be related in several ways: – The control statement of the scope decide the state transitions – You can see the whole scope itself as 1 state and derive sub-states within the scope – Inserting a nested scope break it’s parent scope into at least two stages State Diagram (状态图)

24 Init %Energy Init $InPocket; While ($InPocket>0) and (%Energy > 50) { /* Shopping State*/ if like(Item) AND ($InPocket > cost(Item)) $InPocket-= cost(Item) } Exit Shopping Scopes and States Scopes and states can be seen to be related in several ways: – The control statement of the scope decide the state transitions – You can see the whole scope itself as 1 state and derive sub-states within the scope – Inserting a nested scope break it’s parent scope into at least two stages State Diagram (状态图) 3 2 1 3 2 1

25 #include void main() {//int A[20], N, Temp, i, j; //printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); //scanf("%d", &N); //printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } //printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); fo//r(i=0; i<N; i++) //printf("\n\t\t\t%d", A[i]); getch(); } How do we analyze this program?

26 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } What are the variabes? Identify the structure of the scope? In this case one loop within another loop

27 Type of Variables Direct Control Variable (DCV): Variables that are within control statements and control the flow of the program InDirect Control Variable(ICV): Variables that are NOT within control statements but affect Direct Control Variable Passive Variable(PV): Variables that are neither DCV or ICV Static vs Varying: Variables are static within a scope if they don’t change within the scope, else they are varying within the scope

28 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N

29 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N Dependency between variables N i 1 1 bounding indexing assignment

30 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N Dependency between variables N i 1 1 2 2 Temp A[i]A[i] 2 2 bounding indexing assignment

31 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N Dependency between variables N i 1 1 2 2 Temp A[i]A[i] 2 2 3 3 j bounding indexing assignment

32 Scope_for #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N Dependency between variables N i 1 1 2 2 Temp A[i]A[i] 2 2 3 3 j bounding indexing assignment

33 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N By this time, we know how the two loops are related! N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 4 bounding indexing assignment

34 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i] Passive Varible: A[j+1] Static Variable: N By this time, we know how the two loops are related! N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 5 bounding indexing assignment

35 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N By this time, we know how the two loops are related! N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 bounding indexing assignment

36 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni

37 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=10

38 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=10 j

39 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=10 j 21

40 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=10 j 21

41 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=10 j

42 #include void main() {int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp =0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); getch(); } Direct Control Variable: i, N, Temp, j, A[j] InDirect Control Variable: A[i], i Passive Varible: A[j+1] Static Variable: N Let’s use some test data N i 1 1 2 2 Temp A[i]A[i] 2 2 3 4 3 4 j A[j]A[j] 45 A[j+1] 56 6 A Ni Temp=15 j

43

44 Summary To understand a program quickly: – Use the concept of scope to quickly understand the structure of the program breaking them down into different states – Analyze the dependency between variables to see how the flow among the states are being affected Assignment Index Bounding – Use test data to trace through the program(possibly) with a debugger to see how the variable are affected.


Download ppt "How to Quickly Read a Program? A/Prof Anthony Tung (邓锦浩) Department of Computer Science School of Computing NUS."

Similar presentations


Ads by Google