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

Introduction to Algorithms and Data Structures

The document provides an introduction to algorithms and data structures, covering key concepts such as the definition of algorithms, characteristics of good algorithms, and basic types of data. It explains the roles of variables and constants, simple instructions like assignment, read, and write, as well as conditional statements for decision-making in algorithms. The content is structured to guide readers through the foundational elements necessary for understanding and writing algorithms.

Uploaded by

zih.achuo
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)
11 views13 pages

Introduction to Algorithms and Data Structures

The document provides an introduction to algorithms and data structures, covering key concepts such as the definition of algorithms, characteristics of good algorithms, and basic types of data. It explains the roles of variables and constants, simple instructions like assignment, read, and write, as well as conditional statements for decision-making in algorithms. The content is structured to guide readers through the foundational elements necessary for understanding and writing algorithms.

Uploaded by

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

ALGORITHM AND DATA STRUCTURE 1

Lecturer: MUGHE Godlove

ALgorithm and data structure 1 .............................................................................................................................. 1


Introduction to Algorithms ..................................................................................................................................... 2
Background ......................................................................................................................................................... 2
Characteristics of the algorithms ........................................................................................................................ 3
Variables and constants ...................................................................................................................................... 3
Characteristics of a Good Algorithm ................................................................................................................... 4
Basic types........................................................................................................................................................... 6
Simple Instructions.................................................................................................................................................. 7
Assignment Instruction ....................................................................................................................................... 7
Read Instruction .................................................................................................................................................. 8
Write Instruction ................................................................................................................................................. 9
Conditional Statements (Alternatives) .................................................................................................................. 10
Types of Conditional Statements ...................................................................................................................... 11
INTRODUCTION TO ALGORITHMS

BACKGROUND

The term Computer Science is a neologism proposed in 1962 by Philippe Dreyfu1 for
characterize the automatic processing of information, it is a contraction of the expression
"automatic information".

BASIC CONCEPTS

COMPUTER SCIENCE
Computer science is the science of automatic information processing. It deals with two
additional aspects:
programs or software which describe a treatment to be carried out, the machines or
hardware that performs this processing
HARDWARE

It is the set of physical elements (microprocessor, memory, hard drives, etc.) used to process
information.
SOFTWARE

It is a program (or set of programs) describing the processing of information to be carried out
by computer equipment.

ALGORITHM

The notion of algorithm is the basis of all computer programming.


The simplest definition that can be associated with this notion is that an algorithm is an
ordered sequence of instructions that indicates the steps to follow to solve a problem
or perform a task. The word algorithm comes from the Latinized name of the Persian
mathematician Al-Khawarizmi, nicknamed “the father of algebra”.
Example : PHONE CALL.
1. Open Youri phone,
2. Search/Dial the recipient’s number,
3. Press the “Call” Button.
This manual explains how to make a phone call. It consists of a series order of
instructions (open, search/compose, press) that manipulate data (phone, number,
button) to complete the calling task.
CHARACTERISTICS OF THE ALGORITHMS
GENERAL STRUCTURE

An algorithm generally consists of two parts:

Declarative part: also called the algorithm header, it generally contains the declarations
(constants, variables, etc.).

Body part of the algorithm: consisting of one or more sequences of instructions involving
basic operations to be performed by the computer.

VARIABLES AND CONSTANTS

The unitary element of information storage is called a bit.


A bit can only have two distinct states: 0 or 1 (true or false in logic). In the computer’s
memory, the data is manipulated in groups of 8 bits (bytes), or more (words of 16, 32, 64 bits,
etc.).
A memory location is therefore called a word and so that the central unit can store an
information and find it in memory, each word is identified by an address.
In programming, memory addresses are represented by names.
The programmer therefore does not know the address of a box but rather its name. There
are therefore two ways of looking at the computer’s central memory: from the programmer’s
side and from the computer’s side which is illustrated, as an example, in the following diagram
CHARACTERISTICS OF A GOOD ALGORITHM
A good algorithm should be:
1. Clear and unambiguous – each step is well defined.
2. Finite – it must end after a certain number of steps.
3. Effective – each step is doable.
4. Has input and output – data comes in, result goes out.
5. Efficient – uses minimal time and resources.

