Photo AI

When a user presses the 'Show Results' button, the program should output the number of the heat that had the fastest runner, for example: "The fastest runner ran in heat 2." The program makes use of the following function: FUNCTION fastest_time(ARRAY OF REAL LIST) RETURNS REAL DECLARE upper INITIALLY list() DECLARE min REAL FOR index FROM 1 TO (upper - 1) DO IF min < list[index] THEN SET min TO list[index] END IF RETURN min The function is used in the following section of code: SET heat1 TO [13.4, 14.1, 14.7, 10.8, 12.6] SET heat2 TO [11.5, 13.7, 10.1, 14.2, 12.9] SET first_result TO fastest_time(heat1) SET second_result TO fastest_time(heat2) IF first_result < second_result THEN SEND "The fastest runner ran in heat 1" TO DISPLAY ELSE SEND "The fastest runner ran in heat 2" TO DISPLAY END IF Testing reveals an error in the function - Scottish Highers Computing Science - Question 15 - 2018

Question icon

Question 15

When-a-user-presses-the-'Show-Results'-button,-the-program-should-output-the-number-of-the-heat-that-had-the-fastest-runner,-for-example:-----"The-fastest-runner-ran-in-heat-2."-----The-program-makes-use-of-the-following-function:-----FUNCTION-fastest_time(ARRAY-OF-REAL-LIST)-RETURNS-REAL-----DECLARE-upper-INITIALLY-list()-----DECLARE-min-REAL-----FOR-index-FROM-1-TO-(upper---1)-DO-------IF-min-<-list[index]-THEN---------SET-min-TO-list[index]-------END-IF-----RETURN-min-----The-function-is-used-in-the-following-section-of-code:-----SET-heat1-TO-[13.4,-14.1,-14.7,-10.8,-12.6]-----SET-heat2-TO-[11.5,-13.7,-10.1,-14.2,-12.9]-----SET-first_result-TO-fastest_time(heat1)-----SET-second_result-TO-fastest_time(heat2)-----IF-first_result-<-second_result-THEN-------SEND-"The-fastest-runner-ran-in-heat-1"-TO-DISPLAY-----ELSE-------SEND-"The-fastest-runner-ran-in-heat-2"-TO-DISPLAY-----END-IF-----Testing-reveals-an-error-in-the-function-Scottish Highers Computing Science-Question 15-2018.png

When a user presses the 'Show Results' button, the program should output the number of the heat that had the fastest runner, for example: "The fastest runner ran... show full transcript

Worked Solution & Example Answer:When a user presses the 'Show Results' button, the program should output the number of the heat that had the fastest runner, for example: "The fastest runner ran in heat 2." The program makes use of the following function: FUNCTION fastest_time(ARRAY OF REAL LIST) RETURNS REAL DECLARE upper INITIALLY list() DECLARE min REAL FOR index FROM 1 TO (upper - 1) DO IF min < list[index] THEN SET min TO list[index] END IF RETURN min The function is used in the following section of code: SET heat1 TO [13.4, 14.1, 14.7, 10.8, 12.6] SET heat2 TO [11.5, 13.7, 10.1, 14.2, 12.9] SET first_result TO fastest_time(heat1) SET second_result TO fastest_time(heat2) IF first_result < second_result THEN SEND "The fastest runner ran in heat 1" TO DISPLAY ELSE SEND "The fastest runner ran in heat 2" TO DISPLAY END IF Testing reveals an error in the function - Scottish Highers Computing Science - Question 15 - 2018

Step 1

Explain why line 4 of the function contains the limit (upper - 1).

96%

114 rated

Answer

The upper index of the array is defined as one less than the length of the array. This is because array indexing typically starts at 0, thus the highest valid index is 'upper - 1'. Using 'upper' directly would cause an index error.

Step 2

Describe how the parameters are used when executing line 23.

99%

104 rated

Answer

In line 23, 'heat1' is the actual parameter passed to the formal parameter 'list' in the function 'fastest_time'. 'list' acts as a pointer to the memory address of 'heat1'. Any changes to 'list' within the function affect 'heat1' externally since the actual parameter is directly referenced.

Step 3

State the scope of the min variable. Explain your answer.

96%

101 rated

Answer

The scope of the 'min' variable is local and it exists from lines 1/2 to lines 9/10, meaning it can only be accessed and modified within the function 'fastest_time' in which it is declared.

Step 4

Testing reveals an error in the function. The function is first called during execution of line 23 of the main program.

98%

120 rated

Answer

The values for 'min' during the watchpoint would be as follows:

  • Line 2: A = 13.4
  • Line 6: B = 14.5
  • Line 23: C = 17.4

Step 5

Identify the error in the function that causes incorrect output.

97%

117 rated

Answer

The error arises from using the wrong comparison operator as the if condition on line 25. Instead of checking 'first_result < second_result', it requires a logic check if 'first_result' should be greater or less, depending on the heat times.

Step 6

State the type of error that has caused this issue.

97%

121 rated

Answer

The type of error is logical; it lies in the incorrect operator usage leading to misinterpretation of the fastest heat.

Step 7

Explain why the incorrect code outputs the correct statement.

96%

114 rated

Answer

The incorrect operation may result in the unexpected truth value, but due to the particular values for heat times, it can still occasionally yield the correct output. Given the specific scenario where one heat clearly has a faster time, the results may coincide.

Step 8

If the fastest time in heat 1 and heat 2 is the same, the following output is always displayed.

99%

104 rated

Answer

The output 'The fastest runner ran in heat 2' is shown when the condition is evaluated as false/not met, leading to the else statement being executed.

Step 9

Explain how the code could be altered to include a third option which will state: "Both heats have the identical fastest time."

96%

101 rated

Answer

To include this third option, an additional if condition could be added to check if 'first_result' equals 'second_result' before the existing if condition at line 25. This would allow for the scenario where both heats have identical fastest times to be displayed accordingly.

Step 10

Explain the role of the memory management function of the operating system when a user loads the SportsStats program.

98%

120 rated

Answer

The memory management function tracks and checks available memory, allocates space/address in RAM for the function, and ensures that other processes do not interfere with this allocation.

Join the Scottish Highers students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;