0% found this document useful (0 votes)
8 views13 pages

Algorithms and Pascal Programming Basics

The document provides an overview of algorithm design and Pascal programming, defining key concepts such as algorithms, programs, and programming languages. It outlines the structure of algorithms and Pascal programs, discusses variable types, elementary instructions, and control structures like loops and conditionals. Additionally, it covers the use of arrays for storing multiple values, emphasizing the importance of proper syntax and structure in programming.
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)
8 views13 pages

Algorithms and Pascal Programming Basics

The document provides an overview of algorithm design and Pascal programming, defining key concepts such as algorithms, programs, and programming languages. It outlines the structure of algorithms and Pascal programs, discusses variable types, elementary instructions, and control structures like loops and conditionals. Additionally, it covers the use of arrays for storing multiple values, emphasizing the importance of proper syntax and structure in programming.
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

Computer Science. Basic design of Algorithms and Pascal Programming.

Basic design of Algorithmics and


of Pascal Programming.
I. GENERAL NOTIONS
Algorithmics is a term of Arabic origin (Mohamed Ibn Musa Al Khawarizmi).

I.1. Definition

1) Algorithmic:

It is the science of algorithms.

2) Algorithm:

It is an ordered sequence of instructions that indicates the steps to follow to solve


a problem or a series of problems.

3) Program:

It is a set of instructions written in a programming language that can be


executed by the machine.

4) Programming language:

It is a language that allows for the formulation of algorithms and the production of programs.
computers that apply these algorithms.

5) Compiler:

A compiler is a computer program that translates the source language into a


another language, called the target language.

A compiler is most often used to translate source code written in a language of


programming in another language, usually machine language. The program in
Machine language produced by a compiler is called object code.

1eraESM Year - Tlemcen - Page : 1 / 13


Computer Science. Basic design of Algorithmics and Pascal Programming.

I.2. General structure of an algorithm/Pascal program.

An algorithm or a Pascal program must primarily have two parts: part


header and the body part.

The name of the Pascal Algorithm/Program, the variables/constants used are declared
in the header while the body part contains the various instructions.

The general structure of an algorithm is as follows:

Algorithm Algorithm_name
Const // List of constants // Header of the Algorithm
Var // variables to be used// :Type

Debut
Body of the Algorithm
⋮ List of instructions
End.

The general structure of a Pascal Program is as follows:

Program Nom_du_programme;
Uses units to be used
Const // constants to be used // Program Header
Var // variables to be used // :Type ;

Begin
⋮ // List of instructions //
Body of the Program
End.

1eraESM Year - Tlemcen - Page : 2 / 13


Computer science. Basic design of Algorithms and Pascal Programming.

I.3. Grammar of Pascal:


• A main program always begins with 'Begin' and ends with 'End.'
(with a point). While a block of instructions also begins with 'Begin'
but ends with 'End ;' (with a semicolon).
• Each instruction ends with a semicolon. There are no exceptions to this.
rule except "Begin" and the instruction preceding "End" or "Else"
• It is recommended to write one instruction per line.
• The names of Program, constants, variables, procedures, functions, arrays, etc.
(called identifiers) must be simple names, they must necessarily
must be different from reserved words of the Pascal language and must not exceed 127
characters (at least one letter). They must only be made up of letters, of
numbers and the underscore character "_". Furthermore, Pascal does not differentiate
neither uppercase nor lowercase. Numbers are accepted except in
first place.
• Comments are represented between "{...}" or between "(*...*)". They
can be held on a single line or on several.
• Reserved words of the Pascal language:

AND, ARRAY, ASM, BEGIN, CASE, CONST, CONSTRUCTOR,


["DESTRUCTOR","DIV","DO","DOWNTO","ELSE","END","EXPORTS","FILE"]
["FOR","FUNCTION","GOTO","IF","IMPLEMENTATION","IN","INHERITED"]
INLINE, INTERFACE,LABEL, LIBRARY, MOD, NIL, NOT, OBJECT,
OF, OR, PACKED,PROCEDURE, PROGRAM, RECORD, REPEAT, SET,
SHL, SHR, STRING,THEN, TO, TYPE, UNIT, UNTIL, USES, VAR,
WHILE, WITH, XOR.

