Presentation is loading. Please wait.

Presentation is loading. Please wait.

Higher Computing Science

Similar presentations


Presentation on theme: "Higher Computing Science"— Presentation transcript:

1 Higher Computing Science
Answering programming questions in the exam

2 This homework session will look at the different type of programming questions you may encounter in the exam and will look at a selection of programming questions from past papers

3 Learning content The Haggis reference language Key concepts revision Types of programming question 2016 Exam Paper Questions 2015 Exam Paper Questions 2015 Specimen Paper Questions 2014 Exemplar Paper Questions

4 The Haggis Reference language
Programming code examples in the exam will always use the Haggis Reference language You need to be able to: Understand code written in Haggis Explain code written in Haggis You do not need to be able to: Write code written in Haggis Haggis is not a programming language – it has a syntax, so it is a reference language

5 Programming constructs: Simple / Complex conditions
IF studentScore > 50 THEN SET totalPass TO totalPass + 1 END IF IF age < 5 OR age > 60 = THEN SET concession TO concession + 1 ELSE SET fullFare TO fullFare + 1 When explaining code you should use the technical terms for programming constructs if possible

6 Programming constructs: Fixed Loops
SET minimumValue TO numbers[0] FOR counter FROM 1 TO 9 DO IF minimumValue > numbers[counter] THEN SET minimumValue TO numbers[counter] END IF END FOR This code is part of the finding the minimum algorithm

7 Programming constructs: Conditional Loop
SET counter TO 0 REPEAT IF numbers[counter] = itemToFind THEN SET found TO true END IF SET counter TO counter + 1 UNTIL found OR counter > arraySize This code is from the linear search algorithm

8 Programming constructs: Conditional Loop
WHILE userInput < 1 OR userInput > 100 DO SEND "Input must be between 1 and 100" TO DISPLAY RECEIVE userInput FROM KEYBOARD END WHILE This is part of an input validation algorithm

9 Logical operators: AND / OR
REPEAT SEND "Please enter a value between 1 and 100" TO DISPLAY RECEIVE userInput FROM KEYBOARD UNTIL userInput > 0 AND userInput < 101 WHILE userInput < 1 OR userInput > 100 DO SEND "Input must be between 1 and 100" TO DISPLAY END WHILE Note the two types of conditional loop used here

10 Simple Data Structures
Type Definition Examples STRING Any set of characters from the keyboard An ARRAY of CHARACTER TD5 7EG %$£9*!aV SET name TO ["F","r","e","d"] INTEGER A number without a decimal point REAL A number with a decimal point BOOLEAN A variable which can be either true or false True False A STRING can be considered to be a complex data structure if the language treats it as an array of characters

11 Complex Data Structures
Type Definition Examples ARRAY A set of data items, all of the same data type identified using an index DECLARE names[10] AS ARRAY OF STRING DECLARE nums[10] AS ARRAY OF INTEGER RECORD A single set of values of different data types TYPE student IS RECORD {STRING forename, STRING surname, INTEGER score} SET studentA TO {forename="Jim", surname="Jonson", score=79} ARRAY OF RECORD A set of records identified using an index DECLARE s6Class[10 ] AS ARRAY OF student SET s6Class[0] TO {forename="Jim", surname="Jonson", score=79} Syntax will vary depending on the language used.

12 Functions and procedures
A function returns a value A procedure performs a sequence of actions Functions return a value so need to be declared as returning a particular data type Function names are usually nouns, procedure names are usually verbs

13 validInteger function
Functions and procedures validInteger function FUNCTION validInteger(lowerLimit, upperLimit)RETURNS INTEGER DECLARE userInput AS INTEGER INITIALLY 0 RECEIVE userInput FROM (INTEGER) KEYBOARD WHILE (userInput < lowerLimit) OR (userInput > upperLimit) DO SEND "Input must be between" & lowerLimit "and" & upperLimit TO DISPLAY RECEIVE userInput FROM KEYBOARD END WHILE RETURN userInput END FUNCTION

