You are on page 1of 98

Algiers 1 University

Faculty of Sciences
Computer Science department
1st year

Algorithmic and Data Structure 1

Prepared By:
Mme Messabih.H

2023/2024
CONTENTS
 Introduction
 Sequence

 Selection

 Iteration

 Data Structures :Arrays


INTRODUCTION

Computer definition:

A computer is an electronic device, operating under the control of


instructions stored in its own memory that can accept data (input), process
the data according to specified rules, produce information (output), and
store the information for future use.
INTRODUCTION

Computer functionalities:

Takes data as input.

Stores the data/instruction in its memory and use them when required.

Processes the data and converts it into useful information.

Generates the output.

Controls all the above four steps.


INTRODUCTION

Computer Components :

Any kind of computers consists of

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

Input Devices is any peripheral (piece of computer hardware equipment


to provide data and control signals to an information processing system
such as a computer or other information appliance. Most common are
keyboard and mouse.

Output Devices is any piece of computer hardware equipment used to


communicate the results of data processing carried out by an information
processing system (such as a computer) which converts the electronically
generated information into human-readable form. Most common is
monitor .
INTRODUCTION
Processing unit:

A CPU (Central Processing Unit ) is responsible for all functions and processes.
It is comprised of three main parts :

1) Arithmetic Logic Unit (ALU): Executes all arithmetic and logical


operations. Arithmetic calculations like as addition, subtraction,
multiplication and division. Logical operation like compare numbers, letters,
or special characters

2) Control Unit (CU): controls and co-ordinates computer components.


 Read the code for the next instruction to be executed.
 Increment the program counter so it points to the next instruction.
 Read whatever data the instruction requires from cells in memory.
 Provide the necessary data to an ALU or register.
 If the instruction requires an ALU or specialized hardware to complete,
instruct the hardware to perform the requested operation.

3) Registers :stores the data that is to be executed next.


INTRODUCTION

Storage Devices:

Primary Memory:

1) RAM: Random Access Memory (RAM) is a memory scheme within the


computer system responsible for storing data on a temporary basis, so that
it can be promptly accessed by the processor as and when needed. It is
volatile in nature, which means that data will be erased once supply to the
storage device is turned off. RAM stores data randomly and the processor
accesses these data randomly from the RAM storage.
2) ROM :(Read Only Memory): ROM is a permanent form of storage. ROM
devices do not allow data stored on them to be modified.

Secondary Memory: stores data and programs permanently .


INTRODUCTION

Software

Software is a generic term for organized collections of computer data and


instructions, often broken into two major categories:

System software that provides the basic non-task-specific functions of the


computer,

Application software which is used by users to accomplish specific tasks.


INTRODUCTION
Program development life cycle:

Program
Analysis Program Design

Program
Documentation Program
and Development
maintenance
INTRODUCTION
ALGORITHM

Algorithm is the step-by-step instructions to be followed to solve a particular


task.An algorithm can be specified using flowcharts or pseudocode.

ALGORITHMs

Flowcharts Pseudocode
INTRODUCTION
FLOWCHART:

A flowchart is a pictorial representation of an algorithm in which the step are


drawn in the form of different shapes of boxes and the logical flow is indicated
by interconnecting arrow.

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:

Pseudocode is made up of two words pseudo and code. Pseudo means


limitation and code refers to instructions, written in a programming
language.

Pseudocode used plain English statements rather than symbols to


represent the processes of a computer program. It is also known as
PDL (Program Design Language)
INTRODUCTION
Dry-running:
Dry-running an algorithm means to execute its instruction, step-by-step,
by hand.

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.

Dry-running can also be really useful for identifying difficult-to-find bugs


(logic errors) in your code by taking a suspect portion and meticulously
checking exactly what is happening, step-by-step.
INTRODUCTION
Trace Table:

Trace tables are for following an algorithm through by hand and


demonstrating its effects on data and variables.

A trace table is used to store the values of each variable within an


algorithm.

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:

 We start the algorithm as:

Algorithm <AlgorithmName>;

 The algorithm will finish with the following:

End.
The general structure :

Algorithm Program en C

Algorithm <AlgorithmName>; #include <stdio.h>


Const { Defining constants } #include<stdlib.h>
Type { Defining types }
Var { Declaring variables}

Begin main () {
Instructions 1; Instructions 1;

Instructions N; Instructions N;

End. }
DECLARATIONS PART

DATA

Variables Constants

•Variable contains a value,


•Variable contains a value,
•And that value does not change
•And that value changes over time.
over time.
Identifiers:

