START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10...

12
TRACING ALGORITHMS WITH ARRAYS

Transcript of START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10...

Page 1: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

TRACING ALGORITHMS WITH ARRAYS

Page 2: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ALGORITHM 1STARTDECLARE num AS integer array of size 10DECLARE count AS integerFOR count ← 1 TO 10

num[count] ← count * 2ENDFORSTOP

Trace table headed as follows:count num

1 2 3 4 5 6 7 8 9 10

Page 3: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 1

count num

1 2 3 4 5 6 7 8 9 10

1 2

2 2 4

3 2 4 6

4 2 4 6 8

5 2 4 6 8 10

6 2 4 6 8 10 12

7 2 4 6 8 10 12 14

8 2 4 6 8 10 12 14 16

9 2 4 6 8 10 12 14 16 18

10 2 4 6 8 10 12 14 16 18 20

Page 4: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ALGORITHM 2STARTDECLARE num1, num2 AS integer array of

size 5DECLARE count AS integerFOR count ← 1 TO 5

num1[count] ← count num2[count] ← num1[count] * num1[count]

ENDFORSTOP

Page 5: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 2

count num1 num2

1 2 3 4 5 1 2 3 4 5

Page 6: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 2

count num1 num2

1 2 3 4 5 1 2 3 4 5

1 1 1

2 1 2 1 4

3 1 2 3 1 4 9

4 1 2 3 4 1 4 9 16

5 1 2 3 4 5 1 4 9 16 25

Page 7: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ALGORITHM 3START

DECLARE num1, num2, ans AS integer array of size 4

DECLARE count AS integer

FOR count ← 1 TO 4num1[count] ← count num2[count] ← count + countans [count] ← num1[count] + num2[count]

ENDFORSTOP

Page 8: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 3

count

num1 num2 ans

1 2 3 4 1 2 3 4 1 2 3 4

1 1 2 3

2 1 2 2 4 3 6

3 1 2 3 2 4 6 3 6 9

4 1 2 3 4 2 4 6 8 3 6 9 12

Page 9: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ALGORITHM 4START

DECLARE SIZE AS constant integer with value 7

DECLARE num1 AS integer array of size SIZE

DECLARE count AS integer

FOR count ← 1 TO SIZEIF count MOD 2 == 0 THEN

num1[count] ← count * 2ELSE

num1[count] ← count * 3ENDIF

ENDFORSTOP

Page 10: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 4

SIZE count num

1 2 3 4 5 6 7

7 1 3

7 2 3 4

7 3 3 4 9

7 4 3 4 9 8

7 5 3 4 9 8 15

7 6 3 4 9 8 15 12

7 7 3 4 9 8 15 12 21

Page 11: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ALGORITHM 5START

DECLARE SIZE AS constant integer with value 4

DECLARE cost AS integer array of size SIZE

DECLARE discount AS real array of size SIZE

DECLARE count AS integer

FOR count ← 1 TO SIZE discount[count] = cost[count] * (10/100)

ENDFORSTOP

Cost will have the values --- 12, 5, 10, and 60

Page 12: START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10 num[count] ← count * 2 ENDFOR STOP Trace table headed as follows:

ANSWER TO ALGORITHM 5 Someone writes answer on the board.