04 - Algorithms Arrays
04 - Algorithms Arrays
– arrays
– subscripts or index
– elements
50 8 val[0]
12 val[4]
• Usually:
– Loop to
• read values into the array variable / each of the
array element
• display values from each of the array elements
• compute / manipulate the values within each
element
– first element in the array is at position 0
• hence, looping would be from 0 to size-1
– E.g. if the size of the array is 5, loop from 0 to 4 step 1
Print newline
Print “The values in array are:”
Print newline
LOOP count FROM 0 TO 4 STEP 1
Print val[count]
NEXT count
ENDLOOP LOOP to display values within array
END
val[5]
7 val[1]
55 val[2] 8 55 46
46 val[3]
7 12 44
12 val[4]
num [1][0] num [1][1] num [1][2]
CT010-3-1 Fundamentals of Software Development Module Briefing
Initializing values to the array
num[0][0] = 8
num[0][1] = 55
num[0][2] = 46
num[1][0] = 7
num[1][1] = 12
num[1][2] = 14
Pseudocode
LOOP row FROM 0 TO n-1 STEP 1
LOOP col FROM 0 TO n-1 STEP 1
Read num[row][col]
NEXT col
ENDLOOP
NEXT row
ENDLOOP
CT010-3-1 Fundamentals of Software Development Module Briefing
Read values to the array variable
row = 0
DOWHILE (row<=n-1)
col = 0
DOWHILE (col<=n-1)
Read num[row][col]
col = col + 1
ENDDO
row = row + 1
ENDDO
Pseudocode
LOOP row FROM 0 TO n-1 STEP 1
LOOP col FROM 0 TO n-1 STEP 1
Print num[row][col]
NEXT col
ENDLOOP
NEXT row
ENDLOOP
CT010-3-1 Fundamentals of Software Development Module Briefing
Display values in array variable