Identifiers are the names given to algorithm ,variables, constants,


procedures and functions.
Identifiers writing rules:

 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:

Algorithm C language Example


Integer int 5,-3
Real float,double 7.5 , 3.5, 0.0
Boolean bool True,False
Character char ꞌxꞌ, ꞌCꞌ, ꞌ@ꞌ
String string "This is a string",
""
Variables declarations: In C language:

<identifier> : data type;


data type <identifier>
Example:
x : real; Example:
float x;

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 := =

Input Read (variable); scanf(‘‘ %f ‘‘, &x);

Output Write (variable); printf(‘‘ %d‘‘, x);

Arithmetic operations +,-,*, /, MOD , DIV, ^ +,-,/,%

Logic operators AND, OR , NOT &&,||,!

Comparison operators =,<=,>=,<,>,<> ==,<=,>=,<,>,!=


BOOLEAN LOGIC
 Boolean operators are used in the conditions of:
 IF Statements
 CASE Statements
 WHILE Loops
 FOR Loops
 (REPEAT UNTIL) Loops
BOOLEAN LOGIC

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:

Operator Description Operands type Example


( ) parenthesis (x-8)*2
↑ power Real, integer X² is written as X↑2 and √x is
written as X↑(1/2)

- The negative sign Real, integer -6