VARIABLES
A variable is a memory location intended to contain values of a previously defined type.
(numbers, characters, strings, etc.). It has a name, a type, and a content which can be modified
during the execution of the algorithm.
The keyword is: Var.
CONSTANTS
The definition of a constant is the same as that of a variable except that its
value remains unchanged throughout the course (execution) of the algorithm.
The keyword is: Const.
Variables and constants are declared using the following syntax:

Var variable name: type;


Cost constant_name = value;

Noticed:
In the declarative part, variables and constants are essentially characterized by:
An identifier: is a name assigned to the variable or constant, which can be composed of
letters and numbers but without spaces.
A type: which defines the nature and size of the variable.
Example:
Var x, y: integer;
Const alpha = 0.5;
In this example, we declared:
▪ Two variables (x and y) of integer type, this type is described in the
following subsection.
▪ A constant (alpha) equal to the value 0.5 for example.
BASIC TYPES

The type of a variable defines the set of values that the variable can take, as well as the set
of operations that can be applied to this variable. There are types
simple predefined ones such as: integer, real, character and Boolean.
The type of a variable defines:
• the set of values that the variable can take, and
• the set of operations that can be applied to that variable.
A variable’s type tells the computer what kind of data it contains and what you can do with
it.
Integer
• Definition: Whole numbers (without decimals or fractions).
• Example of values: -5, 0, 7, 120
• Common operations: +, -, *, /, % (modulus)
• Example:
• N ← 10
• M←N+5 → Result = 15

Real
• Definition: Numbers that can have decimal parts (fractions).
• Examples of values: 3.14, -2.5, 0.75
• Common operations: +, -, *, /
• Example:
• A ← 2.5
• B ← 1.2
• C←A+B → Result = 3.7

Character
• Definition: A single letter, digit, or symbol, written in quotes.
• Examples of values: 'A', 'z', '5', '#'
• Common operations: comparison (=, ≠, <, >), concatenation (joining with strings).
• Example :
• L ← 'A'
• M ← 'B'

Boolean
• Definition: Has only two possible values:
o TRUE or FALSE (sometimes 1 or 0)
• Used for: logical tests and conditions in algorithms.
• Example :
• A ← 10
• B←5
Test ←( A > B ) → Results = TRUE
5. Conclusion
This chapter provides an introduction to the basic concepts of writing algorithms.
syntax of an algorithm, the notion of variable and constant, as well as their types are defined
and explained through simple examples.

SIMPLE INSTRUCTIONS
An algorithm, by definition, is a set of instructions that tell the computer (or a person)
exactly what actions to perform in order to solve a problem.
These instructions can be simple or complex, depending on their purpose and structure.
In this chapter, we will focus on simple instructions, which form the foundation of every
algorithm.
The simple instructions inclue:
• Assignment instruction
• Read instruction
• Write instruction
These are the basic operations that allow an algorithm to store, input, and output
information.
ASSIGNMENT INSTRUCTION
The assignment instruction is used to give a value to a variable.
It allows you to store a piece of information (number, character, or result of a calculation)
inside a variable.
In other words, it assigns a value (on the right) to a variable (on the left).

Syntax:
Variable ← Expression
or sometimes written as
Variable = Expression
The arrow (←) means “receives the value of”.

Explanation :
• The expression on the right side is first evaluated (calculated).
• The result is then stored in the variable on the left.
• The previous value of the variable (if any) is replaced by the new one.

