A programmer has written an algorithm to output a series of numbers - OCR - GCSE Computer Science - Question 2 - 2018 - Paper 1
Question 2
A programmer has written an algorithm to output a series of numbers. The algorithm is shown below:
for k = 1 to 3:
for p = 1 to 5:
print (k + p)
nex... show full transcript
Worked Solution & Example Answer:A programmer has written an algorithm to output a series of numbers - OCR - GCSE Computer Science - Question 2 - 2018 - Paper 1
Step 1
Give the first three numbers that will be printed by this algorithm.
96%
114 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
To determine the first three numbers printed by the algorithm, we need to analyze the nested loops:
When k = 1, the inner loop will execute 5 times with p values ranging from 1 to 5:
When p = 1: print(1 + 1) → 2
When p = 2: print(1 + 2) → 3
When p = 3: print(1 + 3) → 4
When p = 4: print(1 + 4) → 5
When p = 5: print(1 + 5) → 6
The first three outputs will be: 2, 3, and 4.
Step 2
State how many times line 03 will be executed if the algorithm runs through once.
99%
104 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Line 03 executes for each iteration of the inner loop. Since k ranges from 1 to 3 (3 iterations) and p ranges from 1 to 5 (5 iterations), the total number of executions of line 03 is:
Total Executions = 3 (values of k) * 5 (values of p) = 15.
Step 3
Identify two basic programming constructs that have been used in this algorithm.
96%
101 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
Sequence - The algorithm follows a linear sequence of steps.
Iteration - The use of 'for' loops to repeat a block of code.
Step 4
Describe what is meant by a variable.
98%
120 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!
Answer
A variable is a symbolic name associated with a memory location that can hold data. It allows programmers to store and manipulate values during program execution. The value assigned to a variable can change throughout the execution of the program.
Step 5
Identify two variables that have been used in the algorithm above.
97%
117 rated
Only available for registered users.
Sign up now to view full answer, or log in if you already have an account!