Professional Documents
Culture Documents
Faculty of Sciences
Computer Science department
1st year
Prepared By:
Mme Messabih.H
2023/2024
CONTENTS
Introduction
Sequence
Selection
Iteration
Computer definition:
Computer functionalities:
Stores the data/instruction in its memory and use them when required.
Computer Components :
HARDWARE
SOFTWARE.
INTRODUCTION
Hardware:
Computer hardware is the collection of physical elements that constitutes
a computer system. Computer hardware refers to the physical parts or
components of a computer such as
A CPU (Central Processing Unit ) is responsible for all functions and processes.
It is comprised of three main parts :
Storage Devices:
Primary Memory:
Software
Program
Analysis Program Design
Program
Documentation Program
and Development
maintenance
INTRODUCTION
ALGORITHM
ALGORITHMs
Flowcharts Pseudocode
INTRODUCTION
FLOWCHART:
The boxes represent operations and the arrows represent the sequence in
which the operation are implemented.
INTRODUCTION
Flowchart symbols
Action
Symbol
Start/End
Input/Output
Arithmetic Process
Decision Making
Directions
INTRODUCTION
PSEUDOCODE:
This is almost always done using a trace table to hold the values of
each variable as the algorithm proceeds.
Dry-running is a vital skill for any programmer as it allows you to plan and
test algorithms before you write any code.
Each time a variable changes value, this gets recorded in the trace table.
The trace table contains a column for each variable in the algorithm and often
an additional column to show any outputs generated by the algorithm whilst it
proceeds.
SEQUENCE
Name of the algorithm:
Algorithm <AlgorithmName>;
End.
The general structure :
Algorithm Program en C
Begin main () {
Instructions 1; Instructions 1;
Instructions N; Instructions N;
End. }
DECLARATIONS PART
DATA
Variables Constants
They can only contain letters (A–Z, a–z) and digits (0–
9).
They must start with a letter and not a digit.
Accented letters and other characters, including the
underscore, should not be used.
As in programming, it is good practice to use
identifier names that describe the algorithm,
variable, procedure or function they refer to.
Single letters may be used where these are
conventional (such as i and j when dealing with array
indices, or X and Y when dealing with coordinates) as
these are made clear by the convention.
Keywords should never be used as variables.
DECLARATIONS PART
Data types:
Constants declarations:
const data type <identifier>= <value> ;
<identifier> := <value> ;
Example:
Example: const float Pi = 3.14;
Pi := 3.14;
COMMON OPERATIONS
Operations Symboles C language
Assignment or := =
A B A AND B A OR B
FALSE FALSE FALSE FALSE
FALSE TRUE FALSE TRUE
TRUE FALSE FALSE TRUE
TRUE TRUE TRUE TRUE
A NOT A
FALSE TRUE
TRUE FALSE
PRIORITY OPERATOR
The following table presents the list of operators ranked in order of priority
from highest priority to lowest priority:
Algorithm C language
Algorithm C language
If (condition) Then if (condition)
Instruction 1; Instruction 1 ;
Else else
Instruction 2; Instruction 2 ;
End If;
if (condition) {
If (condition) Then Instruction 1 ;
Instruction 1; Instruction 2 ;
Instruction 2; }
Else else {
Instruction 3; Instruction 3 ;
Instruction 4; Instruction 4 ;
End If; }
EXAMPLE:
Read two numbers, call them A and B. Is A is
bigger than B, print out A, otherwise print out B.
START
START
Read in A and
B
START
Read in A and
B
A>B?
START
Read in A and
B
Print A Yes
A>B?
START
Read in A and
B
Yes No
Print A A>B? Print B
START
Read in A and
B
Yes No
Print A A>B? Print B
END
EXAMPLE:
Read a number, and check if it is odd or even.
START
START
Read in A
START
Read in A
Does A/2
give a
remainder
?
START
Read in A
Does A/2
Print “It’s Yes give a
Odd” remainder
?
START
Read in A
Does A/2
Print “It’s Yes give a No Print “It’s
Odd” remainder Even”
?
START
Read in A
Does A/2
Print “It’s Yes give a No Print “It’s
Odd” remainder Even”
?
END
EXAMPLE:
Read in three numbers, call them A, B and C.
If A is bigger than B, then if A is bigger than C, print
out A, otherwise print out C.
If B is bigger than A, then if B is bigger than C, print
out B, otherwise print out C.
START
START
Read in A, B
and C
START
Read in A, B
and C
A>B?
START
Read in A, B
and C
Yes
A>C? A>B?
START
Read in A, B
and C
Yes No
A>C? A>B? B>C?
START
Read in A, B
and C
Yes No
A>C? A>B? B>C?
No No
Print C
START
Read in A, B
and C
Yes Yes No
A>C? A>B? B>C?
No No
Print A Print C
START
Read in A, B
and C
No No
Read in A, B
and C
No No
END
CASE STATEMENTS
CASE statements allow one out of several branches of code to be executed,
depending on the value of a variable.
Algorithm C language
Case of <identifier> switch (<identifier> ){
<value 1> : <statement> ; case Value 1 : Action 1 ; break ;
<value 2> : <statement> ; case Value 2 : Action 2 ; break ;
... case Value 3 :
Otherwise : <statement> case Value 4 :
End Case; case Value 5 :
case Value 6
……..
case Value N : Action N ; break ;
default : Action R ;
}
ITERATION
ITERATION
<statements>
Next <identifier> ;
End For;
The identifier must be a variable of data type INTEGER, and the values
should be expressions that evaluate to integers.
The variable is assigned each of the integer values from value1 to value2
inclusive, running the statements inside the FOR loop after each
assignment. If value1 = value2 the statements will be executed once, and if
value1 > value2 the statements will not be executed.
In this case the identifier will be assigned the values from value1 in
successive increments of increment until it reaches value2.
instructionN;
For x 1 to 5
yx*3;
Next x;
End For;
Write (y);
End.
COUNT-CONTROLLED (FOR) LOOPS
Flowchart
COUNT-CONTROLLED (FOR) LOOPS
Trace Table
Algorithm Ex3;
var
x,y,a : integer ;
Begin
a4;
y0;
For x 1 to a
yy+(x*a);
Next x;
End For;
Write (y);
End.
COUNT-CONTROLLED (FOR) LOOPS
Flowchart
COUNT-CONTROLLED (FOR) LOOPS
Trace Table
Algorithm Ex4;
var
a,b,y : integer ;
Begin
a3;
b2;
y0;
For x 1 to 5
If (x mod 2 = 0) Then
y(a*y)+(b*x);
Else
yy+(b*x);
End If;
Next x;
End For;
Write (y);
End.
COUNT-CONTROLLED (FOR) LOOPS
Flowchart
COUNT-CONTROLLED (FOR) LOOPS
Trace Table
Algorithm Ex5;
var
x,z,y : integer ;
Begin
For x 1 to 5
For y 1 to 2
zx*y;
Next y;
End For;
Next x;
End For;
Write (z);
End.
COUNT-CONTROLLED (FOR) LOOPS
Flowchart
PRE-CONDITION (WHILE) LOOPS
Pre-condition loops are written as follows:
C lang
The statements will not be executed if, on the first test, the
condition evaluates to FALSE.
PRE-CONDITION (WHILE) LOOPS
Example
Number Output
inst cond
Algorithm Ex1; Read(Number) ; 20
var
Number : integer ;
(Number>9)T
Begin
Number ← Number – 9; 11
Read (Number );
(Number>9)T
While Number > 9 do
Number ← Number – 9; 2
Number ← Number – 9;
(Number>9)F
End While;
Write (Number ); 2
Write (Number );
End.
PRE-CONDITION (WHILE) LOOPS
Example
A1;
While A <> 6 do
Write (A );
A ← A +1;
End While;
End.
PRE-CONDITION (WHILE) LOOPS
Flowchart START
A 1;
A A + 1;
No
Is A=6? print A
Yes
END
PRE-CONDITION (WHILE) LOOPS
Example
While A <> 6 do
Total Total+A;
A ← A +1;
End While;
Write (Total );
End.
PRE-CONDITION (WHILE) LOOPS
Flowchart
PRE-CONDITION (WHILE) LOOPS
Example
Hand trace the following algorithm:
Algorithm Ex4;
var
x ,turns : integer ;
Begin
turns 0;
x3;
x x*3;
turns ← turns +3;
End While;
Write (x);
Write (turns);
End.
POST-CONDITION (REPEAT UNTIL) LOOPS
Repeat do{
<Statements>
Until <condition> ; } while (condition ) ;
Algorithm Ex1;
var
Password : string ;
Begin
Repeat
Write (Password );
End.
POST-CONDITION (REPEAT UNTIL) LOOPS
Example
Begin
Result0;
Repeat
Read (n);
Until (n=0);
Write (Result);
End.
POST-CONDITION (REPEAT UNTIL) LOOPS
Example
Hand trace the following algorithm using as input the numbers
13,17,21,11 in that order
Algorithm Ex3;
var
A,B,C,X : integer ;
Begin
A0;
B0;
C100;
Write (¨Enter your four values¨);
Repeat
Read (X);
If x>B then
BX;
End If;
If x<C then
CX;
End If;
A A +1;
Until (A=4);
Write (B,C);
End.
Data Structures :Arrays
Arrays are considered to be fixed-length structures of
elements of identical data type, accessible by
consecutive index (subscript) numbers.
Var
<identifier> : ARRAY[<l>..<n>] OF <data type>;
Or
Var
<identifier> : ARRAY[length] OF <data type>;
Var
StudentNames : ARRAY[1..30] OF String;
T1 : ARRAY[1..3] OF Real;
T2: ARRAY[5] OF Integer ;
ONE-DIMENSIONAL ARRAYS
Array of real:
i 1 2 3 4 5 6 7 8
Tab
22.00 65.50 -2.20 78.80 54.00 -3.33 0.00 47.65
ONE-DIMENSIONAL ARRAYS
Array of strings:
i 1 2 3 4 5 6 7 8
Array of booleans :
i 1 2 3 4 5 6 7 8
InSchool
TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE
ONE-DIMENSIONAL ARRAYS
Array of characters :
i 1 2 3 4 5 6 7 8
Tab
T E F A U L R S
ONE-DIMENSIONAL ARRAYS
Operations on arrays:
1)Read or write:
Read (T[3]);
Write (T[3]);
For i← 1 to N
Read (T[i]);
Next i ;
End For;
For i← 1 to N
Write (T[i]);
Next i ;
End For;
ONE-DIMENSIONAL ARRAYS
Operations on arrays:
T[i] Val ;
T[i] T[i]*2 ;
4) Make comparisons:
If (T[i]>0) Then
ONE-DIMENSIONAL ARRAYS
Serching in an array:
Next i;
End For;
End.
Flowchart START
i 1;
Largest T[1];
Yes
Is i=20?
No
Yes END
T[i]> Largest
Largest T[i]; No
ii+ 1;
ONE-DIMENSIONAL ARRAYS