14 stars procedure Functions and procedures
PROCEDURE stars (INTEGER numberOfStars) FOR counter FROM 1 TO numberOfStars DO SEND " * " TO DISPLAY END FOR END PROCEDURE Number_of Stars is a formal parameter – ie it is being declared in the procedure definition. Some languages require a type definition as well.

15 Formal and Actual Parameters
Formal Parameters are variables which are declared when a sub-procedure or function is defined Actual parameters are used when a sub-procedure or function is called A quick re-cap on parameters Formal and actual

16 Formal and Actual Parameters
Formal parameter PROCEDURE stars (INTEGER numberOfStars) FOR counter FROM 1 TO numberOfStars DO SEND " * " TO DISPLAY END FOR END PROCEDURE DECLARE totalStars AS INTEGER INITIALLY 0 SEND " How many stars wanted?" TO DISPLAY RECEIVE totalStars FROM KEYBOARD stars (totalStars) Formal parameters are variables which are part of the definition Actual parameters are variables or values used when the procedure is being used (called) Actual parameter

17 Value Parameters A value parameter is one which is passed into a procedure or function and whose value is used by that procedure When a sub procedure is called with a variable as a value parameter, a copy of that variable is made using its formal parameter while the procedure is running. The copy of the variable is destroyed when the procedure has completed, so the memory can be reused A quick re-cap on parameters.

18 Reference Parameters When a sub procedure is declared with a reference parameter then when it is called with an actual variable its value is changed by that sub procedure. When a variable is passed as a reference parameter, the reference is to the memory location of the variable itself. No copy is being made. NB Not all programming languages make the distinction between value and reference parameters. Some languages need you to declare a parameter as a reference parameter otherwise it assumes that you are intending to use it as a value parameter

19 Reference Parameters The value of the variable changes as a result of calling the sub procedure with it as a parameter PROCEDURE swap (a, b) DECLARE temp AS INTEGER INITIALLY 0 SET temp TO a SET a TO b SET b TO temp END PROCEDURE Note that temp is a local variable. It only has a value inside the swap procedure

20 Input validation (from National 5) Finding the maximum / minimum value
Standard Algorithms Input validation (from National 5) Finding the maximum / minimum value Linear Search Counting Occurrences Don't just memorise these algorithms for the exam. It is unlikely that you will be asked to reproduce them exactly as you have seen them in the notes. Instead, practice coding them using your preferred programming language.

21 Input Validation Uses a conditional loop May need to check for more than one condition

22 Finding the maximum / minimum value
Uses a fixed loop May use parallel arrays: e.g. given an array of pupil names and an array of their scores, find the name of the pupil with the highest mark

23 Linear Search Can use a conditional loop (find the first occurrence in the array) Uses a variable to store the index position when the target is found May use a Boolean variable to give an error message if not found Can use a fixed loop (find every occurrence in the array)

24 Counting Occurrences Uses a fixed loop to check through the array Uses a separate counter for the occurrences inside the loop May use a Boolean variable to give an error message if not found

25 Programming exam questions
Writing code Debugging code Explaining code

26 Questions which ask you to write code
Can be in pseudocode or a programming language of your choice Make sure that code inside loops or conditional statements is clearly delineated: IF … THEN … END IF IF ( … ) { … } FOR counter FROM 1 TO 10 DO … END FOR FOR (counter = 0, counter < 11, counter++) { … } WHILE … DO … END WHILE WHILE ( … ) { … } I've not included Python examples here – it uses indents to separate code in conditional statements

27 Questions which ask you to write code
Read the question carefully Make a trace table: Write down variable names and values for each line number Be methodical

28 Questions which ask you to explain or fix code
Refer to line numbers Don’t miss out anything just because you think it is too obvious to mention Use correct terms for loops, conditions, data structures etc.

29 Links to specimen and past papers
2016 exam paper: 2015 exam paper: 2015 Specimen paper: 2014 Exemplar Paper: It may be easier to follow if you can have these links open when specific questions are being discussed Exam paper: 2015 exam paper: Specimen paper: Exemplar paper:

30 Past Paper programming questions
2016 Q10a, Q13,Q15 2015 Q3, Q7, Q14, Q17b,c Specimen Paper Q2, Q8, Q10, Q9, Q10, Q11d Exemplar paper Q8, Q9, Q10, Q11