Examples
Simple assignment
A ← 10 ( The variable A receives the value 10.)
Using an expression
B ← A + 5(variable B receives the value of A + 5 (if A = 10, then B = 15).)
Assignment with calculation
Area ← Length * Width (The variable Area receives the product of Length and Width.)
NB
• The left side must always be a variable (a memory location).
• The right side can be:
o a constant (e.g., 5)
o another variable (e.g., A)
o or an expression (e.g., A + B).
• Example of error:
10 ← A Wrong (cannot assign to a constant)

Real-Life Analogy
Think of a variable as a box with a name.
The assignment instruction means putting a value inside that box.
Example:
• Box name → “A”
• Instruction → “Put 7 inside box A”
So we write:
A←7

READ INSTRUCTION
The Read instruction is used to input data into an algorithm.
It allows the user to enter values that the algorithm will store in variables for later
use.
In simple terms, it tells the computer:
“Wait for the user to type something and save it.”

Syntax:
Read (Variable)
You can also read several variables at once:
Read (A, B, C)

Examples
Reading a single value
Read (Name)
The user enters a name which is stored in the variable Name.
Reading two numbers
Read (Length, Width)
Area ← Length * Width
The algorithm receives two numbers and then calculates the area.
Explanation
• The Read instruction pauses the algorithm and waits for the user’s input.
• The entered data is then stored in the variable(s).
• It helps algorithms become interactive.

WRITE INSTRUCTION

The Write instruction is used to display information or results on the screen.


It allows the algorithm to communicate with the user by showing messages, variable
values, or computation results.

Syntax:
Write (Expression or Message)
You can display text, numbers, or both together.

Examples
Displaying a message
Write ("Enter your name:")
Displaying a variable
Write (Name)
Displaying a message and a result
Write ("The area is:", Area)
Displays something like:
The area is: 25

. Explanation
• Texts (messages) are written inside quotation marks " ".
• You can display multiple values separated by commas.
• It is often used after calculations to show the results.
CONDITIONAL STATEMENTS (ALTERNATIVES)

Algorithms generally have two types of instructions:

- Simple instructions: which allow the manipulation of variables such as assignment,


reading and writing.

- Control instructions: which specify the chronological sequence of simple


instructions. This is particularly the case for conditional instructions or the tests.

In an algorithm, we often need to make decisions — that is, choose between two or more
possible actions depending on a condition.

A conditional statement (also called an alternative structure) allows the algorithm to test a
condition and execute instructions based on whether the condition is true or false.

Example idea:

If it rains, take an umbrella; otherwise, go out without one.

This is a real-life conditional action — and algorithms work the same way.

A conditional statement is an instruction that allows an algorithm to choose between two


or more execution paths depending on a logical condition.

The condition usuelle inviolés :

• a comparison between two values, and

• a logical result: TRUE or FALSE.

Comparison Operators

These are used to form conditions.

Operator Meaning Example Result


= Equal to A=B TRUE if A and B are equal
≠ or != Not equal to A≠B TRUE if A and B are different
< Less than A<B TRUE if A is smaller
> Greater than A>B TRUE if A is greater
≤ Less than or equal to A≤B TRUE if A ≤ B
≥ Greater than or equal to A≥B TRUE if A ≥ B
TYPES OF CONDITIONAL STATEMENTS
There are two main types of alternatives:

A. SIMPLE ALTERNATIVE (SINGLE SELECTION)


Used when an action is required only if the condition is true.

Syntax:

If Condition then

Instruction(s)

EndIf

Ex:

If (Age ≥ 18) then

Write("You are an adult")

EndIf
If the age entered is 18 or more, the program displays “You are an adult.”
If not, nothing happens.

B. DOUBLE ALTERNATIVE (TWO-WAY SELECTION)


Used when there are two possible actions — one if the condition is true, another if it is
false.

Syntax:

If Condition then

Instruction(s) 1

Else

Instruction(s) 2

EndIf

Example: Explanation:
If (Number mod 2= 0) then The algorithm checks if the number is
divisible by 2.
Write("Even number")
• If true → prints “Even
Else number.”
Write("Odd number") • Otherwise → prints “Odd
EndIf number.”
C. MULTIPLE ALTERNATIVES (NESTED OR CHAIN SELECTION)
Used when there are several possible conditions.