1eraESM Year -Tlemcen- Page : 3 / 13


Computer science. Basic design of Algorithmics and Pascal Programming.

I.4. Variables and constants.

A computer program always processes information entered by the user.


(via the keyboard) or stored in a file, and provided from their part other information that
whether intermediate or final, this information is called 'variables' that can
to be of different types.

A constant is a variable that does not undergo any change over the course of a
program.

Variables can be of three types: Numeric, Alphanumeric and Boolean.

1) Type Numérique :
Numeric type Beach
Octet, 8 bits (Byte) 0 to 255
Simple integer, 16 bits (Integer) -32768 à 32767
Long integer, 32 bits (LongInt) -2,147,483,648 to 2,147,483,647
-3.40×1038-1.40×10-45 for negative values
Real simple
1.40 times 10-453.40 x 1038 for positive values
-1.79×10308-4.94 × 10-324 for negative values
Real double (Double)
4,94×10-324a1.79×10308 for positive values
Some languages allow other numeric types, including:
• the monetary type (with strictly two digits after the decimal point)
• enter the date (day/month/year).

2) Alphanumeric Type:

The alphanumeric type is the character or string type (char or string).


A string is marked with quotation marks (" ") in an Algorithm, and between
apostrophes (' ') in a Pascal Program.

3) Boolean Type:

The last type of variables is the boolean type: it only stores values.
TRUE and FALSE (True, False).

1eraYear ESM -Tlemcen- Page: 4 / 13


Computer science. Basic design of Algorithmics and Pascal Programming.

II. ELEMENTARY INSTRUCTIONS


Algorithms fundamentally consist of four categories of instructions:
• Operations on variables.
• The read/write instructions (input/output).
• The test instructions (alternatives).
• Loops (repetition or iterative instructions).

II.1. Operations on variables:

A variable can undergo changes throughout a program through different


operations using each an operator. We can mention five types of operators:

• The assignment operator.


• The digital operators.
• The alphanumeric operator '&'.
• Relational operators.
• Logical or Boolean operators.

1) The assignment operator:

In an Algorithm, the assignment statement is denoted by the symbol "←", and in a


Pascal program with '꞉=' means: the variable A receives the number 15.

2) The digital operators:


Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
Div Integer division
Mod Remainder of the integer division

1eraYear ESM - Tlemcen - Page: 5 / 13


Computer science. Basic design of Algorithms and Pascal Programming.

There are other functions for numerical calculations presented in the following table:
Function Algorithm Pascal Language
Absolute value of 'x' Abs(x) Abs(x)
integer part of 'x' No need Trunc(x)
The rounding of 'x' No need Round(x)
Cosine of 'x' (in radians): cos(x) cos(x)
Sine of "x" (in radians): sin(x) sin(x)
Tangent of 'x' (in radians): tan(x) tan(x)
Exponential of 'x' exp(x) exp(x)
Natural logarithm of 'x' Ln(x) Ln(x)
Square root of 'x' Sqrt (x) Sqrt (x)
If ofn n= 2: "sqr(x)"
Power "n" of the number "x" Pow (x,n) means x raised to the power
If not: "exp(n*ln(x))"
The mathematical "π" is represented by Pique, whether for the Algorithm or in Pascal.

3) The alphanumeric operator "&":

This operator allows concatenating two strings in the Pascal language.


this operator is replaced by the Addition operator (+).

Example:
A ← "Mohamed"
B← ʺHello ʺ & A
The value of B is: Hello Mohamed.

4) Relational operators:
Operator Operation
= equal
<> Different
> Strictly greater
< Strictly less than
>= Greater than or equal to

<= Less than or equal to

