Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A weather app stores one temperature in degrees Celsius for each day of a week.
DECLARE DailyTemp : ARRAY[1:7] OF REAL
DECLARE Day : INTEGER
FOR Day ← 1 TO 7
OUTPUT "Temperature for day ", Day
INPUT DailyTemp[Day]
NEXT DayHow many elements does the array have, and what are its lowest and highest index? Write one more statement that outputs the temperature of the last day.
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 bounds. ARRAY[1:7] has the lower bound 1 and the upper bound 7, so there are 7 elements, DailyTemp[1] to DailyTemp[7].
Step 2. The new statement.
OUTPUT DailyTemp[7]Why REAL. A temperature such as 21.5 has a fractional part, so the array is of type REAL. The loop counter Day is the index, so one INPUT line fills all seven elements.
Answer: 7 elements, from index 1 to index 7.
Log in to join the discussion.
Loading…