0% found this document useful (0 votes)
40 views68 pages

ASD-1-Algorithmic-dataStructures-1 (1) - 1-68

درس asd 1

Uploaded by

djeddidmohmos06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views68 pages

ASD-1-Algorithmic-dataStructures-1 (1) - 1-68

درس asd 1

Uploaded by

djeddidmohmos06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Algorithms and Data Structures

Course 4 : Static Data Structures

Prof. Hadda CHERROUN


Computer Science Department
Amar Telidji University Laghouat
[email protected]

15 November 2022
Updated December 2022

. . . .... .... .... . . . . .


Static Data Structure
Arrays
Strings & Record & File

Plan

1 Static Data Structure


Data Structure ?

2 Arrays
Declaration
Manipulation Arrays
Multi-dimensional Arrays

3 Strings & Record & File


Strings
Record/Structure
File

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 2/ 68


Static Data Structure
Arrays
Strings & Record & File

Objectives

1 Enlarge your Algorithmic knowledge by new data structures


2 Take aware about static data structures and their manipulations
3 discover Arrays, Strings, File, Record. . .

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 3/ 68


Static Data Structure
Arrays
Strings & Record & File

Details

1 One dimensional Arrays


2 Sort Algorithms (By insertion, by
Bubbles,. . . )
3 Two dimensional Arrays
4 Structures (Records)
5 Files

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 4/ 68


Static Data Structure
Arrays Data Structure ?
Strings & Record & File

Data Structure ?

Definition
A Data Structure is a Specialized Format for :
Organizing,
Processing,
data.
Retrieving
storing

There are several basic and advanced types of data structures,


all designed to arrange data to suit a specific purpose.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 5/ 68


Static Data Structure
Arrays Data Structure ?
Strings & Record & File

Types of most known Data Structures

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 6/ 68


Static Data Structure
Arrays Data Structure ?
Strings & Record & File

Another classification of Data Structures

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 7/ 68


Static Data Structure
Arrays Data Structure ?
Strings & Record & File

How to define a Data Structure ?

Abstract or logic Aspect −→


primitives and organization ;

Concrete Aspect −→ Physical


Representation ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 8/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Motivation

Problem : let 10 students, for each of them, we want to input and store their grade.
Then compute how many students has the average.
One solution can be expressed by using for each student a separate object (variable)
to store the mark !

Mark1, Mark2, Mark3, Mark4, Mark5, Mark6, Mark7, Mark8, Mark9, and Mark10.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 9/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Naive Solution
Algorithm : Success
Data : Mark1, Mark2, Mark3, Mark4, Mark5, Mark6, Mark7, Mark8, Mark9, Mark10 : 10 reals (marks)
Result : NB-success : a Integer that represents the number of succeeded students
/* Declaration */
Mark1, Mark2, Mark3, ... Mark10, success : real ;
success : integer ;
begin
Input(Mark1) ;
Input(Mark2) ;
Input(Mark3) ;
.
.
.
Input(Mark10) ;
success ← 0 ;
if Mark1≥10 then
success ← success +1 ;
endif
if Mark2 ≥ 10 then
success ← success +1 ;
endif
.
.
.
if Mark10 ≥ 10 then
success ← success +1 ;
endif
Output("the number of succeeded Student is", success) ;
end . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 10/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Solution

Solution −→ Array
Algorithmic Language (and Programming languages) offer the
possibility of gathering several values in a single data
structure called an array.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 11/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Definition

Defintion
An array is a collection of items stored at contiguous location.

The idea is to store multiple items of the same type


together.

It makes easier to access the position of each element by


simply adding an offset to a base value.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 12/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Declaration of Array

name-of-array : array [ 1..N ] ) of Type-elements ;

OR

name-of-array : array [ N ] ) of Type-elements ;

Examples

temperature : array [ 30 ] ) of real ;

Letters : array [ 26 ] ) of char ;

Notes : array [ 100 ] ) of real ;


. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 13/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

How to Access elements ?

An array is accessed through manipulating its elements


(components)
An element is accessed by its index.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 14/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Access to elements (2)

As any simple object an array element can appear in


Input/Output Action :
Input (A[5])
Output (A[i+3])
Assignment Action :
A[j] ← 100 ;
Z ← A[j+7] * A[m-9] ; /* figures in an expression

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 15/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Solution with an Array

Algorithm : Success-with-array
Data : Mark an array of 10 Integers
Result : NB-success : a Integer that represents the number of succeeded students
/* Declaration of Objects */
Mark : array [10] of real ;
success : integer ;
begin
success ← 0 ;
for i ← 1 to 10 do
Input( Mark[i] ) ;
if Mark[i] ≥ 10 then
success ← success +1 ;
endif
done
Output("The number of succeeded Student is", success) ;
end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 16/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Input and Output an array

