Tutorial 5 Arrays
Tutorial 5 Arrays
Exercise 1
• What does the following algorithm produce?
Algorithm 1: Exercise
1 Algorithm Exercise 01;
2 type Tab=Array [1..5] of Integer;
3 Var T:Tab;
4 i: integer;
5 begin
6 for i ← 1 à 5 do
7 T [i] ← i ∗ i;
8 EndFor
9 for i ← 1 à 5 do
10 Display(T [i]);
11 EndFor
12 end
Exercise 2
Write the algorithms that allow to:
1. Declare and fill an array of 7 numeric values, setting them all to zero.
2. Declare an array of 9 notes, whose values are then entered by the user.
3. Declare and fill in an array containing the six vowels of the Latin alphabet.
4. Compute the average and minimum of the elements of an array.
5. Test if an array is sorted.
6. Calculate the number of occurrences of an item and its position in an array.
Pn
7. Calculate the scalar product of two real vectors u and v of dimension n:u.v = i=1 ui ∗ vi
u= 2 8 3 6 0 7 5
v= 1 6 2 3 1 5 10
u*v= 2 48 6 18 0 35 50
Exercise 3
Write the algorithm performing the shifting of the elements of an array.
Initial table:
T A B L E A U
Table shifted to the left:
A B L E A U T
Exercise 4
Write a program that transfers a two-dimensional L and C array M (maximum dimensions: 10 rows and 10
columns) into a one-dimensional L*C array V.
Exercise 5
1. Write an algorithm that displays any matrix (2-dimensional array) line by line.
2. Write an algorithm that performs the product of 2 matrices represented by 2-dimensional arrays.
B : p rows q columns
b11 b12 ... b1q
b21 b22 ... b2q
2
b1
×
.. .. .. ..
1
+
a2
.
2
. . .
b2
×
2
a2
bp1 bp2 ... bpq
..
.+
b p2
×
p
a2
a11 a12 ... a1p c11 c12 ... c1q
a21 a22 ... a2p ;
c21 c22 ... c2q
.. .. .. .. .. ..
.. ..
. . . .
. . . .
an1 an2 ... anp cn1 cn2 ... cnq
3. Write an algorithm that displays all cells surrounding a given cell (i,j) in a matrix. The algorithm must
not cause an error in the case where the cell (i,j) is on the edges of the matrix (1era or last row and/or
1era column and/ or last column).