You are on page 1of 81

Array Data Structure

Course Material for First-Year


Computer Science Students
WorkPlan

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

● An array is a data structure that stores a collection of elements of the same


data type in contiguous memory locations.

● 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

● Arrays is a fixed data structure. Once an array is declared, it cannot be


changed.
● Each element in the array can be accessed by its position in the array, which is
known as its index.
● The index of an array must always be of ordinal type.
● An array may not be completely filled, but it will never contain more elements
than the number specified in the declaration.
● An array is never accessed in its entirely, but element by element.
III. Single-dimentional array (vector)
III. Single-dimentional array (vector)
1) Declaration

● Idf_Tab : Array [Nb-elements] Type;

● Idf_Tab: Name of the array, which is its identifier.


● Type: type of values contained in the array.
● Nb-elements : represents the number of elements contained in the array.
Example

Var T : Array[5] integer;

● This means: Reserve a memory space containing 5 integer numbers.


● These elements are stored in the vector T.
● The index of the first element is at position number 1
● The index of the last element is at position number 5
2) Access to a vector element

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:

• The index can be expressed as a constant, a variable, or a calculated


expression

• Attempting to access an element that does not exist will result in an error for
the program

• Acceding an uninitialized cell can lead to runtime errors.


a) Access to a vector element using a constant

Syntax: Idf-Array[<constant>];

• The index can be expressed as a constant

Example
b) Access to a vector element using a variable

Syntax: Idf-Array[<idf-variable>];

• The index can be expressed as a variable


Example
c) Access to a vector element using a calculated expression

Syntax: Idf-Array[<calculated expression>];

• The index can be expressed as a calculated expression


Example
3) Input Action

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

Reading a vector is never done in its entirely, but element by element.


4) Output Action

Syntax: write(Idf-Array[<index>]);

• it does allow you to display on the screen the element of the vector
designated by the index.

Remark

Display a vector is never done in its entirely, but element by element.


Exercise
● Consider a vectors, V, each containing N elements (where N <= 50). Write an
algorithm that displays the positives elements of a vector V.
Algorithm Ex1;
Var V: Array[50] Integer;
N, i: Integer;
Begin
Repeat
Read(N);
until (N>=1) and (N<=50);

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

Sorting is a process that rearranging the values in an array or collection into a


specific order (ascending or descending order).

• 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

 i=1 PosMin=6 permutation T[1] and T[6] 101 115 30 63 47 20

 i=2 PosMin=3 permutation T[2] and T[3] 20 115 30 63 47 101

 i=3 PosMin=5 permutation T[3] and T[5] 20 30 115 63 47 101

 i=4 PosMin=4 no have permutation because i=PosMin 20 30 47 63 115 101

 i=5 PosMin=6. permutation T[5] and T[6] 20 30 47 63 115 101

 Sorted array 20 30 47 63 101 115


Algorithm sort1;
Var V: array[50] integer;
N, i, j, PosMin, z: integer;
Begin
Repeat Read(N); until (N>=1) and(N<=50);
For i ← 1 to N do
Read(V[i]);
done;
For i ← 1 to N-1 do
PosMin ←i ;
For j ←(i+1) to N do
if V[j]<V[PosMin] then PosMin ← j;
Eif;
done;
if i <> PosMin then z ← V[PosMin] ;
V[PosMin] ← V[i] ;
V[i] ← z ;
Eif;
done;

write(‘’Sorted array ’’);


For (i ← 1 to N do
write(V[i]);
done;
END.
b) Sorting by permutation

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

We want to classify the second element i=2 20 101 115 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

We want to classify the element i=4 20 30 47 115 101 63

20 30 47 115 101 63

20 30 47 101 115 63

We want to classify the element i=5 20 30 47 63 115 101

Sorted array 20 30 47 63 101 115


Algorithm sorted2;
Var V: array[50] integer;
N, i, j, z: integer;
Begin
Repeat Read(N); until (N>=1) and(N<=50);
For i ← 1 to N do
Read(V[i]);
done;
For i ← 1 To N-1 do
For j ←(i+1) To N do
if V[j] < V[i] then z ← V[i];
V[i] ← V[j];
V[j] ← z;
Eif;
done;
done;
write(‘sorted array’’);
For i ← 1 To N do
write(V[i]);
done;
END.
2) The basic operations

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

● The insertion operation adds one or more elements to an array.


● The new elements can be added at the beginning, end, or any given position in
the array.

special cases
there are three special cases for vector insertion:

• If the vector is full, insertion is impossible.

• 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

Write an algorithm allowing the insertion at a given position k, of an integer val


value into an unsorted vector T of N integers (N<=50).
Algorithm insertion;
Var T: array[50] integer;
N, val, i, k: integer;
Begin
Repeat Read(N); until (N>=1) and (N<=50);
For i ← 1 to N do
Read(T[i]);
done;
If (N=50) then write ("Insertion into a full vector is impossible.“);
else
Read(k);
If (k<1) ou (k> N+1) then write("bad position, Insertion is impossible");
else Read(val);
write(" Right shift of elements from position k to N ");
i←N;
while (i>= k) do
T[i+1] ←T[i];
i←i-1;
done;
T[k] ←val;
N ←N+1;
For i ← 1 to N do
write(T[i]); done;
EIf;
EIf;
END.
b) Inserting a value into a sorted vector