Input and Output an Array

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 17/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Input and Output an array : Modules

/* Declaration of modules */ /* Declaration of modules */


Action Read-Array( T : array [100] of integer, N : Action Write-Array( T : array [100] of integer, N :
integer ) integer) ;
i : integer ; i : integer ;
begin begin
for i ← 1 to N do for i ← 1 to N do
Input( T[i] ) ; Output( T[i] ) ;
done done
end end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 18/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Input and Output an array : Algorithm


Algorithm : Bubble-Sort
Data : A : an array of N Integers
Result : A : input an array then Display it
/* Declaration of objects */
N : Integer ;
A : array [100] of integer ;
/* Declaration of modules */
Action Read-Array( T : array [100] of integer, N : integer )
i : integer ;
begin
for i ← 1 to N do
Input( T[i] ) ;
done
end
Action Write-Array( T : array [100] of integer, N : integer) ;
i : integer ;
begin
for i ← 1 to N do
Output( T[i] ) ;
done
end
begin
Input(N) ;
/* fill in the array’ elements */
Read-Array(A, N) ;
/* display the array’ elements */
Write-Array(A, N) ;
end . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 19/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Why is important to Sort Arrays ?

Pb : Search your Wilaya in this unsorted array !

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 20/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Why is important to Sort Arrays ?


Pb : Search your Wilaya in this unsorted array !

Adrar Tlemcen Constantine Tindouf

Chlef Tiaret Médéa Tissemsilt

Laghouat Tizi Ouzou Mostaganem El Oued

Oum El Bouaghi Alger M’Sila Khenchela

Batna Djelfa Mascara Souk Ahras

Béjaïa Jijel Ouargla Tipaza

Biskra Sétif Oran Mila

Béchar Saïda El Bayadh Aïn Defla

Blida Skikda Illizi Naâma

Bouira Sidi Bel Abbès Bordj Bou Arreridj Aïn Témouchent

Tamanrasset Annaba Boumerdès Ghardaïa

Tébessa Guelma El Tarf Relizane

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 21/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Why is important to Sort Arrays ?


Pb : Search your Wilaya in this Sorted array ⌣

Adrar Boumerdès Laghouat Sétif

Aïn Defla Chlef Mascara Sidi Bel Abbès

Aïn Témouchent Constantine Médéa Skikda

Alger Djelfa Mila Souk Ahras

Annaba El Bayadh Mostaganem Tamanrasset

Batna El Oued M’Sila Tébessa

Béchar El Tarf Naâma Tiaret

Béjaïa Ghardaïa Oran Tindouf

Biskra Guelma Ouargla Tipaza

Blida Illizi Oum El Bouaghi Tissemsilt

Bordj Bou Arreridj Jijel Relizane Tizi Ouzou

Bouira Khenchela Saïda Tlemcen

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 22/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Primitive : Sorting an Array

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 23/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sorting an Array

Definition
A sorting algorithm puts elements of an array or a list into an
order.
The most frequently used orders are :
numerical : 1 12 23 50
lexicographical "Bird" "Dolphin" "Elephant" "Zebra"

The order can be either


Ascending. ↗
Descending. ↘

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 24/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Popular Sort Algorithms

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 25/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Array Bubble Sort : Principle

1 It compares the first two elements, and if the first is greater


than the second, it swaps them.
2 It continues doing this for each pair of adjacent elements to
the end of the data set.
3 It then starts again with the first two elements, repeating
until no swaps have occurred on the last pass

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 26/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sort by Bubble : Illustration

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 27/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sort by Bubble : Module

Action Sort-Bubble (A : array [100] of integer, N : integer) ;


i : integer ;
Change : Boolean ;
begin
repeat
i ← 1;
Change ← false ;
while ( i ≤ (N-1) ) do
if A[i+1] ≤ A[i] then
swap( A[i+1], A[i] ) ;
Change = true ;
endif
done
until (Change = false) ;
end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 28/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sort by Bubble : Algorithm