Syntax:

If Condition1 then

Instruction1

ElseIf Condition2 then

Instruction2

Else

Instruction3

EndIf

Example:

If Note ≥ 16 then

Write("Very Good")

ElseIf Note ≥ 12 then Explanation:


Depending on the student’s mark
Write("Good") (Note), the program displays the
ElseIf Note ≥ 10 then corresponding remark.

Write("Pass")

Else

Write("Fail")

EndIf

Practical Examples:

Check if a number is positive

Read (N)

If N > 0 then

Write("Positive")

Else

Write("Negative or zero")

EndIf
Find the largest of two numbers

Read (A, B)

If A > B then

Write("A is greater")

Else

Write("B is greater")

EndIf

Student grade classification

Read (Note)

If Note ≥ 10 then

Write("Pass")

Else

Write("Fail")

EndIf
. SUMMARY TABLE

Type Structure Description

Simple Alternative If ... then Executes only if condition is true

Double Alternative If ... then ... else Chooses between two actions

Multiple Alternative If ... elseif ... else Chooses among several options

Conclusion

Conditional statements (alternatives) are essential in algorithms because they enable


decision-making.
They allow programs to behave differently according to different conditions, making
algorithms flexible, intelligent, and realistic.

Common questions

Powered by AI

Conditional statements allow algorithms to make decisions by executing code paths based on conditions, adding flexibility. For example, a weather-checking algorithm can advise carrying an umbrella if rain is forecasted, illustrating conditional logic similar to choosing actions based on varying circumstances .

Assignment instructions store or update data values in memory, facilitating dynamic data manipulation. Read instructions input data from external sources, enabling real-time interaction. Write instructions output data to users or files, providing feedback. Together, they enable comprehensive data handling, essential for interactive and functional programs .

Variable types dictate permissible operations, ensuring data types suit computational needs. Integer types allow arithmetic operations like addition; real types support decimals, useful in precision calculations; character types enable data comparison and concatenation; Boolean types facilitate logical operations. For instance, integers can perform modulus operations, while reals handle fractions .

Clear and unambiguous steps ensure that each instruction is explicitly defined, leaving no room for confusion or misinterpretation during execution. This characteristic helps prevent errors, facilitates easier debugging, and ensures that algorithms perform tasks consistently and predictably, ultimately improving reliability and efficiency .

Simple instructions handle direct manipulations, like assignments and I/O operations, whereas control instructions regulate execution flow, such as loops and conditionals. Both are crucial; simple instructions perform essential tasks, and control instructions enable adaptable and logical workflows, enhancing algorithm complexity and utility .

Conditional statements include: simple alternative (single condition execution), double alternative (execute one of two paths), and multiple alternatives (choosing among multiple conditions). Simple is ideal for straightforward decisions, double provides binary choice, while multiple is for nuanced scenarios, like grading systems or multifactorial checks .

Input and output are crucial as they define the data flow through an algorithm: input processes incoming data, and output communicates results. For instance, in a telephone dialing algorithm, the input is the phone number entered, and the output is the connection to establish the call .

Variables are memory locations with modifiable values, characterized by identifiers and types, allowing dynamic data handling. Constants, however, have fixed values throughout execution, providing stability and simplifying logic. Both are declared in the declarative part, ensuring data type consistency and memory efficiency .

A memory address uniquely identifies a storage location. From the programmer's view, it abstracts complexity via named references (variables/constants), aiding readable and manageable code. The computer uses addresses directly to locate and access data efficiently, ensuring precise data manipulation .

Efficiency is vital as it ensures that an algorithm uses minimal resources and time to perform a task. Factors contributing to efficiency include optimized computational logic, minimal redundancy, and effective memory usage, leading to faster processing and lower resource consumption .

You might also like