a) Example

Write an algorithm to insert an integer val value into a sorted vector T of N


integers (N<=50) while maintaining the sorted order:
Algorithm insertion2;
Var T: Array[50] integer;
N, val, i, k: integer;
Begin
Repeat read(N); until (N>=1) et (N<=50);
for i ← 1 to N do
read(T[i]);
done;
If (N=50) then write ("Insertion into a full vector is impossible.“ );
else write(" endding the insertion position ");
k←1; read(val);
while (k<= N) and (val> T[k]) do
k ←k+1;
done;
write(" Right shift of elements from position k to N ");
i←N;
while (i>= k) do
T[i+1] ←T[i];
i←i-1;
done;
write(" insert value ",val); T[k] ←val;
N ←N+1;
for i ← 1 to N do
write(T[i]); done;
Eif;
END.
III/ DELETION ALGORITHMS IN VECTORS
a) Delete a value into an unsorted vector

a) Example

Write an algorithm to delete an integer val value located at a position k, into a


unsorted vector T of N integers (N<=50) .
Algorithm delete1;
Var T: array[50] integer;
N, i, k: integer;
Begin
Repeat Read(N); until (N>=1) and (N<=50);
For i ← 1 to N do
Read(T[i]);
done;
write(" give deletion position ");
Repeat read(k); until (k>=1) and (k<=N);
write(" deletion vector elements with left shift from position k to N");
i←k+1;
while (i<= N) do
T[i-1] ←T[i];
i←i+1;
done;
N ←N-1;
write(" vector after deletion ");
For i ← 1 to N do
writeT[i]);
done;
END.
b) Delete a value into a sorted vector

Example
Write an algorithm to delete an integer val value into a sorted vector T of N
integers (N<=50) .

The vector must remain sorted after deleting val.


Algorithm deletion2;
Var T: array[50] integer;
N, val, i, k: integer;
Begin
Repeat Read(N); until (N>=1) and (N<=50);
For i ← 1 to N do Read(T[i]); done;
write(" endding the position of val in the vector ");
k←1; read(val);
while (k<= N) and (val> T[k]) do
k ←k+1;
done;
if (val<> T[k]) then write(val, " does not exist in the vector, deletion impossible ");
else write(" left shift of vector elements from position k to N");
i←k+1;
while (i<= N) do
T[i-1] ←T[i];
i←i+1;
done;
N ←N-1;
write(" vector after deletion of ", val);
for i ← 1 to N do
write(T[i]); done;
Eif;
END.
III/ SEARCH ALGORITHMS IN VECTORS
a) Search a value into an unsorted vector

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

1) The table is sorted in ascending order. The principle is as follows: the


search stops in the following cases:
2) If T[1]> val
3) If T[N] < val then search for val in the left part ofT[middle].
4) If an element of the array T is greater than val.

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

The principle is as follows:


compare the element with the value of the element in the middle of the
array;
if the values are equal, the value is found,
otherwise we start again in the relevant half of the array.
We have the following cases:

1) If T[middle]=val then val found


2) If T[middle] >val then search for val in the left part of T[middle].
3) If T[middle] >val then Search for val in the right-hand part of
T[milieu].

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 two-dimensional array is identified by the same name but each of


these values is identified by two indices indicating its location.
● Example:

15.00 5.25 5.25 1.25 6.00

-6.00 -4.00 2.00 -6.00 0.00

5.25 5.25 -6.00 1.00 8.50

7.00 5.75 -12.00 5.25 5.50


REMARKS...
● The values contained in a matrix can be simple (integer, character,
boolean) or structured (string, record, etc.).

● 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.

● The indices of a matrix must always be of ordinal type.


 Déclaration…

Idf_Tab : Array[NbRaw, NbCol] Type;

● Idf_Tab: Name of the array, which is its identifier.


● Type: type of values contained in the array.
● NbRaw : represents the number of rows in the array.
● NbCol : represents the number of columns contained in the
array.
Example
Var T : Array[4,5] real;

● This means: Reserve a memory space containing 4 rows and 5 columns of


real numbers.
● These real elements are stored in the vector T.
Schematic representation
Var T : Array[4,5] real ;

Schematically we can represent this array T by:

Column Indice j=1 j=2 j=3 j=4 j=5


15.00 5.25 5.25 1.25 6.00
i=1
-6.00 -4.00 2.00 -6.00 0.00
i=2
Row Indice
i=3 5.25 5.25 -6.00 1.00 8.50

i=4 7.00 5.75 -12.00 5.25 5.50


 Access to an element of the matrix
syntax
Idf_Tab[indiceRow,indicecol];
• Allows access to the matrix element, indicated by the row index
(indiceRow) and that of the column (indicecl).
•The two indices indicate the position of the element in the matrix.

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;

