From the IB guide: The focus of this topic is how an understanding of programming languages enhances the students’ understanding of computational thinking and provides them with opportunities for practical, hands-on experience of applying computational thinking to practical outcomes.
In externally assessed components questions will be presented using flow charts and/or pseudocode as outlined in the approved notation sheet. Answers will only be required in pseudocode. Students must be given the opportunity to convert algorithms into working code that is executed and tested.
Exit skills
Define the term variable
Define the term array
Explain the characteristics of sequential (linear) search, binary search, bubble sort and selection sort
Develop various algorithms on arrays
Introduction
Variables
In computer science, a variable acts as a storage location that can be used to store a value. Each variable has a name that is used to refer to the stored value. The value of the variable can be changed during program execution. Each variable can store a particular type of data like strings, reals, boolean and integers.
Linear (one dimensional arrays)
One variable can only store a data element of a program. If a second data element is needed then another variable should be created. The more data elements needed, the more distinct variables should be created by the programmer. An array can hold multiple data elements of the same type (integers, strings, boolean etc.). An array has a name, a size that cannot be changed during program execution (in most cases is a static data structure) and a data type that describes the type of data that it can store. A one-dimensional array is a type of linear array. Many programming languages, including Python, always define the lower bound of an array starting with the number 0. The pseudocode described in the IBO Computer Science Guide, uses zero-based arrays by default (unless otherwise specified). Consider the following array of integers: The size of the array is 10, the type is integer and the name is A.
Note: At SL level only one dimensional arrays will be examined.
Pseudocode example 10: Use of array (page 201)
The following program (p201_Ex10), will fill the array A with values from 1 to 10 and then print the values of the array.
Run this program in Mat's pseodocode software tool.
Pseudocode example 11: Symmetric 1D array
The following program (p202_Ex11 Symmetric 1D array), evaluates if an array is symmetric with respect to its middle element.
If the array size is even, then the program stops.
Run this program in Mat's pseodocode software tool and re-write as a Python program.
Parallel arrays
Parallel arrays are extremely useful when a programmer wants to store different properties
of an entity (fields of a record). All elements of an array should be of the same data type. So,
if a programmer wants to store different data types of an entity (e.g. student) parallel arrays
offer a convenient solution. The data are organised as a table. Each row represents a
particular student and all columns are of the same data type.
Pseudocode example 13: Parallel arrays (names and grades) (page 203)
In the following example 10 students’ names and their equivalent grades will be used. The array NAMES of type String will be used to hold the names of the students, while the array GRADES of type Integer will hold the equivalent grades. The index of each array is the same for the same student. It is very important when using parallel arrays to always access each array at the same index when storing or retrieving values. This process guarantees the reference of corresponding data elements.
p203_Ex13 - Run this program in Mat's pseodocode software tool
Convert Ex13 in to a python program and run it in codeskulptor3.
Two dimensional arrays
Although two dimensional arrays do not appear in the SL syllabus it is strongly
recommended to study them. The understanding of the structure and function of 2D arrays enhances various algorithmic skills.
A one dimensional array should be considered as a single line of elements. However, in many cases, data come in the form of a data table. A typical example is a table that depicts the average monthly temperature for 10 cities.
20 arrays are indexed by two subscripts. The indices must be integers. The first one refers to the row and the second to the column. TEMP [1] [1] refers to February temperature of City 2. The value is 27. Each element in a two dimensional array must be of the same data type.
Pseudocode example 15: Two dimensional array (temperatures)
//This program will use the array TEMP which is a 2D ARRAY
//It will print the contents of the array
//12 months 5 cities
TEMP =
[[10,11,12,13,10],
[10,13,14,12,12],
[13,13,14,15,12],
[16,17,17,17,16],
[22,23,24,24,24],
[26,25,24,25,26],
[29,28,26,27,26],
[29,28,27,28,28],
[24,23,24,25,25],
[20,21,22,23,24],
[15,16,17,18,18],
[12,11,13,11,11]]
MONTH = 0
CITY = 0
loop MONTH from 0 to 11
output MONTH +1, "Month"
loop CITY from 0 to 4
output "City", CITY+1, TEMP[MONTH] [CITY]
end loop
end loop
Download p206_Example_15_Two_dim_array and run in Mat's pseodocode software tool
Convert this program to Python using VSC.