IGCSE 4 Cambridge Data types and arrays Computer Science Section 2 Unit 7 Algorithm design and problem-solving 4
Objectives Define the terms variable and constant Use and identify variables and constants in program code Identify and use the data types integer, real, Boolean, character and string Use predefined procedures/functions Use one-dimensional arrays
What is a variable? A variable is a location in memory in which you can temporarily store text or numbers It is used like an empty box or the Memory function on a calculator You can choose a name for the box (the “variable name” or “identifier”) and change its contents in your program
Using variables in a program You have used variables in many different ways in programs you have already written Add 1 to NumberOfStudents CircleArea 3.142 * Radius * Radius WHILE NOT Found … Answer “Y” OUTPUT StudentName What different types of variable are shown above?
Variable types Variable types typically include integer, real, Boolean, character and string In many programming languages variables have to be declared at the start of the program so that the appropriate amount of memory can be reserved for them Var NumberOfStudents : integer; CircleArea : real; Found : Boolean; Answer : char; StudentName : string;
Definitions of data types Type of data Typical amount of memory Integer Whole number such as 156, 0 -54 2 bytes Real Number with a fractional part such as 1.5276, -68.4, 20.0 4 bytes Char A single ASCII character such as A, b, 3, ! or space 1 byte String Zero or more characters 1 byte per character in the string Boolean Can only take the values True or False Theoretically just one bit, but in a high level language such as Python, Pascal etc., one byte
Constants As well as variables, you can define constants in a program PI = 3.14157926536 MonthlyTariff = 15.60 Why declare a constant instead of a variable? Can a constant ever change its value?
Predefined functions High-level programming languages have built-in functions which are part of the language Examples of common functions are: int(s) convert a string s to an integer real(s) or float(s) convert a string s to a number with a decimal point round(x,n) round a number x to n decimal places sqrt(x) return the square root of x E.g. a round (15.168534 ,2) puts a = 15.17 What is num after the statement num int (“57”)?
Importing modules Modules are files containing code (procedures or functions) that can be used by other programs In Python, for example, the statement import random written at the beginning of the program will allow you to use a function to generate a random number in a given range x = random.randint (1,100) will put a random number between 1 and 100 in the variable x
Worksheet 4 Now complete Task 1, questions 1 and 2
Arrays An array is a data structure that allows you to hold several variables, all of the same type, with one name. An array of 10 integers called UserName could be declared as UserName : array[1..10] of string The elements of the array are typically referred to as UserName[1], UserName[2], …. UserName[10] In some programming languages the “index” starts at 0: UserName[0], UserName[1], …. UserName[9]
Defining an array of constants If you wanted to store constant values in an array, you could write the pseudocode statement: Resort [“Wakatobi”, “Isola Bella”, “Coolangatta”, “Paphos”, “Bentota”] You can find out whether a resort name input by a user is in the list: FOR n = 1 to 5 IF ResortName = Resort [n] THEN Found True ENDIF ENDFOR Can you rewrite the algorithm using a WHILE loop?
Advantages of using arrays Imagine that you want to input, sort and print 100 user names. It would be very inconvenient to use 100 different variable names Using an array, you can write: FOR i = 1 TO 100 UserName[i] USERINPUT NEXT i (Sort the names) Output (UserName[i]) Next i
Data types Now complete Worksheet 4 Task 2
Plenary In this lesson you have worked with: Variables and constants Different variable types such as integer, real and string Predefined procedures and functions such as int and round One-dimensional arrays