DIV Quotient of Integer 9 DIV 3=3; 13 DIV 5=2;
integer division
MOD Rest of integer Integer 9 MOD 3=0; 13 MOD 5=3;
division
/* Division and Real, integer 5*3=15; 7/2=3.5;
multiplication (with a real result
For /)
+ - Addition and Real, integer 5+2=7;4-1=3;
subtraction

If the order between the operators in an expression is the same, we evaluate


the expression from left to right.
PRIORITY OPERATOR
When it comes to evaluating a Boolean expression, the order of priority of
Boolean operators from highest priority to lowest priority is as follows:

Operator Action Operands type


() parenthesis
NOT NOT Boolean
AND Both Boolean
OR Either Boolean
> Greater Than
< Less Than
= Equal to
<> NOT Equal to Real, integer.
>= Greater than or
equal to
<= Less than or
equal to
SELECTION
IF STATEMENTS
IF statements may or may not have an ELSE clause.

IF statements without an else clause are written as follows:

Algorithm C language

If (condition) Then if (condition)


Instruction 1; Instruction 1 ;
End If;
if (condition) {
If (condition) Then
Instruction 1 ;
Instruction 1; Instruction 2 ;
Instruction 2; ………………..
……………….. Instruction n ;
Instruction n;
}
End If;
IF STATEMENTS
IF statements with an else clause are written as follows:

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

Yes Yes No Yes


A>C? A>B? B>C?

No No

Print A Print C Print B


START

Read in A, B
and C

Yes Yes No Yes


A>C? A>B? B>C?

No No

Print A Print C Print B

END
CASE STATEMENTS
CASE statements allow one out of several branches of code to be executed,
depending on the value of a variable.

CASE statements are written as follows:

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

An Iterative statement gives us a way to repeat a


statement while satisfying a condition.

 Count-controlled (FOR) loops


 Post-condition (REPEAT UNTIL) loops

 Pre-condition (WHILE) loops


COUNT-CONTROLLED (FOR) LOOPS
Count-controlled loops are written as follows:

For <identifier> ← <value1> to <value2>

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

It is good practice to repeat the identifier after NEXT, particularly with


nested FOR loops.
COUNT-CONTROLLED (FOR) LOOPS

An increment can be specified as follows:

For <identifier> ← <value1> to <value2> Step <increment>


<statements>
Next <identifier> ;
End For;

 The increment must be an expression that evaluates to an integer.

 In this case the identifier will be assigned the values from value1 in
successive increments of increment until it reaches value2.

 If it goes past value2, the loop terminates. The increment can be


negative.
COUNT-CONTROLLED (FOR) LOOPS (C
LANG)

for (initialisation; condition; incrémentation){


instruction1;
instruction2;

instructionN;

for (initialisation; condition; incrémentation)


instruction1;
COUNT-CONTROLLED (FOR) LOOPS
Trace Table
Example
inst i cond x y
Algorithm Ex1;
Read(x,z) ; 1 0
var
i,x,y : integer ;
Begin i1 1 (i<=3)T
Read(x,z) ; x2*x; 2
For i 1 to 3 step 1
x2*x; yy+i; 1
yy+i; Next i; 2 (i<=3)T
Next i; x2*x; 4
End For;
yy+i; 3
Write (“The value of x = ", x); Next i; 3 (i<=3)T
Write (“The value of y = ", y); x2*x; 8
End.
yy+i; 6
Next i; 4 (i<=3)F
COUNT-CONTROLLED (FOR) LOOPS
Flowchart
COUNT-CONTROLLED (FOR) LOOPS
Trace Table
Algorithm Ex2;
var
x,y : integer ;
Begin

For x 1 to 5
yx*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
a4;
y0;
For x 1 to a
yy+(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
a3;
b2;
y0;
For x 1 to 5
If (x mod 2 = 0) Then
y(a*y)+(b*x);
Else
yy+(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
zx*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

While <condition> do while (condition ) {


<statements>
End While; }

The condition must be an expression that evaluates to a Boolean.

 The condition is tested before the statements, and the statements


will only be executed if the condition evaluates to TRUE.

 After the statements have been executed the condition is tested


again.

 The loop terminates when the condition evaluates to FALSE.

 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

Print out the numbers from 1 to 5


PRE-CONDITION (WHILE) LOOPS
Algorithm Ex2;
var
A : integer ;
Begin

A1;

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

Add up the numbers 1 to 5 and print out the result


PRE-CONDITION (WHILE) LOOPS
Algorithm Ex3;
var
A ,Total : integer ;
Begin
Total0;
A1;

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;
x3;

While (turns < 22 ) do

x x*3;
turns ← turns +3;

End While;

Write (x);
Write (turns);

End.
POST-CONDITION (REPEAT UNTIL) LOOPS

Post-condition loops are written as follows: C lang

Repeat do{
<Statements>
Until <condition> ; } while (condition ) ;

The condition must be an expression that evaluates to a Boolean.

 The statements in the loop will be executed at least once.

 The condition is tested after the statements are executed and if it


evaluates to TRUE the loop terminates, otherwise the statements are
executed again.
POST-CONDITION (REPEAT UNTIL) LOOPS
Example

Algorithm Ex1;
var
Password : string ;

Begin

Repeat

Write ("Please enter the password“);


Read (Password );

Until (Password = "Secret“);

Write (Password );

End.
POST-CONDITION (REPEAT UNTIL) LOOPS
Example

Hand trace the following algorithm using as input


the numbers 2,6,34,12,0 in that order
Algorithm Ex2;
var
Result,n : integer ;

Begin
Result0;

Repeat

Read (n);

Result Result +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
A0;
B0;
C100;
Write (¨Enter your four values¨);
Repeat
Read (X);
If x>B then
BX;
End If;
If x<C then
CX;
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.

 It is good practice to explicitly state what the lower


bound of the array (i.e. the index of the first element)
is because this defaults to either 0 or 1 in different
systems.

 Generally, a lower bound of 1 will be used.

 Square brackets are used to indicate the array


indices.
ONE-DIMENSIONAL ARRAYS
Declaring arrays :

Var
<identifier> : ARRAY[<l>..<n>] OF <data type>;

Or

Var
<identifier> : ARRAY[length] OF <data type>;

Example – array declaration

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

Dog Cat Dog Bird Fish Fish Cat Cat


ONE-DIMENSIONAL ARRAYS

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]);

The loop structure is used to save N number of values in the


table:

For i← 1 to N
Read (T[i]);
Next i ;
End For;

The loop structure is used to print N number of values in the


table:

For i← 1 to N
Write (T[i]);
Next i ;
End For;
ONE-DIMENSIONAL ARRAYS
Operations on arrays:

2) Assign a value in position i of the table:

T[i]  Val ;

3) Perform calculation operations :

T[i] T[i]*2 ;

4) Make comparisons:

If (T[i]>0) Then
ONE-DIMENSIONAL ARRAYS
Serching in an array:

Question: There is an array which contains 20 elements of integer number .


Search through the array and find the largest number.

Algorithm largestNumb; Trace Table


var
Largest,i : integer ;
T: ARRAY[1..20] OF Integer ;
Begin
Largest T[1];
For i 2 to 20
If T[i]> Largest then
Largest T[i];
End If;

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

ii+ 1;
ONE-DIMENSIONAL ARRAYS

Question: There is an array which contains names of 500 students.


Search at which position “Ahmed” is stored.
ONE-DIMENSIONAL ARRAYS
Question: There is an array which contains 500 elements of data type string.
Search through the array and find (how many lines) “Empty” was repeated.

You might also like