START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10...
-
Upload
laurel-wilkerson -
Category
Documents
-
view
216 -
download
1
Transcript of START DECLARE num AS integer array of size 10 DECLARE count AS integer FOR count ← 1 TO 10...
TRACING ALGORITHMS WITH ARRAYS
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
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
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
ANSWER TO ALGORITHM 2
count num1 num2
1 2 3 4 5 1 2 3 4 5
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
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
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
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
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
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
ANSWER TO ALGORITHM 5 Someone writes answer on the board.