31 2016 Exam Paper Q 10a 2016 Exam paper:

32 Counting occurrences using parallel arrays
2016 Exam Paper Q 10a Counting occurrences using parallel arrays SET total TO 0 FOR counter FROM 0 TO 5 DO IF SDD[counter] = "B" AND ISDD[counter] = "B" THEN SET total TO total + 1 END IF END FOR SEND ("The total number of pupils attaining a B in both tests was " & total) TO DISPLAY

33 2016 Exam Paper Q13a Line 1 SET list TO [71,76,66,67,89,72]
Eloïse wants to search for an item of data held in an array. She writes the following algorithm. Line SET list TO [71,76,66,67,89,72] Line SET target TO 71 Line SET found TO false Line FOR counter FROM 0 TO 5 DO Line IF list[counter] = target THEN Line SET found TO true Line ELSE Line SET found TO false LINE END IF LINE END FOR LINE IF found =true THEN LINE SEND “Item found” TO DISPLAY LINE ELSE LINE SEND “Not found” TO DISPLAY LINE END IF

34 2016 Exam Paper Q13a 71 False True False
A trace table is shown below which shows the line numbers where a variable has changed. State the missing values at A, B, C and D line list target counter found 1 [71,76,66,67,89,72] 2 A 3 B 4 6 C 8 D 71 False True False

35 The algorithm is incorrect and so outputs the wrong message.
2016 Exam Paper Q13a The algorithm is incorrect and so outputs the wrong message. Explain why the algorithm is incorrect. Describe how to correct the algorithm

36 2016 Exam Paper Q13a SET list TO [71,76,66,67,89,72] SET target TO 71
SET found TO false FOR counter FROM 0 TO 5 DO IF list[counter] = target THEN SET found TO true ELSE END IF END FOR IF found = true THEN SEND “Item found” TO DISPLAY SEND “Not found” TO DISPLAY Remove lines 7 and 8

37 2016 Exam Paper Q13a SET list TO [71,76,66,67,89,72] SET target TO 71 SET found TO false SET counter TO 0 REPEAT IF list[counter] = target THEN SET found TO true END IF SET counter TO counter + 1 UNTIL found OR counter > 5 IF found = true THEN SEND “Item found” TO DISPLAY ELSE SEND “Not found” TO DISPLAY OR Use a conditional loop until found is true (or end of list)

38 2016 Exam Paper Q15a Tony coaches a team of eight elite athletes for a 400 metre race. Tony uses a program to help analyse each athlete’s performance. A sample of the data held on each athlete is shown below. Tony has added a record structure to his program. RECORD athleteData IS {STRING forename, STRING surname, INTEGER runnerNumber, BOOLEAN professional, REAL seasonBest, REAL weight} Tony wants to store his eight athletes’ data using the record structure shown above. The variable name is athletes. Using pseudocode, or a programming language of your choice, declare the variable which can store the data for the eight athletes. DECLARE athletes[7] AS athleteData

39 2016 Exam Paper Q15b Using pseudocode, or a programming language of your choice, write the code necessary to add the data for the athlete Salma shown in the table above. Your answer should use the variable declared in part (a). DECLARE athletes[0 ] INITIALLY athleteData (“Salma”, “Hussain”,324, True, 45.12, 67·5)

40 2016 Exam Paper Q15c Tony wants to find the fastest 400 m time of the season. Using pseudocode, or a programming language of your choice, design an algorithm to find the fastest season time. Your answer should use the variable declared in part (a). SET minimum TO athletes[0].seasonBest FOR counter FROM 1 TO 7 DO IF athletes[counter].seasonBest < minimum THEN SET minimum TO athletes[counter].seasonBest END IF END FOR SEND ("Fastest time was " & minimum) TO DISPLAY