Algorithm : Bubble-Sort
Data : A : an array of N Integers
Result : A : the array A within sorted elements
/* Declaration of objects */
A : array [100] of integer ;
N, i : integer ;
Change : Boolean ;
/* Declaration of modules */
Read-Array( T : array [100] of integer, N : Integer) ;
Write-Array( T : array [100] of integer, N : Integer) ;
swap(A :integer, B integer ) ;
begin
Input(N) ;
Read-Array(A, N) ;
repeat
i ← 1;
Change ← false ;
while ( i ≤ (N-1) ) do
if A[i+1] ≤ A[i] then
swap( A[i+1], A[i] ) ;
Change = true ;
endif
done
until Change = false ;
Write-Array(A, N) ;
end
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 29/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Searching an array : Problem

Given an array A and an element e.


One of the basic operations to be performed on an array
is searching.
Definition
Searching an array means to find a particular
element in the array.
The search can be used to return the position of the
element (Output is an index).
Or simply check if it exists in the array (Output is
Boolean).

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 30/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Two Categories of Search

Based on the type of search operation, these algorithms are


generally classified into two categories :

Sequential/linear Search : the array is traversed sequentially


and every element is checked.

Interval Search : Here the searching is specifically designed


for searching in sorted arrays.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 31/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sequential Search : Principle

1 Start from the leftmost element of the array A and one by


one compare x with each element of A
2 If x matches with an element, return the index.
3 If x doesn’t match with any of the elements, return -1.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 32/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sequential Search : Function

/* Declaration of modules */
Action Search ( T : array [100] of integer, e : integer) : integer ;
i : integer ;
Found : boolean ;
begin
Search ← -1 ;
Found ← false ;
while (i ≤ N) and ( not Found) do
if (T[i] = e) then
Search ← i ;
Found ← true ;
endif
i← i+1 ;
done
end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 33/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Sequential Search : Algorithm


Algorithm : Linear Search
Data : A : an array of N Integers
x : the searched element
Result : ind : the position where x is found (or -1 Else)
/* Declaration of objects */
A : array [100] of integer ;
x, i : integer ;
/* Declaration of modules */
Action Search ( T : array [100] of integer, e : integer) : integer
i : integer ;
Found : boolean ;
begin
Search ← -1 ;
Found ← false ;
while (i ≤ N) and ( not Found) do
if (T[i] = e) then
Search ← i ;
Found ← true ;
endif
i← i+1 ;
done
end
begin
Input(N) ;
Input(x) ;
Read-Array(A, N) ;
Input(x) ;
Output(search(A,x) ; . . . . . . . . . . . . . . . . . . . .

end . . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 34/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

New concept : Loop Invariant

A loop invariant is a condition [among algorithm variables]


that is necessarily true immediately before and
immediately after each iteration of a loop.
Note that this says nothing about its truth or falsity part way
through an iteration.
A loop invariant is some predicate (condition) that holds for
every iteration of the loop.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 35/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

New concept : Loop Invariant

Loop Invariant predicate


∀ k / 1 ≤ k ≤ i−1 A[ i ] ̸= e begin
Search ← -1 ;
Note :
Found ← false ;
The loop condition is Loop Invariant while (i ≤ N) and ( not Found) do
but weak one if (T[i] = e) then
The loop invariant must be true : Search ← i ;
before the loop starts Found ← false ;
before each iteration of the endif
loop done
after the loop terminates end
although it can temporarily be
. . . . . . . . . . . . . . . . . . . .
false during the body of the . . . . . . . . . . . . . . . . . . . .
loop
H. CHERROUN Algorithmic Formalism 36/ 68
Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Loop Invariant : Usage

Loop invariants capture key facts


that explain why code works.
This helps other
At the iteration i, The array from
designers/programmers understand
[1, i − 1] is searched,
the code, and
This loop invariant informs the
helps keep them from accidentally
programmer what has to be
breaking the invariant with future
preserved,
changes.
It helps keep them from accidentally
Loop invariants can be used to prove
breaking the invariant with future
the correctness of an algorithm,
changes,
Debug an existing algorithm without
even tracing the code or develop an
algorithm directly from specification.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 37/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Dichotomy Search : Principle

Problem : Given a sorted array A of N elements, write a


function to search a given element x in A and return the
index of x in the array. (-1 Else)

A dichotomy 1 or a binary Search is a divide-to-conquer


Algorithm

1. From Latinized form of Greek dikhotomia "a cutting in half," from dikho-, combining
.
form of dikha "in two, +
. . . . . . . . . . . . . . . . . . .
temnein "to cut". Also : Dividing into 2 opposed parts . . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 38/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Dichotomy Search : Principle

Let us searching 4 in A

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 39/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Dichotomy Search : Principle

1 Begin with the mid element of the whole array as a search key.

2 If the value of the search key is equal to the sought element then return an index
of the search key.

3 Or if the value of the search key is sought element in the middle of the interval,
narrow the interval to the lower half.

4 Otherwise, narrow it to the upper half.

5 Repeatedly check from the second point until the value is found or the interval is
empty.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 40/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Dichotomy Search : Function

/* Declaration of modules */
Action Dichotomy-Search ( T : array [100] of integer, e : integer) : integer ;
i, mid, upper, lower : integer ;
Found : boolean ;
begin
Dichotomy-Search ← -1 ;
Found ← false ;
lower ← 1 ;
upper ← N ;
while (lower ≤ upper) and ( not Found) do
mid ← (upper+lower) div 2 ;
if (T[mid] = e) then
Dichotomy-Search ← mid ;
Found ← true ;
else
if (T[mid] < e) then
lower ← mid+1 ;
else
upper ← mid - 1 ;
endif
endif
done
end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 41/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Dichotomy Search : Algorithm

Algorithm : Linear Search


Data : A : an array of N Integers
x : the searched element
Result : ind : the position where x is found (or -1 Else)
/* Declaration of objects */
A : array [100] of integer ;
x, i : integer ;
/* Declaration of modules */
Action Dichotomy-Search ( T : array [100] of integer, e : integer) : integer ;
/* main Algorithm */
begin
Input(N) ; Input(x) ;
Read-Array(A, N) ;
Input(x) ;
Output(Dichotomy-Search(A,x) ;
end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 42/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

2-dimensional Array

Assume that we need to store 6 Marks for a list of 80


students

2 possible solutions
1 Array of array of Integer
2 Array of 2 dimensions (2-dimensional)
1 1st (Row) Dimension to consider the 80 Students
2 2sd (Column) Dimension to consider the marks of one
student

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 43/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

2-dimensional Array

Definition
A 2-dimensional array can be termed as an array of arrays that
stores homogeneous data in tabular form.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 44/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

2-dimensional Array : Declaration

Syntax

name-of-array : array [ Max-Row , Max-Col ] ) of Type-elements ;

Examples

Mark : array [ 80 , 6] ) of real ;


