0% found this document useful (0 votes)
127 views30 pages

Chapter 10

Computer science hodder A level
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
127 views30 pages

Chapter 10

Computer science hodder A level
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 30
10 Data types and structures ATR lM) aol RIL eel Ben ee eee ety Se ie nee Re Com eee EA ENS Se Re eas Rau A cme pt cd See eet CC ea Ose cn tee uh ence WHAT YOU SHOULD ALREADY KNOW Try this activity to see if you can use one-dimensional arrays before you read the first part of this chapter. Write an algorithm, using pseudocode, to find the largest and smallest of five numbers. The numbers are to be input with appropriate prompts, stored in an array, and the largest and smallest are to be output with appropriate messages. If you haven't used an array before store the values in five separate variables. Key terms Data type a classification attributed to an item of data, which determines the types of value it can take and how it can be used Identifier — a unique name applied to an item of data Record (data type) — a composite data type comprising several related items that may be of different data types Composite data type — a data type constructed using several of the basic data types available in a particular programming language. Array — a data structure containing several elements of the same data type. Index (array) — a numerical indicator of an item of data's position in an array. Lower bound —the index of the first element in an array, usually 0 or 1 Upper bound — the index of the last element in an array. Linear search — a method of searching in which each element of an array is checked in order. Bubble sort — a method of sorting data in an array into alphabetical or numerical order by comparing adjacent items and swapping them if they are in the wrong order. File —a collection of data stored by a computer program to be used again. Abstract data type (ADT) — a collection of data and a set of operations on that data, Stack —a list containing several items operating on the last in, first out (LIFO) principle. Queue ~ a list containing several items operating on the first in, first out (FIFO) principle. Linked list—a list containing several items in which each item in the list points to the next item in the list. @ 10.1 Data types and records Any computer system that is reusable needs to store data in a structured way so that it can be reused in the future. One of the most powerful tools in computer science is the ability to search large amounts of data and obtain results very quickly. This chapter introduces data structures that enable effective and efficient computer-based storage and searching to take place. 10.1.1 Data types Data types allow programming languages to provide different classifications for items of data, so they can be used for different purposes. For example, integers are discrete whole numbers used for counting and indexing, whereas real numbers can be used to provide accurate measurements, ‘You need to be able to understand and make appropriate use of data types when writing pseudocode or programs to provide a solution to a problem. Programming languages have a number of built in data types. Table 10.1 lists the basic data types that you need to know and how they are referred to in pseudocode and different programming languages. Data type | Description Pseudocode | Python Java VB.NET Boolean | Logical values, True (1) and False (2) |BOOLEAN | bool boolean _| Boolean char Single alphanumerical character CHAR Not used char Char date Value to represent a date DATE class datetime | class Date | Date integer | Whole number, positive ornegative | INTEGER | int byte Integer short int long real Positive or negative number witha | REAL Float float single decimal point double string Sequence of alphanumerical STRING str class string characters string Table 10.1 Basic data types ACTIVITY 10A Decide which data type would be the best one to use for each item a) Your name b) The number of children in a class ) The time taken to run a race ) Whether a door is open or closed e) My birthday In pseudocode and some programming languages, before data can be used, the type needs to be decided. This is done by declaring the data type for each item to be used. Each data item is identified by a unique name, called an identifier. In pseudocode a declaration statement takes this form: DECLARE : For example DECLARE myBirthday : DATE ACTIVITY 10B Write declaration statements in pseudocode for each item. a) Your name b) The number of children in a class c) The time taken to run a race ) Whether a door is open or closed Write these declaration statements in your chosen programming language. If this is Python, you may need to write assignment statements. 10.1.2 Records Records are composite data types formed by the inclusion of several related items that may be of different data types. This allows a programmer to refer to these items using the same identifier, enabling a structured approach to using related items. A record will contain a fixed number of items. For example, a record for a book could include title, author, publisher, number of pages, and whether it is fiction or non-fiction. A record data type is one example of a composite user-defined data type. A composite data type references other existing data types when it is defined. A composite data type must be defined before it can be used. Any data type not provided by a programming language must be defined before it can be used. In pseudocode, a record data type definition takes the following form: TYPE DECLARE : DECLARE : DECLARE : ENDTYPE For example, the book record data type could be defined like this: TYPE TbookRecord DECLARE title : STRING DECLARE author : STRING DECLARE publisher : STRING DECLARE noPages : INTEGER DECLARE fiction : BOOLEAN ENDTYPE ‘The data type, ThookRecord, is now available for use and an identifier may now be declared in the usual way: DECLARE Book : TbookRecord Items from the record are now available for use and are identified by: . For example: Book.author < "David Watson" Book.fiction < FALSE ACTIVITY 10C 4. Write definition statements in pseudocode for a student record type containing these items. a) Name b) Date of birth ©) Class ) Gender 2 Use this record type definition to declare a record mystudent and set up and output a record for a male student Ahmad Sayed, in Class 5A, who was born on 21 March 2010. 3 In your chosen programming language, write a short program to complete this task. 10.2 Arrays ‘An array is a data structure containing several elements of the same data type; these elements can be accessed using the same identifier name. The position of each element in an array is identified using the array’s index. The index of the first element in an array is the lower bound and the index of the last element is the upper bound. The lower bound of an array is usually set as zero or one. Some programming languages can automatically set the lower bound of an array. Arrays can be one-dimensional or multi-dimensional. In this chapter, we will look at one-dimensional (1D) and two- dimensional (2D) arrays. 10.2.1 1D arrays A ID array can be referred to as a list. Here is an example of a list with nine elements and a lower bound of zero. Index —_myList Lower bound + [0] 27 (1) 19 [2] 36 [3] 42 [4] 16 [5] 89 [6] 21 (7 16 Upper bound — [8] 55 Figure 10.1 Example of a 1D array When a ID array is declared in pseudocode, the lower bound (LB), upper bound (UB) and data type are included: DECLARE : ARRAY[LB:UB] OF For example: DECLARE myList : ARRAY[0:8] OF INTEGER ‘The declared array can then be used, as follows: myList[7] <« 16 ACTIVITY 10D 4 Write statements in pseudocode to populate the array myList, as shown in Figure 10.1, using a FOR .. NEX’ loop. 2 In your chosen programming language, write a short program to complete this task, then output the contents of the array. Before writing your program find out how your programming language sets up array bounds. 10.2.2 2D arrays A 2D array can be referred to as a table, with rows and columns. Here is an example of a table with nine rows and three columns (27 elements) and lower bounds of zero. MyArray Column index [0] [4] [2] [0,c] 27 31 7 < lower bound row [1c] 19 67 48 [2,c] 36 98 29 Row index Bl ae ze Es [4,c] 16 35 61 [5.c] 89 46 41 [6,c] 21 1 28 [7 16 23 13 [8,c] 55 11 77 < upper bound row t t lower upper bound bound column column Figure 10.2 Example of a 2D array When a 2D array is declared in pseudocode, the lower bound for rows (LBR) and the upper bound for rows (UBR), the lower bound for columns (LBC) and the upper bound for columns (UBC), and data type are included: DECLARE : ARRAY[LBR:UBR, LBC:UBC] OF For example: DECLARE myArray : ARRAY[0:8,0:2] OF INTEGER The declared array can then be used, as follows: myArray[7,0] < 16 ACTIVITY 10E 4. Write statements in pseudocode to populate the array myArray, as shown in Figure 10.2, using a nested FOR ... NEXT loop 2 In your chosen programming language, write a short program to complete this task, then output the contents of the array. Arrays are used to store multiple data items in a uniformly accessible manner. All the data items use the same identifier and each data item can be accessed separately by the use of an index. In this way, lists of items can be stored, searched and put into an order. For example, a list of names can be ordered alphabetically, or a list of temperatures can be searched to find a particular value. EXTENSION ACTIVITY 10A Write a program to populate a three-dimensional (3D) array. ACTIVITY 10F In small groups of three or four, identify at least three uses for a 1D array and three uses for a 2D array. Compare array structures with record structures, decide if any of your uses would be better structured as records. 10.2.3 Using a linear search To find an item stored in an array, the array needs to be searched. One method of searching is a linear search, Each element of the array is checked in order, from the lower bound to the upper bound, until the item is found or the upper bound is reached. For example, the search algorithm to find if an item is in the populated ID array myList could be written in pseudocode as: DECLARE DECLARE DECLARE DECLARE DECLARE DECLARE myList : ARRAY[0:8] OF INTEGER upperBound : INTEGER lowerBound : INTEGER index : INTEGER item : INTEGER found : BOOLEAN upperBound < 8 lowerBound < 0 OUTPUT "Please enter item to be found" INPUT item found < FALSE index < lowerBound REPEAT IF item = mybist[index] THEN found <« TRUE ENDIF index « index + 1 UNTIL (found = TRUE) OR (index > upperBound) IF found THEN OUTPUT "Item found" ELSE OUTPUT "Item not found" ENDIF This algorithm uses the variables uppexBound and LowexBound so that the algorithm is easier to adapt for different lengths of list. The REPEAT. UNTIL loop makes use of two conditions, so that the algorithm is more efficient, terminating as soon as the item is found in the lis. As stated in Chapter 9, it is good practice to provide an identifier table to keep track of and explain the use of each identifier in an algorithm. This allows the programmer to keep track of the identifiers used and provides a useful summary of identifiers and their uses if the algorithm requires modification at a later date. Table 10.2 is the identifier table for the linear search algorithm. Identi Description item The integer to be found mybist Array to be searched upperBound Upper bound of the array LowerBound Lower bound of the array index Pointer to current array element found Flag to show when item has been found Table 10.2 ACTIVITY 10G Extend the pseudocode algorithm to output the value of the index if the item is found. In your chosen programming language write a short program to complete this task. You will need to populate the array myList before searching for an item. Use the sample data shown in myList in Figure 10.1 and search for the values 89 and 77. EXTENSION ACTIVITY 10B Extend your program created in Activity 10G to find any repeated items in a list and print out how many items were found 10.2.4 Using a bubble sort Lists can be more useful if the items are sorted in a meaningful order. For example, names could be sorted in alphabetical order, or temperatures could be sorted in ascending or descending order. There are several sorting algorithms available, One method of sorting is a bubble sort, Each element of the array is compared with the next clement and swapped if the elements are in the wrong order, starting from the lower bound and finishing with the element next to the upper bound. The element at the upper bound is now in the correct position. This comparison is repeated with one less element in the list, until there is only one element left or no swaps are made. For example, the bubble sort algorithm to sort the populated ID array myL.ist could be written in pseudocode as: DECLARE DECLARE DECLARE DECLARE DECLARE DECLARE DECLARE myList : ARRAY[0:8] OF INTEGER upperBound : INTEGER lowerBound : INTEGER index : INTEGER swap : BOOLEAN temp : INTEGER top : INTEGER upperBound <« 8 lowerBound < 0 top < upperBound REPEAT FOR index = lowerBound TO top - 1 Swap « FALSE IF myList [index] > myList[index + 1] THEN temp <« myList [index] myList[index] < myList[index + 1] myList[index + 1] < temp swap « TRUE ENDIF NEXT top < top -1 UNTIL (NOT swap) OR (top = 0) Table 10.3 is the identifier table for the bubble sort algorithm. Identifier Description myList Array to be searched Upper bound of the array Lower bound of the array Pointer to current array element ndex swap Flag to show when swaps have been made top Index of last element to compare temp ‘Temporary storage location during swap Table 10.3 The following cight tables show the changes to the 1D array my’ NEXT loop, the highest value in the list is correctly placed and the lower values of the list are iteration of the FOR as the bubble sort is completed. After each swapped and move towards the start of the list, until no more swaps are made. First pass of bubble sort All nine elements compared and five swaps: index | myList 0] 7] top + [8] Figure 10.3 27 19 36 42 16 89 21 16 55 19 19 19 19 19 19 19 19 ety 27 27 27 27 27 27 27 36 36 36 36 36 36 36 36 42 42 42 16 16 16 16 16 16 16 16 42 42 42 42 42 89 89 89 89 89. il 21 ral 21 21 21 21 2t 9. 16 16 16 16 16 16 16 16° Ba 55 55 55 55 55 55 55 557 89 ‘Second pass of bubble sort Eight elements compared and three swaps: index | myList 0. 19 27 36 16 42 21 16 55 89 top > SURAT RONS Figure 10.4 Third pass of bubble sort Seven elements compared and three swaps index | myList 19 27 16 36 21 16 42 55 89 top > SU AURVUNeS Figure 10.5 Fourth pass of bubble sort Six elements compared and three swaps: 19 27 36 16 42 21 16 55 89 19 27 36 16 42 21 16 55 89 19 19 27 27 36 16 16° Se 42 42 21 ral 16 16 55 55 89 89 19 19 27 27 36. 16 167 36 42 42 ra ral 16 16 55 55 89 89 19 19 19 27 27 27 16 16 16 36 36 36 ge 21 aL ea td 16 16 42 55 55 55 89 89 89 19 19 19 27 27 27 16 16 16 36 36 36 42. ul 21 a eas 8 16 16 ‘42 55 55 55 89 89 89 19 27 16 36 21 16 42 55 89 index 0. Ir 2 3 4: top > 5. 6 7 8 Figure 10.6 Fifth pass of bubble sort Five elements compared and three swaps: index 0 1 (2 2. top > 4 Bb 6 [7 8 Figure 10.7 myList. 19 19 19 27 27 16 16 167 27 36 36 36 21 21, 21 16 16 16 42 42 42 55 55 55 89 89 89 myList 19 16 16 16 7X 19 27 27. it 21 21 16 16 16 36 36 36 42 42 42 55 55 55 89 89 89 ‘Sixth pass of bubble sort Four elements compared and one swap: 19 19 19 16 16 16 27 27 27 36. 1 21 21 6. 16 16 167 836 42 42, 42 55 55 55 89 89 89 16 16 19 19 21 21 27. 16 16° 7 36 36 42 55 89 42 55 89 index | myList 0]) 16 = 16 1) 19 =| (19 2]} 21° 21 top> [3]| 16 16 4]) 27 27 5], 36 © 36 6]} 42 42 7]} 55 55 8]} 89 89 Figure 10.8 Seventh pass of bubble sort Three elements compared and one swap: index | myList 0. 16 16 1 19 19. top = 2 16 16 3 21 21 4 27 27 5. 36 36 6 42 42 Z 55 55 8 89 89 Figure 10.9 Eighth pass of bubble sort Two elements compared and no swaps: 16 19 16 19 21 16 16 a1 27 27 36 42 55 89 16 6 19 21 27 36 42 55 89 36 42 55 89 index | myList [o]} 16 16 top > (| 16 16 [2]; 19 19 PB]; 21 21 [4]| 27. 27 [5]] 36 36 [6]} 42 42 [7]} 55 55 [8]| 89 89 igure 10.10 ACTIVITY 10H In your chosen programming language, write a short program to complete a bubble sort on the array myList. Use the sample data shown in myiist in Figure 10.1 to populate the array before sorting. Output the sorted list once the bubble sort is completed. 10.3 Files Computer programs store data that will be required again in a file. Every file is identified by its filename. In this chapter, we are going to look at how to use text files. Text files contain a sequence of characters. Text files can include. an end of line character that enables the file to be read from and written to as lines of characters, In pseudocode, text files are handled using the following statements, To open a file before reading from it or writing to it OPEN FOR Files can be opened in one of the following modes: READ reads data from the file WRITE writes data to the file, any existing data stored in the file will be overwritten APPEND adds data to the end of the file Once the file is opened in READ mode, it can be read from a line at a time: READFILE , Once the file is opened in WRITE or APPEND mode, it can be written to a line at a time: WRITEFILE , In both cases, the variable must be of data type STRING. ‘The function EOF is used to test for the end ofa file. It returns a value TRUE if the end of a file has been reached and FALSE otherwise. EOF() When a file is no longer being used it should be closed: CLOSEFILE ‘This pseudocode shows how the file myText.. txt could be written to and read from: DECLARE textLn : STRING DECLARE myFile : STRING myFile < "myText.txt" OPEN myFile FOR WRITE REPEAT OUTPUT "Please enter a line of text" INPUT textLn IF textLn <> " THEN WRITEFILE, textLn ELSE, CLOSEFILE (myFile) ENDIF UNTIL textLn = '"™ OUTPUT "The file contains these lines of text:" OPEN myFile FOR READ REPEAT READFILE, textLn OUTPUT textLn UNTIL EOF(myFile) CLOSEFILE(myFile) ACTIVITY 101 Extend this pseudocode to append further lines to the end of myFile, In your chosen programming language write a short program to complete this file handling routine. Description textLn Line of text myFile File name Table 10.4 10.4 Abstract data types (ADTs) An abstract data type (ADT) is a collection of data and a set of operations on that data. For example, a stack includes the items held on the stack and the operations to add an item to the stack (push) or remove an item from the stack (pop). In this chapter we are going to look at three ADTs: stack, queue and linked list. Table 10.5 lists some of the uses of stacks, queues and linked lists mentioned in this book. Stacks ‘Queues Linked lists Memory management (see | Management of files sent to a Using arrays to implement a stack Section 16.1) printer (see Section 5.1) (see Section 19.1) Expression evaluation (see | Buffers used with keyboards (see _| Using arrays to implement a queue Section 16.3) Section 5.1) (see Section 19.1) Backtracking in recursion | Scheduling (see Section 16.1) Using arrays to implement a binary (see Section 19.2) tree (see Section 19.1) Table 10.5 Uses of stacks, queues and linked lists + Stack —a list containing several items operating on the last in, first out (LIFO) principle. Items can be added to the stack (push) and removed from the stack (pop). The first item added to a stack is the last item to be removed from the stack. + Queue ~a list containing several items operating on the first in, first out (FIFO) principle. Items can be added to the queue (enqueue) and removed from the queue (dequeue). The first item added to a queue is the first item to be removed from the queue. + Linked list ~a list containing several items in which each item in the list points to the next item in the list. Ina linked list a new item is always added to the start of the list. 7 1] 27. |< frontPointer 6 2 34 5 3 82 4| 79 | topPointer 4| 79 |< endPointer 3 82 5 2| 34 6 1 27 « basePointer 7 Stack Queue Figure 10.11 Stack and queue startPointer startPointer ~— ea DE li fm node node node node ‘igure 10.12 Linked list, Stacks, queues and linked lists all make use of pointers to manage their operations. Items stored in stacks and queues are always added at the end. Linked lists make use of an ordering algorithm for the items, often ascending or descending. A stack uses two pointers: a base pointer points to the first item in the stack and a top pointer points to the last item in the stack. When they are equal there is only one item in the stack. ‘A queue uses two pointers: a front pointer points to the first item in the queue and a rear pointer points to the last item in the queue, When they are equal there is only one item in the queue. A linked list uses a start pointer that points to the first item in the linked list, Every item in a linked list is stored together with a pointer to the next item. This is called a node. The last item in a linked list has a null pointer. 10.4.1 Stack operations ‘The value of the basePointer always remains the same during stack operations: 7 7 7 6 6 6 5 5 5 4| 79 |< topPointer 4 4| 31 |< topPointer 3] 82 3| 82 | NameList[Pointer + 1] THEN Temp < NameList [Pointer] NameList [Pointer] <« NameList[Pointer + 1] NameList [Pointer + 1] < Temp ENDIF NEXT NEXT a) A special case is when NameList is already in order. The algorithm above is applied to this special case. Explain how many iterations are carried out for each of the loops. 2] b) Rewrite the algorithm using pseudocode, to reduce the number of unnecessary comparisons. Use the same variable names where appropriate. [5] Adapted from Cambridge International AS & A Level Computer Science 9608 Paper 41 Q5 part (b) June 2015 A queue Abstract Data Type (ADT) has these associated operations: — create queue — add item to queue remove item from queue ‘The queue ADT is to be implemented as a linked list of nodes Each node consists of data and a pointer to the next node. a) The following operations are carried out: CreateQueue AddName("A1i' AddName("Jack") AddName ("Ben") AddName ("Ahmed") RemoveName AddName("Jatinder") RemoveName Copy the diagram below and add appropriate labels to show the final state of the queue Use the space on the left as a workspace. Show your final answer in the node shapes on the right: BI b) Using pseudocode, a record type, Nocle, is declared as follows: TYPE Node DECLARE Name : STRING DECLARE Pointer : INTEGER ENDTYPE ‘The statement DECLARE Queue : ARRAY[1:10] OF Node reserves space for 10 nodes in array Queue. i) The CreateQueue operation links all nodes and initialises the three pointers that need to be used: HeadPointer, TailPointer and FreePointer. Copy and complete the diagram to show the value of all pointers after Crea teQueue has been executed. 4] Queue Name Pointer HeadPointer (1) TailPointer [4] FreePointer 7 8] | [10] ii) The algorithm for adding a name to the queue is written, using pseudocode, as a procedure with the header: PROCEDURE AddName(NewName) where NewName is the new name to be added to the queue, ‘The procedure uses the variables as shown in the identifier table. Identifier Data type Description Queue Array[1:10] OF Array to store node data NewName STRING Name to be added FreePointer NTEGER Pointer to next free node in array HeadPointer NTEGER Pointer to first node in queue TailPointer Pointer to last node in queue CurrentPointer Pointer to current node PROCEDURE AddName(BYVALUE NewName STRING) // Report error if no free nodes remaining IF FreePointer = 0 THEN Report. Error ELSE // new name placed in node at head of free list CurrentPointer <« FreePointer Queue [CurrentPointer].Name < NewName /{ adjust free pointer FreePointer < Queue[CurrentPointer].Pointer // if first name in queue then adjust head pointer IF HeadPointer = 0 THEN HeadPointer < CurrentPointer ENDIF // current node is new end of queue Queue [CurrentPointer].Pointer < 0 TailPointer < CurrentPointer ENDIF ENDPROCEDURE Copy and complete the pseudocode for the procedure RemoveName. Use the variables listed in the identifier table. (6) PROCEDURE RemoveName() // Report error if Queue is empty -Name OUTPUT Queuel. // current node is head of queue // update head pointer // i£ only one element in queue then update tail pointer ENDPROCEDURE Cambridge International AS & A Level Computer Science 9608 Paper 41 Q6 June 2015

You might also like