T[3,5] allows access to the element of the table T located at the


intersection of row 3 and column 5

15.00 5.25 5.25 1.25 6.00

-6.00 -4.00 2.00 -6.00 0.00

5.25 5.25 -6.00 1.00 8.50

7.00 5.75 -12.00 5.25 5.50


b) access to an element of the matrix using a variable

Idf_Tab[Idf_var1, Idf_var2];

Allows access to the table element indicated by the row number


expressed by value of Idf_variable1 and the column number
expressed by value of Idf_variable2.
 example
Var T : Array[4,5] real;
X ← 3; y ← 2;

T[X,Y] allows access to the element of the table T located at the


intersection of row 3 and column 2.

15.00 5.25 5.25 1.25 6.00

-6.00 -4.00 2.00 -6.00 0.00

5.25 5.25 -6.00 1.00 8.50

7.00 5.75 -12.00 5.25 5.50


b) access to an element of the matrix using an expression

Idf_Tab[expression1, expression2];

Allows access to the matrix element indicated by the result of


expression1 which will indicate the position of the element by the rows
and the result of expression2 which will indicate the position of the
element by the columns.
 example
Var T : array[4,5] real;
Si on considère une variable x avec la valeur 2 (x ← 2) et une variable
à la quelle on affecte la valeur 1 (y ← 1)

T[X+1,Y+1] allows access to the element of the table T located at the


intersection of row 3 and column 2.

15.00 5.25 5.25 1.25

-6.00 -4.00 2.00 -6.00

5.25 5.25 -6.00 1.00

7.00 5.75 -12.00 5.25


Example

 Consider a matrix with 40 rows and 30 columns. Write an


algorithm that displays the number of values greater than 10.
Assume the array is already filled.
solution
Algorithm exo;
Var i ,j, nb : integer;
notes: array[40,30] real ;
Begin
nb ←0;
For i ← 1 to 40 do
For j ← 1 to 30 do
If (notes[i,j] >10) then nb ←nb+1;
Eif;
done;
done;
write (" the number of values greater than 10 is : ", nb);
END.
 Input action
syntax:
Read (Idf_Tab [indiceLi, indicecol]);

• 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

Reading a vector is never done in its entirely, but element by element.


 Output action
syntax:
Write (Idf_Tab [indiceLi, indicecol]);

• Display a value which is indicated by the value of the row index and the
column index.

Remark

Display a vector is never done in its entirely, but element by element.


Exercise.

 write an Algorithm which allows you to enter the


elements of an array T with integers stored on 4 rows
and 5 columns.
SOLUTION
Algorithm Example;
var V: array[4,5] integer;
i, j : integer;
Begin
For i ←1 to 4 do
For j ←1 to 5 do
write(" Enter the integer element");
read(V[i,j]);
done;
done;
END.
Assignment instruction
It is performed between two array elements or with a
variable of the same type.
Syntax:

(Idf_Tab [indice1,indice2]) ← value;


Example:
1 3 4 6
T[2,3] 8
3 8 7
3
8.00 8 5 9
4
6 4 6
9
The diagonals of a matrix
A square matrix has two diagonals.
a) A main diagonal: If i=j thenT[i,j]

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:

•Write an algorithm that displays whether or not the sum of the


elements of the first diagonal is equal to the sum of the elements
of the second diagonal.
•We consider a matrix T of N integers (N<=50) .

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).

Write an algorithm that checks whether matrix M is an identity matrix


or not.
Algorithm identity;
Var M: array[50,50] integer;
N, i, j, k: integer;
B: Booleen;
Begin
Repeat Read(N);
until(N>=1) and(N<=50);
B ←True;
i ←1;
while (i<= N) and (B ←True) do
j ←1;
while (j<= N) and (B ←True) do
Read(M[i,j]);
If (i = j) and (M[i,j] <>1) then B ←False;
Eif;
if (i <> j) and (T[i,j] <>0) then B ←False;
Eif;
j ←j+1;
done;
i ←i+1;
done;
If (B =True) then write(" Matrix identity");
else write(" Matrix is not identity ");
Eif;
Triangular matrix
• An upper (respectively lower) triangular matrix is a square
matrix in which all terms "below" (respectively "above") the
main diagonal are zero.
Exercise:
We consider a square matrix M of size (N<50) .
write an algorithm to check whether an M matrix is upper triangular or
not.
Algorithm exo;
Var M: array[50, 50] integer;
N, i, j: integer;
B: Booleen;
Begin
Repeat Read(N); until(N>=1) and(N<=50);
For i ← 1 to N do
For j ← 1to N do Read(M[i,j]); done;
done;
B ←True; i ←1;
while (i<= N) and (B ←True) do
j ←1;
while (j<= N) and (B ←True) do

If (i>j) and (M[i,j] <>0) then B ←False;


Eif;
j ←j+1;
done;
i ←i+1;
done;
If (B =True) then write(" Matrix is upper triangular ");
else write(" Matrix is not upper triangular ");
Fsi;
end.
Symmetrical matrix
1 3 4 6

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.

You might also like