41 2015 Exam Paper Section1 Q3 An online company uses a computer program to display particular customer records. The algorithm of this program is shown below. Line 1 SET found TO false Line 2 RECEIVE search_name FROM (STRING) KEYBOARD Line 3 FOR counter FROM 0 TO <End Of List> DO Line 4 IF name[counter] = search_name THEN Line 5 SET found TO true Line 6 SEND name[counter] & counter TO DISPLAY Line 7 END IF Line 8 END FOR Line 9 IF found = false THEN Line 10 SEND “Name not found” TO DISPLAY Line 11 END IF 2015 exam paper:

42 2015 Exam Paper Section1 Q3 The following data is stored in the name array: Jimmy, Samina, Kate, Jimmy, Adam State the output from the above program if Jimmy is entered at line 2 from the keyboard. FOR counter FROM 0 TO <End Of List> DO IF name[counter] = search_name THEN SET found TO true SEND name[counter] & counter TO DISPLAY END IF END FOR found search_name counter name[counter] DISPLAY false Jimmy Here making a trace table might be useful. This is a linear search using a fixed loop, so all the occurrences will be found but in addition it displays the position of the item in the array. true Jimmy Jimmy Jimmy 0 true Jimmy 1 Samina

43 2015 Exam Paper Section1 Q3 The following data is stored in the name array: Jimmy, Samina, Kate, Jimmy, Adam State the output from the above program if Jimmy is entered at line 2 from the keyboard. FOR counter FROM 0 TO <End Of List> DO IF name[counter] = search_name THEN SET found TO true SEND name[counter] & counter TO DISPLAY END IF END FOR found search_name counter name[counter] DISPLAY false Jimmy true Jimmy 0 1 Samina 2 Kate

44 2015 Exam Paper Section1 Q3 The following data is stored in the name array: Jimmy, Samina, Kate, Jimmy, Adam State the output from the above program if Jimmy is entered at line 2 from the keyboard. FOR counter FROM 0 TO <End Of List> DO IF name[counter] = search_name THEN SET found TO true SEND name[counter] & counter TO DISPLAY END IF END FOR found search_name counter name[counter] DISPLAY false Jimmy true Jimmy 0 1 Samina 2 Kate 3

45 2015 Exam Paper Section1 Q3 The following data is stored in the name array: Jimmy, Samina, Kate, Jimmy, Adam State the output from the above program if Jimmy is entered at line 2 from the keyboard. FOR counter FROM 0 TO <End Of List> DO IF name[counter] = search_name THEN SET found TO true SEND name[counter] & counter TO DISPLAY END IF END FOR found search_name counter name[counter] DISPLAY false Jimmy true Jimmy 0 1 Samina 2 Kate 3 Jimmy 3

46 2015 Exam Paper Section1 Q3 The following data is stored in the name array: Jimmy, Samina, Kate, Jimmy, Adam State the output from the above program if Jimmy is entered at line 2 from the keyboard. FOR counter FROM 0 TO <End Of List> DO IF name[counter] = search_name THEN SET found TO true SEND name[counter] & counter TO DISPLAY END IF END FOR found search_name counter name[counter] DISPLAY false Jimmy true Jimmy 0 1 Samina 2 Kate 3 Jimmy 3 4 Adam

47 2015 Exam Paper Section1 Q7 Line 1 SET found TO false
Craig has been asked to write an algorithm that will search for a target ID from a list of fifty receipts. Each receipt has a unique receipt ID. Part of the algorithm is shown below. Line 1 SET found TO false Line 2 SET counter TO −1 Line 3 RECEIVE target_id FROM (INTEGER) BARCODEREADER Line 4 REPEAT Line 5 SET counter TO counter + 1 Line 6 IF receipt_id [counter] = target_id THEN Line 7 SET found TO true Line 8 END IF Line 9 UNTIL ……………… Using pseudocode, or a language with which you are familiar, complete line 9 of the algorithm shown above.

48 2015 Exam Paper Section1 Q7 Line 1 SET found TO false Line 2 SET counter TO −1 Line 3 RECEIVE target_id FROM (INTEGER) BARCODEREADER Line 4 REPEAT Line 5 SET counter TO counter + 1 Line 6 IF receipt_id [counter] = target_id THEN Line 7 SET found TO true Line 8 END IF Line 9 UNTIL found = true OR counter = 49 Using pseudocode, or a language with which you are familiar, complete line 9 of the algorithm shown above.

