ExamBank Scot · N5 Computing Science question bank · All N5 Computing Science questions
16 marks · Input validation
The distance of a stage can range from 5 to 75 km. Using a design technique of your choice, refine step 6.2 to check the user enters a valid distance. Test data is used to ensure the distance entered is valid. State the type of test in the table below. Type of test Input Expected results 67.6 Program continues 3.7 Program displays an error message The following lines of code are written to input the distance for each stage of ‘Race one’. … Line 6 FOR stage FROM 0 TO 3 DO Line 7 RECEIVE distance FROM KEYBOARD … Changes should be made to the code above to ensure that any number of stages could be processed. Describe two changes that would be required. Change 1 Change 2 Edge Races classify races as ‘beginner’, ‘intermediate’ or ‘advanced’ based on the total distance of the race. The program displays the classification after calculating the total distance. … Line 25 <calculate the total distance for the race> Line 26 IF totalDistance < 25 THEN Line 27 SET race TO "beginner" Line 28 END IF Line 29 IF totalDistance >= 25 OR totalDistance <=100 THEN Line 30 SET race TO "intermediate" Line 31 END IF Line 32 IF totalDistance > 100 THEN Line 33 SET race TO "advanced" Line 34 END IF Line 35 SEND race TO DISPLAY … When tested the code produced an unexpected result. Identify the type of error in the code above. The code above is inefficient. Using a programming language of your choice, re‑write lines 26 to 34 to make this more efficient. During execution the code is translated. State the type of translator that has been used. The average stage distance for each race is calculated and stored in the variable avgDistance. Using a programming language of your choice, write the code to store the average to 1 decimal place. The average stage distance should be displayed, as shown below for ‘Race one’. The average is 20.4 km Using a programming language of your choice and the variable avgDistance, write the code to produce the output above. Edge Races are developing a program to process information on races with multiple stages. The first race has four stages as shown below. stage 1 stage 2 start finish stage 3 stage 4 Race one Stage Distance (km) 1 19.9 2 6.5 3 35.2 4 20.0 The design for part of the program is shown below. 6.1 Loop 4 times 6.2 Get valid distance for stage
• conditional loop • correct loop condition for valid data from 5 to 75 • input within the loop • error message displayed within loop Where candidate uses a post conditional loop the error message must be contained within an if statement Where candidate does not use a loop, one mark may be awarded for the error message where attached conditions are correct If the loop is missing the start/end of the loop assume that everything above or below the start/end is inside the loop Distances in the question are real values so conditions <=4, >=76 are not valid • normal • exceptional • ask user for number of stages • replace fixed loop value with input value Alternative solution: • change to conditional loop • ask user if they wish to enter another stage logic Accept logical (c) • change structure to - if - elseif - else/elseif OR - nested ifs • rewrite conditions, without complex conditions, with correct assignment to race IF totalDistance < 25 THEN SET race TO “beginner” ELSE IF totalDistance <=100 THEN SET race TO “intermediate” ELSE SET race TO “advanced” END IF Note that candidates may change order of conditions and assignments interpreter • round() function with brackets • with correct parameters: - avgDistance - 1 • display the correct text using quotes • avgDistance variable used between text with appropriate separators Note that the question does not ask for concatenation If one piece of text is missing only one separator is required