5) Logical operators:
Operator Operation Types opérandes Type résultat
Not Logical Negation Boolean Boolean
And And Logic Boolean Boolean
Or Or Logic Boolean Boolean
Xor Exclusive Boolean Boolean

1eraYear ESM -Tlemcen- Page : 6 / 13


Computer Science. Basic conception of Algorithm and Pascal Programming.

II.2. The read/write instructions:

1) Read:

It is an operation that enters a value into a variable through a


input device (keyboard, mouse, scanner ...). In Pascal "read, readln".

2) Write:

It is an operation that displays on an output device (screen, printer,...) the


content of a variable. In Pascal "write, writeln) ;

Example :
Hello Algorithm
Var A : string
Debut
Write (ʺenter your nameʺ)
Read (A)
Write ("Hello", A)
End.

II.3. The test structures:

There are three testing structures:

1) The simple conditional structure:

In an algorithm, the conditional structure begins with "if" and ends with
"Finish". In a Pascal program, if the conditional structure contains several
instructions must be enclosed by 'Begin' and 'end ;'

Algorithm Pascal Language

second hand If condition then


Instruction_1 Begin
Instruction_2 Instruction_1;
Finished Instruction 2;
End ;

1eraYear ESM -Tlemcen- Page : 7 / 13


Computer science. Basic design of Algorithms and Pascal Programming.

2) The alternative structure:

The alternative structure is a conditional structure containing an 'else' block.


In Pascal language, the statement preceding the 'else' command must not end.
by ";" :

Algorithm Pascal Language

siconditionalors If condition then


Instruction_1 Begin
Instruction_2 Instruction_1;
otherwise Instruction_2;
Instruction_3 End
Finish Else
Instruction_3;

3) The nesting of 'if':

In this structure, we nest two or more conditional structures:

Pascal Language Algorithm

second conditional
siconditionalors If condition then
Instruction_1 If condition then
otherwise Instruction_1
Instruction_2 Otherwise
Finish Instruction_2
otherwise Else
Instruction_3 Instruction 3;
Finish

Note:

Dans une structure imbriquée la condition subséquente présente un bloc d’instruction de la


preceding condition.

1eraYear ESM -Tlemcen- Page : 8 / 13


Computer science. Basic design of Algorithmics and Pascal Programming.

Example:

If the student's average 'moy' is less than 10, they are 'failed'; if it is between 10 and
In 15, there will be the mention "[Link]" if the average exceeds 15, the mention is "very good".

Algorithm Pascal Language

if simoy >= 10 then


If my value is greater than or equal to 10 then
if simoy >= 15 then
write (ʺ very goodʺ) Ifmoy >= 15 then
otherwise very good
write (ʺ [Link]ʺ) Else
Finish Write(' [Link]')
otherwise Otherwise
write (ʺPostponedʺ) Postponed
Finish

II.4. The loops:

These are iterative structures in which one must repeat a process until
a predefined condition, we will study three types of loops: 'While', 'Repeat'
and 'For'.

1) The instruction 'While':

"TantQue" is used in situations where a treatment must be carried out.


systematic on the elements of a set whose quantity is not known in advance.

Algorithm Pascal Language


while condition do
As long as conditions are met begin
Instruction_1 Instruction_1
Instruction_2 Instruction 2;
… …
Instruction_N Instruction_N;
As long as End;

Similarly to before, several instructions in the Pascal program must


to be represented in the form of a block delimited by 'Beginetend;'.

1eraYear ESM -Tlemcen- Page : 9 / 13


Computer science. Basic design of Algorithms and Pascal Programming.

Example:

Double a number "a" entered by the user and display it; as long as it is
less than 1000.
Algorithm Pascal Language
while a < 1000 do
As long as < 1000 do begin
to write"The double of", a, "is: ", a*2 writeln('The double of ', a, ' is ', a*2);
a← a*2 a:=a*2;
As long as End;

2) The instruction 'Repeat':

The instruction "Repeat" is used in cases where the program must perform
operations until a condition is met.

