Structured Programming (AQA GCSE Computer Science)

Exam Questions

25 mins8 questions
11 mark

Figure 9 shows a subroutine represented using pseudocode.

Figure 9

SUBROUTINE calculate(n)
   a ← n
   b ← 0 
   REPEAT
     a ← a DIV 2
     b ← b + 1
   UNTIL a ≤ 1
   OUTPUT b
ENDSUBROUTINE

The DIV operator is used for integer division.

State the value that will be output for the subroutine call calculate(1)

Did this page help you?

21 mark

A local variable called size has been used in getSize.

Explain what is meant by a local variable in a subroutine.

Did this page help you?

12 marks

Explain one advantage of the structured approach to programming.

Did this page help you?

22 marks

A REPEAT…UNTIL iteration structure was used in Figure 9.

Figure 9

SUBROUTINE calculate(n)
   a ← n
   b ← 0
   REPEAT
     a ← a DIV 2
     b ← b + 1
   UNTIL a ≤ 1
   OUTPUT b
ENDSUBROUTINE

Figure 10 shows another subroutine called calculate that uses a WHILE…ENDWHILE iteration structure.

Figure 10

SUBROUTINE calculate(n)
   a ← n
   b ← 0
   WHILE a > 1
        a ← a DIV 2
        b ← b + 1
   ENDWHILE
   OUTPUT b
ENDSUBROUTINE

One difference in the way the subroutines in Figure 9 and Figure 10 work is:

  • the REPEAT…UNTIL iteration structure in Figure 9 loops until the condition is true

  • the WHILE…ENDWHILE iteration structure in Figure 10 loops until the condition is false.

Describe two other differences in the way the subroutines in Figure 9 and Figure 10 work.

Did this page help you?

3a6 marks

The size of a sound file is calculated using the following formula:

size (in bits) = sampling rate sample resolution seconds

To calculate the size in bytes, the number is divided by 8

The algorithm in Figure 12, represented using pseudocode, should output the size of a sound file in bytes that has been sampled 100 times per second, with a sample resolution of 16 bits and a recording length of 60 seconds.

A subroutine called getSize has been developed as part of the algorithm.

Complete Figure 12 by filling in the gaps using the items in Figure 11.

You will not need to use all the items in Figure 11.

Figure 11

bit

byte

getSize

OUTPUT

rate

res

RETURN

sampRate

seconds

size

size + 8

size * 8

size / 8

size MOD 8

SUBROUTINE

USERINPUT

Figure 12

SUBROUTINE getSize(____________, ____________, seconds)

         ______________ ← sampRate  *   res  *    seconds

         size ← ______________

         ______________ size

ENDSUBROUTINE

OUTPUT ______________(100, 16, 60) 
3b3 marks

State three advantages of using subroutines.

Did this page help you?

42 marks

State two benefits to the developer of using the three separate subroutines described in Figure 2 instead of writing the program without using subroutines.

Figure 2

Subroutine

Purpose

getSuit(n)

Returns:

  • the string 'hearts' if n is 0

  • the string 'diamonds' if n is 1

  • the string 'spades' if n is 2

  • the string 'clubs' if n is 3.

getRank(n)

Returns the number value of the card as a string, for example:

  • if n is 1 then 'ace' is returned

  • if n is 2 then 'two' is returned

  • if n is 10 then 'ten' is returned

  • if n is 11 then 'jack' is returned.

convert(cards)

Returns the complete string representation of the array cards.

For example:

  • if cards is [3, 1], the string returned would be 'three of diamonds '

  • if cards is [1, 0, 5, 2, 7, 0], the string returned would be 'ace of hearts five of spades seven of hearts '.

Did this page help you?

12 marks

State two benefits of developing solutions using the structured approach.

Did this page help you?

2a1 mark

A developer is writing a program to convert a sequence of integers that represent box playing cards to Unicode text.

The developer has identified that they need to create the subroutines shown in Figure 2 to complete the program.

Figure 2

Subroutine

Purpose

getSuit(n)

Returns:

  • the string 'hearts' if n is 0

  • the string 'diamonds' if n is 1

  • the string 'spades' if n is 2

  • the string 'clubs' if n is 3.

getRank(n)

Returns the number value of the card as a string, for example:

  • if n is 1 then 'ace' is returned

  • if n is 2 then 'two' is returned

  • if n is 10 then 'ten' is returned

  • if n is 11 then 'jack' is returned.

convert(cards)

Returns the complete string representation of the array cards.

For example:

  • if cards is [3, 1], the string returned would be 'three of diamonds '

  • if cards is [1, 0, 5, 2, 7, 0], the string returned would be 'ace of hearts five of spades seven of hearts '.

Explain how the developer has used the structured approach to programming.

2b5 marks

Figure 3 shows the subroutine convert described in Figure 2.

Some parts of the subroutine have been replaced with the labels L1 to L5.

Figure 3

SUBROUTINE convert(cards)
   result ← ''
   max ← LEN(cards)
   index ← 0
   WHILE index < L1
     rank ← L2(cards[index])
     suit ← getSuit(cards[L3 + 1])
     c ← rank + ' of ' + suit + ' '
     result ← result + L4
     index ← index + 2
   ENDWHILE
   RETURN L5
ENDSUBROUTINE

State the pseudocode that should be written in place of the labels in the subroutine written in Figure 3.

Did this page help you?