Professional Documents
Culture Documents
I. Motivations
II. Deendition of the array structure
III. Part I: single-dimentional array (vector)
a) Declaration
b) Algorithms on vectors
IV. Part II: Two-dimentional array (Matrix)
a) Declaration
b) Algorithms on matrix
I/ Deendition
● Arrays are a very common data structure, and they are used in a wide variandy
of programming applications. For example, arrays can be used to store lists of
numbers, strings, or objects.
II/ ARRAY PROPERTIES
Syntax: Idf-Array[<index>];
● Access to a vector element is done using the square brackand operator []. To
access an element we use a specific index,
Remark:
• Attempting to access an element that does not exist will result in an error for
the program
Syntax: Idf-Array[<constant>];
Example
b) Access to a vector element using a variable
Syntax: Idf-Array[<idf-variable>];
Syntax: Read(Idf-Array[<index>]);
• Allows you to enter a value by keyboard and store it by the element of the
vector which is indicated by the index value.
Remark
Syntax: write(Idf-Array[<index>]);
• it does allow you to display on the screen the element of the vector
designated by the index.
Remark
For i ← 1 To N Do
Read V[i]);
Done;
i←1;
While (i<= N) do
if V[i]>0 then write(V[i]);
Eif;
i←i+1;
done;
End.
Exercise
● Consider two vectors, V1 and V2, each containing N elements (where N <=
50). Write an algorithm that displays the elements that are common to V1 and
V2.
Algorithm Ex2;
Var V1, V2, V3: Array[50] Integer;
N, k, i: Integer; B:boolaen;
Begin
Repeat Read(N); To (N>=1) and (N<=50);
For i ← 1 To N Do
Read(V1[i]); Read(V2[i]);
Done;
i←1; k←0;
While (i<= N) do
j←1;
B← False;
While (j<= N) and (B=false) do
if V1[i]=V2[j] then B← True;
k←k+1;
V3[k] ←V1[i] ;
Eif;
j←j+1;
done;
i←i+1;
done;
For i ← 1 To k do
write(V3[i]); done;
End.
SORTING ALGORITHMS FOR ARRAY
1) SORTING ALGORITHMS for array
• Formally
• Input: A sequence of n numbers <a1,a2,…,an>
• Output: A reordering <a1,a2,…,an> of the sequence such that a1 ≤ a2 ≤ … ≤ an
• Given the input <6, 3, 1, 7>, the algorithm should produce
<1, 3, 6, 7>
a) sort by selection
This is a successive sort. For a given position, the Min element to be placed
there is selected by traverising the array from left to right
Example i=1 i=2 i=3 i=4 i=5 i=6
Sorting by permutation consist by generating all possible permutations of the input vector and
then randurning the permutation that is sorted. This means that it tries all possible solutions until it
endds the correct one.
Example
We want to classify the first element i=1 101 115 30 63 47 20
30 115 101 63 47 20
20 115 101 63 47 30
20 115 101 63 47 30
20 63 115 101 30 47
20 30 115 101 63 47
Example
We want to classify the element i=3 20 30 115 101 63 47
20 30 101 115 63 47
20 30 63 115 101 47
20 30 47 115 101 63
20 30 47 101 115 63
The basic operations in the Arrays are insertion, deletion, searching, and
update. These operations are usually performed to modify the data in the
array.
II/ INSERTION ALGORITHMS IN VECTORS
II/ Insertion operation
special cases
there are three special cases for vector insertion:
• If the insertion position is less than 1 or greater than the vector size plus 1, insertion is impossible.
• If the insertion position is equal to the vector size plus 1, insertion is possible at the end of the
vector.
a) Inserting a value into an unsorted vector
Example
a) Example
a) Example
Example
Write an algorithm to delete an integer val value into a sorted vector T of N
integers (N<=50) .
Example
Write an algorithm to search an integer val value into an unsorted vector T
of N integers (N<=50) .
First solution
Algorithm search1;
Var T: Array[50] integer;
N, Nb, val, i : integer;
Begin
Repeat
Read(N);
until (N>=1) and (N<=50);
For i ← 1 to N do
Read(T[i]);
done;
i←1;
Read(val);
Nb←0;
For i ← 1 to N do
If (val= T[i]) then Nb ← Nb +1;
Eif;
done;
If Nb= 0 then write(val, " does not exist in the vector ");
else write(val, "exist in the vector ");
Eif;
END.
Second solution
Algorithm search2;
Var T: Array[50] integer;
N, val, i : integer;
B: Boolean;
Begin
Repeat Read(N); until (N>=1) and (N<=50);
For i ← 1 to N do
Read(T[i]);
done;
i←1; Read(val);
B←False;
while (i<= N) and (B=False) do
If (val= T[i]) then B←True;
else i ←i+1;
Eif;
done;
If B= False then write(val, " does not exist in the vector ");
else write(val, "exist in the vector ");
Eif;
END.
a) Searching a value into a sorted vector
Example
Write an algorithm to search an integer val value into a sorted vector T of N
integers (N<=50) .
Sequential search
1. Principe
45
Solution
Algorithm search1;
Var T: array[50] integer;
N, val, i : integer;
begin
Repeat read(N);
until (N>=1) and (N<=50);
for i ← 1 a N do
read(T[i]);
done;
read(val);
If ((val< T[1]) or (val> T[N])) then write(val, " not exist");
else
i ← 1;
while (val >T[i]) and (i<=N) do
i← i+1;
done;
If T[i]=val then write(val, " exist");
else
write(val, " not exist");
EIf;
Eif;
END. 46
Dichotomous search
Principe
47
Solution
Algorithm search2;
Var T: array[50] integer;
N, Binf, Bsup, mid, val, i : integer;
B: Boolean;
begin
Repeat read(N); until (N>=1) and (N<=50);
for i ← 1 to N do read(T[i]); done;
read(val);
If ((val< T[1]) or (val> T[N])) then write(val, " not exist");
else Binf ← 1; Bsup ← N; B← False;
while (Binf <= Bsup ) and (B= False) do
mid←(Binf+Bsup) Div 2;
If T[mid]=val then B←True;
else
If T[mid] >val then Bsup ← mid-1; else Binf ← mid+1;
EIf;
EIf;
done;
EIf;
If val= True then write(val, " exist");
else write(val, " not exist"); EIf;
END. 48
Two-dimentional array
(Matrix)
49
DEendITION...
● A two-dimensional table, also called a matrix, is a data structure
that allows you to represent, on several rows and columns, a set of
values of the same data type.
● A matrix has a fixed size, which means that the number of rows and
columns cannot be changed after the matrix is created.
● A matrix may not be completely filled, but it will never contain more
elements than the number expected when it was declared.
Remark
The indices which are used to designate the elements of a matrix can be expressed directly as a
constant, but they can also be a variable, or a calculated expression.
a) access to an element of the matrix using a constant
Idf_Tab[constant1, constant2];
Allows access to the matrix element indicated by the row number expressed by
value of constant1 and the column number expressed by value of constant2.
example
Var T : Array[4,5] real;
Idf_Tab[Idf_var1, Idf_var2];
Idf_Tab[expression1, expression2];
• Allows you to enter a value by keyboard and store it by the element of the
vector which is indicated by the value of the row index and the column index.
Remark
• Display a value which is indicated by the value of the row index and the
column index.
Remark
Example:
The elements of the main diagonal are: 1 3 4 6
T[1,1], T[2,2], T[3,3], T[4,4]
3 3 8 7
4 8 5 9
6 4 9 6
The diagonals of a matrix
A square matrix has two diagonals.
A secondary diagonal: these elements are found in T[i,N-i+1].
For i-1..N
Example:
The elements of the secondary diagonal 1 3 4 6
are:
3 3 8 7
T[1,4], T[2,3], T[3,2], T[4,1]
4 8 5 9
6 4 9 6
Exercise:
71
Algorithm example;
Var T: Array [50, 50] integer;
N, i, j, S1, S2: integer;
Begin
Repeat Read(N); until(N>=1) and (N<=50);
S1← 0; S2← 0;
For i ← 1 to N do
For j ← 1 to N do
Read(T[i,j]);
done;
done;
For i ← 1 to N do
S1 ←S1+T[i, i]; /* The sum of the main diagonals
done;
For i ← 1 to N do
S2 ←S2+T[i,N-i+1]; /* The sum of the secondary diagonals
done;
If S1=S2 then write(‘’ The sum of the two diagonals is equal’’);
else write(‘’The sum of the two diagonals is not equal’’);
Eif;
end.
72
Identity matrix
● The identity matrix is a square matrix with n rows and n
columns. All elements of its main diagonal are equal to 1
and all other elements are equal to 0.
● Example :
Exercise:
We consider a square matrix M of size (N<50).
3 3 7 7
4 7 5 9
6 7 9 6
Exercise:
We consider a square matrix M of size (N<50) .
write an algorithm to check whether an M matrix is Symmetrical or not.
Algorithm exo;
Var M: array[50, 50] integer;
N, i, j: integer;
B: Booleen;
Begin
Repeat Read(N); Read(M); until(N>=1) et (N<=50);
For i ← 1 to N do
For j ← 1 to N do Read(M[i,j]); done;
done;
B ←True; i ←1;
while (i<= N) and (B ←True) do
j ←1;
while (j<= N) et (B ←True) do
If (i<>j) and (M[i,j] <> M[j,i]) then B ←False;
Eif;
j ←j+1;
done;
i ←i+1;
done;
If (B =True) then write(" Matrix is Symmetrical");
else write(" Matrix is not Symmetrical ");
Fsi;
end.