Algorithm Pascal Language


Repeat Repeat
Instruction_1 Instruction_1;
Instruction_2 Instruction_2;
… …
Instruction N Instruction_N;
Up to condition Until condition
Note:

The instructions between 'Repeat' and 'Until' are executed in sequence until
that at the end of the sequence, the boolean condition "condition" is true. So no need to
group instructions in a block (Begin...end;).

3) The 'For' Instruction:

We use the 'For' loop when we know exactly how many iterations we need.
achieve.

Algorithm Pascal Language


ForCounter := initialToFinalDo
For Counter Initial to Final Step Value of Step (ForCounter:=finalDownToinitialDo)
Instruction_1 Begin
Instruction_2 Instruction_1
… Instruction 2;
Instruction N …
FinPour Instruction_N;
end ;

1eraYear ESM -Tlemcen- Page: 10 / 13


Computer Science. Basic design of Algorithmics and Pascal Programming.

Note:
• In the absence of "ValeurDuPas"; the algorithm uses the default value "1".
• The command 'To' increments while 'DownTo' decrements by '1'.
value of the meter.

Example:

Multiply the number "a" by the numbers 0 to 10 and display the results.

Algorithm Pascal Language


Pouri := 0 to 10 For i := 0 to 10 do
write (i,"*", a, "=" , i*a) (*For i:=10 down to 0 do*)
EndFor writeln(i,' * ', a, ' = ‘, i*a);

1eraYear ESM - Tlemcen - Page: 11 / 13


Computer science. Basic design of Algorithmics and Pascal Programming.

III. ONE-DIMENSIONAL AND TWO-DIMENSIONAL TABLES


So far, we have dealt with data types that only stored one
value at the same time (integer, real, ...). However, it is often useful to store all at once
several values. This is the case for vectors or matrices, for this we use this
what are called tables.

In our course, we will study one-dimensional and two-dimensional arrays.

III.1. One-dimensional arrays:

An array, just like any variable, must be declared in the part


declarative of the program. One must always specify the type of variables that will be
introduced in the table.

The declaration of an array in Pascal is done as follows:

vartable_name: array[dim_init..dim_fin] of type;

• table_name: le nom du tableau.


• dim_init: the index of the first term of the array.
• dim_fin: the index of the last term in the array.
• type: the type of the variables in the array (integer, real,...).

Example:

varVect:array[1..5]ofInteger;

"Vect" is an array of 5 integer variables.

Reading or writing the terms of the table is done via a loop, for our example:

for i = 1 to 5 do read(Vect[i]);

In general, accessing an element of the array is done by its index (Vect[i]).

1eraESM Year - Tlemcen - Page : 12 / 13


Computer science. Fundamentals of Algorithm Design and Pascal Programming.

III.2. Two-dimensional tables:

One can create arrays with components of all types, including arrays.
And consequently, we can create multi-dimensional arrays for our course.
we will limit ourselves to two-dimensional arrays.

for example; we can see a matrix as a table of lines that are themselves
component tables.

Example: a matrix with 3 rows and 4 columns is declared as follows:

varMat:array[1..3]of array[1..4]ofInteger;

you

varMat: array[1..3, 1..4] of Integer;

Access to the terms of the matrix is done through these indices (Mat[i,j]):

( ( ) ) 1.2
1.1 ( ) 1.3 ( )1.4
= ( (2.1
) ( ) ( ) 2.2
) 2.3 2.4
( 3.1) ( )
3.2 3.3 ( )3.4 ( )

for reading or displaying the values of the terms of the matrix, it is preferable
to use two loops:

for i = 1 to 3 do

begin
for j:=1 to 4 do write(Mat[i,j], ' ');

writeln;
end;

III.3. Useful Functions


Function name Explanation
length Returns the length of the array or string
low Returns the minimum index of the array
high Return the maximum index of the array

1eraYear ESM -Tlemcen- Page: 13 / 13

You might also like