49 2015 Exam Paper Section2 Q14b Rowena accidentally entered an invalid mobile phone number and an error message is displayed. A valid mobile phone number will consist of a string of 11 digits. Using pseudocode or a programming language of your choice, write the algorithm which would check that the mobile phone number is valid.

50 2015 Exam Paper Section2 Q14b Input validation using two conditions
FUNCTION checkPhone (STRING mobileNumber) RETURNS BOOLEAN SET valid TO true IF Length (mobileNumber) <> 11 THEN SET valid TO False ELSE FOR counter FROM 1 TO 11 DO IF (Mid(mobile_number, counter, 1) < "0") OR Mid(mobile_number, counter, 1) > "9") THEN SET valid TO false END IF END FOR RETURN valid END FUNCTION

51 2015 Exam Paper Section2 Q14c An algorithm is implemented to validate the applicant’s data from the application form. There are two subprograms at lines two and three. The parameters for these subprograms are not shown. Line 1 REPEAT Line 2 Enter_applicant_data (…) Line 3 Validate_form_data (…) Line 4 UNTIL <form data is valid> Name a parameter that should be passed at line 2, State the type of Parameter passing used and justify your answer. title, firstName, surname, gender, Address, or mobilePhoneNumber. Passed by reference as it will be updated and returned for line 3 to process

