Representing algorithms

EasyMediumHard
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
Question 40
Medium

An algorithm in pseudocode is designed to display and sum the daily shipment counts (5 working days per hub) for 4 different regional hubs.

1  hubs ← ['Alpha', 'Beta', 'Gamma', 'Delta']
2  shipments ← [105, 112, 98, 115, 120, 88, 92, 85, 90, 99, 134, 142, 128, 130, 135, 76, 80, 82, 79, 85]
3  totalShipments ← 0
4  FOR i ← 0 TO 3
5      hubName ← hubs[i]
6      OUTPUT 'Hub: ', hubName
7      FOR j ← 0 TO 4
8          val ← shipments[i * 4 + j]
9          OUTPUT 'Day: ', j + 1, ' - ', val
10         totalShipments ← totalShipments + val
11     ENDFOR
12 ENDFOR

How could the logic error in this algorithm be corrected so that the correct shipment data for each day and hub is accessed?

Change line number 4 to: FOR i←0 TO 4\text{FOR } i \leftarrow 0 \text{ TO } 4FOR i←0 TO 4

Change line number 7 to: FOR j←0 TO 3\text{FOR } j \leftarrow 0 \text{ TO } 3FOR j←0 TO 3

Change line number 8 to: val←shipments[i∗5+j]\text{val} \leftarrow \text{shipments}[i * 5 + j]val←shipments[i∗5+j]

Change line number 8 to: val←shipments[j∗4+i]\text{val} \leftarrow \text{shipments}[j * 4 + i]val←shipments[j∗4+i]

Representing algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Representing algorithms