Exercise 5.3
- How many elements can be stored in each of the following arrays?
var a : array 1 .. 12, 1970 .. 1990 of real
var b : array 'a' .. 'j', 0 .. 20 of char
var c : array -5 .. 5, -5 .. 5 of int
var d : array 0 .. 4, 'A' .. 'E', 1985 .. 1989 of int
var e : array -2 .. 6, 0 .. 5 of char
- State the error(s) in each fragment.
var array 1 .. 10, 0 .. 4 of real
var low, high : int
var table : array low .. high, low .. high of char
var scores array -5 .. 10, 5 .. -10 of int
var matrix : 1 .. 10, 1..10 of real
- Suppose that an array a has been created by the declaration
var a : array 1 .. 2, 1 .. 4 of int
and that a has been given values by the following fragment:
var k : int := 0
for i : 1 .. 2
for j : 1 .. 4
k := k + 1
a(i, j) := k
end for
end for
Determine what will be written by each fragment.
for i : 1 .. 2
for decreasing j : 4 .. 1
put a(i, j):2 ..
end for
end for
for decreasing i : 2 .. 1
for decreasing j : 4 .. 1
put a(i, j):2 ..
end for
end for
for i : 1 .. 4
for j : 1 .. 2
put a(j, i):2 ..
end for
end for
for decreasing j : 4 .. 1
for i : 1 .. 2
put a(i, j):2 ..
end for
end for
- Write one or more statements to perform each operation.
- Declare an array containing three rows (numbered one to three) and five columns (numbered one to five) of integers.
- Set all the elements of the array to zero.
- Find the sum of all the elements in the array and store this sum in grandTotal.
- Find the sum of the elements in the second row and store this value in row2sum.
- Find the sum of the elements in the third column and store this value in col3sum.
- Find the sum of all negative elements in the array and store this value in negSum.
- Replace each element by its square.
- Find the largest value in the array and store this value in largest.
- A magic square is a square array of numbers in which the sums of the rows, the columns, and the diagonals are all equal. Write a program that reads sixteen integers into a 4 x 4 array and determines whether or not they form a magic square.
|
|