52 2015 Exam Paper Section2 Q17a Chris wants a program to process information about each of the pupils in his class. Line1 RECORD Test_marks IS {STRING surname, INTEGER mark_1, INTEGER mark_2, INTEGER mark_3, STRING } Line2 SET pupil[1] TO (“Smith”, 67, 89, 91, Line3 SET pupil[2] TO (“Latif”, 42, 91, 84, Line4 SEND pupil[1].mark_2 TO DISPLAY (i) Explain the purpose of line 2. State the output from line 4.

53 2015 Exam Paper Section2 Q17a Chris wants a program to process information about each of the pupils in his class. Line1 RECORD Test_marks IS {STRING surname, INTEGER mark_1, INTEGER mark_2, INTEGER mark_3, STRING } Line2 SET pupil[1] TO (“Smith”, 67, 89, 91, Line3 SET pupil[2] TO (“Latif”, 42, 91, 84, Line4 SEND pupil[1].mark_2 TO DISPLAY (i) Explain the purpose of line 2. It assigns values to element at position 1 in the array of Test_marks records State the output from line

54 2015 Exam Paper Section2 Q17a (iii) Chris wants to calculate the average for the first pupil. Using pseudocode, or a language with which you are familiar, write the line to calculate this average.

55 2015 Exam Paper Section2 Q17a (iii) Chris wants to calculate the average for the first pupil. Using pseudocode, or a language with which you are familiar, write the line to calculate this average. SET average TO (pupil[1].mark_1 + pupil[1].mark_2 + pupil[1].mark_3) / 3

56 2015 Exam Paper Section2 Q17b Chris calculates the average mark for each pupil and stores the average marks in an array. He writes the following pseudocode to count the number of grade A passes of 70 or more Line 1 SET list TO [74.33, 57.67, 73.33, 82.33] Line 2 SET amount TO 0 Line 3 FOR counter FROM 0 TO 2 DO Line 4 IF list[counter] >= 70 THEN Line 5 SET amount TO amount + 1 Line 6 END IF Line 7 END FOR Line 8 SEND amount TO DISPLAY

57 2015 Exam Paper Section2 Q17b When Chris tests the program, it outputs the wrong number of A passes. (i) State the output from the code above. (ii) State the name of this type of error. (iii) Identify and correct the line of the algorithm which contains the error. amount counter list[counter] 74.33 Make a trace table to debug FOR counter FROM 0 TO 2 DO IF list[counter] >= 70 THEN SET amount TO amount + 1 END IF END FOR

58 2015 Exam Paper Section2 Q17b When Chris tests the program, it outputs the wrong number of A passes. (i) State the output from the code above. (ii) State the name of this type of error. (iii) Identify and correct the line of the algorithm which contains the error. amount counter list[counter] 74.33 1 57.67 FOR counter FROM 0 TO 2 DO IF list[counter] >= 70 THEN SET amount TO amount + 1 END IF END FOR

59 2015 Exam Paper Section2 Q17b When Chris tests the program, it outputs the wrong number of A passes. (i) State the output from the code above. (ii) State the name of this type of error. (iii) Identify and correct the line of the algorithm which contains the error. amount counter list[counter] 74.33 1 57.67 2 73.33 FOR counter FROM 0 TO 2 DO IF list[counter] >= 70 THEN SET amount TO amount + 1 END IF END FOR

60 2015 Exam Paper Section2 Q17b amount counter list[counter] 74.33 1
When Chris tests the program, it outputs the wrong number of A passes. (i) State the output from the code above. (ii) State the name of this type of error. (iii) Identify and correct the line of the algorithm which contains the error. (ii) Logic Error (iii) Line 3 FOR counter FROM 0 TO 3 DO amount counter list[counter] 74.33 1 57.67 2 73.33

61 2015 Exam Paper Section2 Q17c Chris creates an algorithm that will search the array of average marks and return the smallest value present. Line 1 SET list TO [74.33, 57.67, 73.33, 87.33] Line 2 SET minimum TO list [0] Line 3 FOR counter FROM 1 TO 3 DO Line 4 IF minimum > list[counter] THEN Line 5 SET minimum TO list[counter] Line 6 END IF Line 7 END FOR return the smallest value present. Array indexes start at zero. This hasn't always been consistent in previous example papers

62 2015 Exam Paper Section2 Q17c A trace table is used to record the change to a variable at the corresponding line number. Part of the trace table is shown below. State the values missing from the trace table below at A, B and C. A – B – 1 C – 57.67 Line 1 SET list TO [74.33, 57.67, 73.33, 87.33] Line 2 SET minimum TO list [0] Line 3 FOR counter FROM 1 TO 3 DO Line IF minimum > list[counter] THEN Line SET minimum TO list[counter] Line END IF Line 7 END FOR return the smallest value present.

63 Specimen Paper Q2 Clare has just started programming and has created an algorithm to search the array cars which holds one hundred car registration numbers. Clare wishes to search for a specific registration number each time she uses the program. Clare’s algorithm is shown below. 1 SET check TO 0 2 SET counter TO 1 3 RECEIVE registration FROM KEYBOARD 4 REPEAT 5 IF cars[counter] = registration THEN 6 SET check TO 1 7 END IF 8 SET counter TO counter UNTIL check = 1 OR counter = 101 Specimen paper:

64 Specimen Paper Section1 Q2
Clare could have used a Boolean variable called “found” as part of this algorithm. She alters line 1 to read: 1 SET found TO false. With reference to the line numbers shown, state the other changes that Clare would need to make if she wished to use this Boolean variable. 1 SET found TO false 2 SET counter TO 1 3 RECEIVE registration FROM KEYBOARD 4 REPEAT 5 IF cars[counter] = registration THEN 6 SET found TO true 7 END IF 8 SET counter TO counter UNTIL found = true OR counter = 101

65 Specimen Paper Section 2 Q8b
A program has been written to find the position of the maximum value in a list, however the program stops responding. The algorithm responsible is shown below. 1 SET source TO [71,76,66,67,89,72] 2 SET position TO 1 3 FOR counter FROM 2 TO 6 4 IF source[counter] > source[position] THEN 5 SET counter TO position 6 END IF 7 END FOR

66 Specimen Paper Section 2 Q8b
Complete the information in the table, recording the value assigned to the variable for line numbers 2, 3 and 5. Line Source Position Counter 1 [77,66,88,67,89,72] 2 3 5

67 Specimen Paper Section 2 Q8b
Complete the information in the table, recording the value assigned to the variable for line numbers 2, 3 and 5. Line Source Position Counter 1 [77,66,88,67,89,72] 2  1 3 5

68 Specimen Paper Section 2 Q8b
Complete the information in the table, recording the value assigned to the variable for line numbers 2, 3 and 5. Line Source Position Counter 1 [77,66,88,67,89,72] 2  1 3  2 5

69 Specimen Paper Section 2 Q8b
Complete the information in the table, recording the value assigned to the variable for line numbers 2, 3 and 5. Line Source Position Counter 1 [77,66,88,67,89,72] 2  1 3  2 5 (ii) Explain why the loop never terminates. (iii) Describe how the algorithm should be corrected.

70 Specimen Paper Section 2 Q8b(iii)
The Counter goes back to 1 on every time the loop goes round so the terminating condition of the loop is never met 1 SET source TO [71,76,66,67,89,72] 2 SET position TO 1 3 FOR counter FROM 2 TO 6 4 IF source[counter] > source[position] THEN SET position TO counter 6 END IF 7 END FOR

71 Specimen Paper Section 2 Q9a
This TaxCode function returns a string value from two inputs: Income1 and Income2 1 SET taxCode TO “Z” 2 SET salary TO (income1 + income2) 3 IF salary < 9000 THEN 4 SET taxCode TO “A” 5 END IF 6 IF salary > 9000 AND salary < THEN 7 SET taxCode TO “B” 8 END IF 9 IF salary > AND salary < THEN 10 SET taxCode TO “C” 11 END IF 12 IF salary > THEN 13 SET taxCode TO “D” 14 END IF 15 RETURN taxCode Explain why this algorithm would return an incorrect taxCode if income1 is and income2 is

72 Specimen Paper Section 2 Q9b
Using pseudocode or a language with which you are familiar, rewrite the algorithm to correct the logic error and make the code more efficient. FUNCTION taxCode SET taxCode TO “Z” SET salary TO (income1 + income2) IF salary < 9000 THEN SET taxCode TO “A” ELSE IF salary >= 9000 AND salary < THEN SET taxCode TO “B” ELSE IF salary >= AND salary < THEN SET taxCode TO “C” ELSE IF salary >= THEN SET taxCode TO “D” END IF RETURN taxCode END FUNCTION Function and End function lines are not in original question

73 Specimen Paper Section 2 Q9c
Using pseudocode or a language with which you are familiar, write an algorithm for a subroutine that will: • Ask the user for the values for variables salary and bonus • Use the function to assign the variable code • Display code on screen RECEIVE salary FROM KEYBOARD RECEIVE bonus FROM KEYBOARD code = taxcode (salary, bonus) SEND code TO DISPLAY

74 Specimen Paper Section 2 Q10a

75 Specimen Paper Section 2 Q10a
Counting Occurrences and File Handling SET frequency TO 0 FOR counter FROM 1 TO 30 IF rainfall[counter]=0 AND lowtemp[counter]<0 THEN SET frequency TO frequency+1 END IF END FOR OPEN drydays SEND frequency TO drydays CLOSE drydays We have to assume that the two arrays are indexed by the number of the day, so rainFall[0] and lowTemp[0] contain no data

76 Specimen Paper Section 2 Q10b
The algorithm used to count the number of dry days below freezing is implemented as a subroutine. Describe how the subroutine could make this value available to other parts of the program. You could declare it as a parameter whose value is changed by a procedure or as the value returned from a function. This value could then be passed to subsequent routines. PROCEDURE minusTemp (INTEGER minusValues) FUNCTION minusTemp RETURNS INTEGER

77 Specimen Paper Section 2 Q11d
When registering, the user must enter a valid address. This validation process is carried out by code written in a scripting language. In the language used, the syntax for an IF statement is: and the OR comparator is written using the symbol | | The following code is used to validate the address: In the code above • The variable length stores the number of characters in the address • The variable atpos stores the position of character • The variable dotpos stores the position of the last dot if (expression) { command(s) } if (atpos<2 || dotpos< atpos+2 || dotpos+2>=length){ alert(“Not a valid address”); return false; }

78 Specimen Paper Section 2 Q11d
For example, if the address is then length = 14, atpos = 7 and dotpos = 11 Explain how the code above would process the validation of the address: alert(“Not a valid address”); return false; expression value atpos < 2 false dotpos < atpos+2 true dotpos + 2 >= length atpos = 8 dotpos = 3 length = 11 Again a trace table is useful here

79 Exemplar Paper Q 8 An algorithm will ask for a target score and then find all the (target) scores in the array and reduce them by 10. Using pseudocode, or a language with which you are familiar, complete the missing lines of code at lines 4 and 5. 1 SET scores TO [16,12,19,20,17,8,13,19] 2 RECEIVE target FROM (INTEGER) KEYBOARD 3 FOR counter FROM 0 TO 7 DO 4 IF END IF 7 END FOR IF target = scores[counter] THEN SET Scores[counter] TO scores[counter]-10 Exemplar paper: This question did not make it clear that only the target score was to be reduced by 10

80 Exemplar Paper Q9a A record data structure is used for the members’ details. RECORD Members IS {STRING surname, STRING forename, STRING username, INTEGER password} Describe how the website could store the information for five million members, using the record data structure above. DECLARE people [ ] AS ARRAY OF Members Or

81 Exemplar Paper Q9b(i) When someone logs on, a search is performed using the Username that they entered. Using pseudocode, or a language with which you are familiar, write an algorithm that asks for a Username and then finds the position of that Username. We need the linear search algorithm here

82 Exemplar Paper Q 9b(i) RECORD Members IS {STRING surname, STRING forename, STRING username, INTEGER password} DECLARE people AS ARRAY OF Members INITIALLY [] SET found TO false SET counter TO -1 SET position to 0 RECEIVE targetname FROM (STRING) KEYBOARD REPEAT SET counter TO counter + 1 IF people[counter].username= targetname THEN SET found TO true SET position TO counter END IF UNTIL counter = OR found= true IF found = true THEN SEND "Found at "& position TO DISPLAY

83 Exemplar Paper Q 10a Finest Fashions is an online fashion store. Customers can browse the company website, search for items of clothing and make online purchases. The screen shot below shows one page from the Finest Fashions website: All images on the website have been stored as bitmaps of integers, corresponding to the colour of each pixel. Explain whether this compression algorithm is lossy or lossless, by making reference to the line numbers in the algorithm

84 Exemplar Paper Q 10a 1 SET pixelPrev TO " " 2 SET runLength TO 0 3 SET counter TO 0 4 REPEAT 5 RECEIVE pixelColour[counter] FROM (INTEGER)file1 6 IF pixelPrev ≠ pixelColour[counter] THEN 7 SEND pixelColour[counter] TO file2 8 SEND runLength TO file2 9 SET pixelPrev TO pixelColour[counter] 10 SET runLength TO 0 11 END IF 12 SET counter TO counter SET runLength TO runLength UNTIL end of file1 Exemplar paper:

85 Exemplar Paper Q 10a Lines 4, 5 and 6: Every pixel in the original file1 is processed. Line 13: Counts the number of consecutive pixels that are the same colour. Line 7: Sends the pixel colour to the compressed file2. Line 8: Sends the number of consecutive pixels of that colour to the compressed file2.

86 Exemplar Paper Q 10b

87 Exemplar Paper Q 10b RECEIVE customerItemRating FROM WEBSITE SET itemRatings TO itemRatings + 1 SET totalRatings TO totalRatings + 1 SET averageRating TO totalRatings / itemRatings SEND averageRating TO WEBSITE

88 Exemplar Paper Q11 A function used to perform an arithmetical operation is shown below: FUNCTION combinations (INTEGER number) RETURNS INTEGER SET factor TO 1 FOR counter FROM 1 TO number DO SET factor TO counter * factor END FOR RETURN factor END FUNCTION A program has two variables called items and permutations which contain the values 3 and 0 respectively. State the values contained in items and permutations after the execution of this line of code: SET permutations TO combinations(items) A trace table is useful here

89 Exemplar Paper Q11 SET permutations TO combinations(items) Items
counter factor number 3 1 1 3 2 2 3 6 6

90 Thank you Please complete the online evaluation form


Download ppt "Higher Computing Science"

Similar presentations


Ads by Google