Exercise 5.1
- A portion of the table of atomic masses of the elements according to atomic number is shown below.
Number | 1 | 2 | 3 | 4 | 5 |
Atomic Mass | 1.01 | 4.00 | 6.94 | 9.01 | 10.81 |
Suppose that this was stored as an array called atomicMass.
- What is the value of atomicMass(3)?
- What is the value of atomicMass(5)?
- What is the name of the element whose value is 4.00?
- What is the name of the element whose value is 1.01?
- What are the indices of the array?
- Of what type are the indices?
- Of what type are the elements?
- A substitution cipher is a table that maps each letter of the alphabet onto another letter. Consider a simple substitution cipher table in which each letter maps onto the letter three places further in the alphabet ("wrapping around" at the end of the alphabet) as shown.
Letter | 'A' | 'B' | 'C' | ... | 'Y' | 'Z' |
Cipher | 'D' | 'E' | 'F' | ... | 'B' | 'C' |
Suppose that this was stored as an array called cipher.
- What is the value of cipher('P')?
- What is the name of the element whose value is 'E'?
- What is the name of the element whose index is 'D'?
- What is the value of the element whose index is 'W'?
- What is the index of the element whose value is 'C'?
- What index gives a cipher of 'L'?
- What is the type of the indices?
- What is the type of the elements?
- How many elements can be stored in each of the following arrays?
var a : array 1980 .. 1990 of real
var b : array -10 .. 10 of int
var c : array 'a' .. 'h' of int
var d : array -5 .. 5 of boolean
var e : array false .. true of char
- Write declarations to create arrays for storing the information in the following tables:
Time (h) | 0 | 1 | 2 | ... | 12 |
Temperature (oC) | 11 | 14 | 16 | ... | -7 |
Question | 1 | 2 | 3 | ... | 10 |
Mark | 5 | 5 | 10 | ... | 8 |
Size | 'A' | 'B' | 'C' | 'D' |
Quantity | 240 | 185 | 210 | 375 |
- Identify, with reasons, any of the following declarations that are illegal
const FIRST := 'A'
const LAST := 'J'
var a : array -20 .. -15 of int
var b : array 'p .. 'k' of char
var c : array FIRST .. 'E' of real
var d : array 1 .. LAST of boolean
var e : array FIRST .. LAST - 1 of string
var f : array 1 .. 1e1 of int
var g : array 1 .. 5 of int := (1, 2, 3, 4, 5)
var h : array 5 .. 8 of string := init("one", "two", "three", "four")
var i : array 2 .. 5 of int := init(1, 2, 3)
|
|