Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Follow the program.
DECLARE Numbers : ARRAY[1:6] OF INTEGER
DECLARE Index : INTEGER
FOR Index ← 1 TO 6
Numbers[Index] ← Index * Index - 1
NEXT Index
OUTPUT Numbers[4] + Numbers[2]
OUTPUT Numbers[Numbers[2] + 1]What values does the array hold, and what are the two outputs?
DECLARE Name : ARRAY[Lower:Upper] OF TYPE makes a fixed-length array of one data type. An element is Name[Index], and the index may be a variable or an expression. A FOR loop from the lower bound to the upper bound visits every element. Pseudocode arrays usually start at 1; Python lists at 0.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The array. Numbers[1] to Numbers[6] hold 0, 3, 8, 15, 24 and 35.
Step 2. The first output. Numbers[4] + Numbers[2] is .
Step 3. The second output. The index is itself an expression. Numbers[2] is 3, so the index is , and Numbers[4] is 15.
Answer: 18, then 15.
Watch out. An index can be any expression that gives a whole number inside the bounds. Work it out first, then look up the element.
Log in to join the discussion.
Loading…