Temp : array [ 30 , 3] ) of real ;

In C language

float Mark [ 80 ] [ 6] ;
int Temp [ 30 ] [ 3] ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 45/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

2-dimensional Array : Presentation

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 46/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Access to elements

Input/Output Action :
Input (A[5,4])
Output (A[ i, j])
Assignment Action :
A[ i, j] ← 101 ;
Z ← A[2, 4] * A[2, 6] ; /* figures in an expression

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 47/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Input and Output an array

Input and Output an 2-dimensional Array

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 48/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Input and Output a 2-dimensional array : Modules

Let T a matrix of N rows and M columns

/* Declaration of modules */ /* Declaration of modules */


Action Read-Matrix( T : array [100,100] of integer, N : Action Write-Matrix( T : array [100,100] of integer, N :
integer, M : integer ) integer, M : integer )
i : integer ; i : integer ;
begin begin
for i ← 1 to N do for i ← 1 to N do
for j ← 1 to M do for j ← 1 to M do
Input( T[i,j] ) ; Output( T[i,j] ) ;
done done
done done
end end

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 49/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

Local Activity

1 Write Search-Matrix() function that checks the


existence of an element e into a Matrix of N × M.
2 write Search-Matrix-odd() function that checks the
existence of an element e into the odd rows of Matrix of
N × M.
3 Scan Row by Row
4 Scan Column by Column

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 50/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

3-dimensional Array

Syntax name-of-array : array [ Max-Row , Max-Col, Max-3D] ) of Type-elements ;

Example of deployment
In order to store the temperatures : 3 by day, for all days of the month and for all
months of the year.
Temp : array [ 3 , 31, 12 ] ) of integer ;
In C language
. . . . . . . . . . . . . . . . . . . .
int Temp [ 3 ] [ 31] [ 12] ; . . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 51/ 68


Static Data Structure Declaration
Arrays Manipulation Arrays
Strings & Record & File Multi-dimensional Arrays

k -dimensional Array

Syntax name-of-array : array [ Max-Row , Max-Col, Max-3D, , Max-4D, . . . , Max-k D] )

of Type-elements ;

Example of deployment

Temp : array [ 3 , 7, 5, 31, 12, 100 ] ) of integer ;


