Algorithms- Arrays
CT010-3-1 Python Programming
Topic & Structure of the lesson
• Problem Solving Using Programmed
Solutions
– Introduction to arrays
– Program design using pseudocodes and
simple notations of flowcharts
• arrays
– single-scripted array
– double scripted array
CT010-3-1 Fundamentals of Software Development Module Briefing Slide 2 (of 22)
Learning outcomes
• At the end of this lecture you should be
able to:
– define a problem
– outline a solution using algorithms –
• arrays
CT010-3-1 Fundamentals of Software Development Module Briefing Slide 3 (of 22)
Key terms you must be able to
use
• If you have mastered this topic, you should
be able to use the following terms correctly
in your assignments and exams:
– arrays
– subscripts or index
– elements
CT010-3-1 Fundamentals of Software Development Module Briefing Slide 4 (of 22)
Arrays
• Provide programmers with a way of organizing a
collection of data items that have the same type
into a single data structure
• Is a data structure that is made up of a number
of variables all of which have the same data type
• When used, will enable a single variable name
to be associated with all data items / elements
CT010-3-1 Fundamentals of Software Development Module Briefing
Arrays
val[5] size of the array
num
position
50 8 val[0]
Variable called num
7 val[1]
Array variable
called val[ ]able to
able to store one
value, i.e. 50 55 val[2] store five values,
i.e. 8, 7, 55, 46
46 val[3] and 12
12 val[4]
CT010-3-1 Fundamentals of Software Development Module Briefing
Arrays
• 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
CT010-3-1 Fundamentals of Software Development Module Briefing
Sample
Design a program to display a series of 5 values
from the user and display the values
Sample input-output:
Enter 5 values:
21 55 78 63 25
CT010-3-1 Fundamentals of Software Development Module Briefing
Pseudocode
PROGRAM GetAndDisplay5Values
BEGIN
Print “Enter 5 values :”
Print newline
LOOP count FROM 0 TO 4 STEP 1
Read val[count] LOOP to read values into array
NEXT count
ENDLOOP
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
CT010-3-1 Fundamentals of Software Development Module Briefing
2 D Arrays
• Format : variable_name [row][col]
num [2][3] 2 x 3 = 6
val[5]
8 val[0] num [0][0] num [0][1] num [0][2]
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
CT010-3-1 Fundamentals of Software Development Module Briefing
Read values to the array variable
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
Pseudocode - Alternative method
row = 0
DOWHILE (row<=n-1)
col = 0
DOWHILE (col<=n-1)
Read num[row][col]
col = col + 1
ENDDO
row = row + 1
ENDDO
CT010-3-1 Fundamentals of Software Development Module Briefing
Display values in array variable
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
Pseudocode – Alternative solution
row = 0
DOWHILE (row<=n-1)
col = 0
DOWHILE (col<=n-1)
Print num[row][col]
col = col + 1
ENDDO
row = row + 1
ENDDO
CT010-3-1 Fundamentals of Software Development Module Briefing
Summary of Main Teaching
Points
• Problem Solving Using Programmed
Solutions
– Introduction to arrays
– Program design using pseudocodes and
simple notations of flowcharts
• arrays
– single-scripted array
– double scripted array
CT010-3-1 Fundamentals of Software Development Module Briefing
Next Session
• Introductory Programming Skills
– Python
CT010-3-1 Fundamentals of Software Development Module Briefing