In C language
int Temp [ 3 ] [ 7] [ 5 ][ 12 ][ 100 ] ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 52/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Type Char and Encoding


The set of values of Char’ type has an order relationship due to the internal (physical)
representation of chars. They are coded by numbers. Example of encodings

Coding # Bits # chars Note


first character set (encoding standard) American
Standard Code for Information Interchange)
adapted to the American language :American
Standard Code for Information Interchange)
adapted to the American language : upper and
lower case letters without accents, numbers,
ASCII 7 128 punctuation symbols. (Intel family processors).
120
Unicode thousands
UTF-8 , 8, characters
UTF-16, 16, coded A Universal handling of text expressed in most of
GB 18030 32 (2015) the world’s writing systems.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 53/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Illustration : ASCII encoding

ASCII control characters (character code 0-31)


Hexa 0D Carriage Return
Hexa 1B Escape
Hexa 19 Horizontal Tab

ASCII printable characters (character code 32-127)


Upper letters OCTA 101–132
Lower letters OCTA 141–172
Digits ACTA 060 071 048-057

The extended ASCII codes (character code 128-255) (ASCII 8 bits )


Pound sign č
Latin Letters . . .

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 54/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Declaration of char object

Syntax

name-variable char Example

My_answer char ;
Djawab char ;

Usage

My_answer ← ’O’ ;
Djawab ← ’Y’ ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 55/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Strings

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 56/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Strings

Usage-Motivation
In many applications, we need to store human-readable data, like words (name, last
name, address. . . ), sentences, or lists of alphabetical data, like the nucleic acid
sequences of DNA, . . .

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 57/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Strings

A string is a data structure


It is traditionally a sequence of characters, using some character encoding,
either as a literal constant or as variable object.
It is often implemented as an array data structure of bytes (or words)
the cotes " " are used to delimited a string
Predefined type in most programming languages

Example

It can be :
text — "Introduction to Algorithms is a very interesting book"
Char — "C"
Empty string — ""

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 58/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Declaration of String

Syntax

name-of-string : String

Example

name : String ;
address : String ;
label : String ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 59/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Operations on String

Str1, Str2, Str3 : String ;

Assignment
Str1 ← "Designing Algorithm" ;
Str2 ← "Is Amazing" ; " ;
Length() of a string returns the number of its chars
Length(Str1) = 19
Concatenation : + Str3 ← Str1 + " " + Str2 ; /* "Designing Algorithm Is Amazing"
Comparison
Str1 = Str2 , Str1 ̸= Str

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 60/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Strings in C

In C , the length of a string can be stored implicitly by using


a special terminating character ;
The null character (NUL, \0, which has all bits zero.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 61/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Definition
Record/Structure/Compound Structure/
A record, a static data structure, is a collection of one or more
objects which can belong to different data Types.

Example of the declaration/definition Record Type

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 62/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Example of Record
Usage
Are used to gather many objects that describe a unique unit
(Client, Product, Person, Node. . . )

Declaration Syntax

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 63/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Access of Record’ Elements

Access by referring to the name of the record object followed by


the name of the targeted field Syntax

name-record . name-of-field

Example

E1.name ← "Ahmed Hachimi" ;


E1.Permanent ← false ;
E2.Salary ← 200000 ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 64/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Array vs Record

Array vs. Structure


The key difference between array and structure is that in the
array we can store a collection of data elements that are often
of the same data type whereas in the structure we get the
flexibility to store different data types as part of a single unit.

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 65/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Declaration of Record in C

/* declaration of the type


struct Corporate
{ char employee_name[45] ;
int employee_id ;
string employee_department ;
}
/* declaration of the object of the type Corporate
Corporate employee_1, employee_2, employee_3 ;

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 66/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

File

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 67/ 68


Static Data Structure Strings
Arrays Record/Structure
Strings & Record & File File

Bibliography & Sources

D. Beauquier, J. Berstel, and P. Chrétienne.


Éléments d’algorithmique.
Masson, 1992.
Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest.
Introduction to Algorithms.
The MIT Press and McGraw-Hill Book Company, 1989.
Dorit S. Hochbaum, editor.
Approximation Algorithms for NP-Hard Problems.
PWS publishing company, 1997.
Ellis Horowitz and Sartaj Sahni.
Fundamentals of Computer Algorithms.
Computer Science Press, 1978.
[2] [3] [1] [4]

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .

H. CHERROUN Algorithmic Formalism 68/ 68

You might also like