Chapter Two - Algorithms and Data Structures
Chapter Two - Algorithms and Data Structures
Chapter 2:
Algorithms and Data Structures
◦ What is an algorithm?
◦ Writing Algorithms
◦ Translation of algorithms to flowcharts
and pseudo code
◦ Data Structures and computer programs
Pseudocode Flowchart
Artificialand Informal • A graphical way of
language writing pseudocode
Helps programmers to • Rounded rectangle -
plan an algorithm terminal
Similar to everyday
• Parallelogram-input /
output
English
• Rectangle - actions
Not an actual
• Diamonds - decision
programming language /conditional
• Circles - connector
FOP -- Chapter One -- Introduction 16
06/27/2024
1.2. Writing Algorithms
Flowchart Symbols
Example:
Write an algorithm that receives two numbers
from an input device and returns their sum.
Translate the algorithm to a pseudocode and a
flowchart.
Algorithm:
Begin
Step 1: Receive two integers
Step 2: Calculate their sum
Step 3: Display sum
End
Sequences: one
command is
executed after
previous one.
◦ IF … ELSE Structure
◦ Switch Structure
Example:
Write an algorithm that receives two non-equal
numbers and outputs the maximum.
Pseudocode
Start
Read A, B
Max = A
If (B > A)
Max = B
Display Max
Stop
Example:
Write an algorithm to determine a student’s
average grade and indicate whether he is
successful or not.
The average grade is calculated as the average
of mid-term and final marks.
Student will be successful if his average grade
is grater or equals to 60.
Start
Use variables total, counter, grade, average
Initialize total = 0
Initialize counter = 1
While (counter <= 5)
Input grade
Calculate total = total + grade
Calculate counter = counter + 1
End-while
Calculate average = total / 5
Display average
Stop
FOP -- Chapter One -- Introduction 42
06/27/2024
1.3. Translation
Flowchart
Properties of algorithms
• Finiteness
• Definiteness
• Input
• Output
• Effectiveness
Data structure …
mainly specifies the structured
organization of data, by providing accessing
methods with correct degree of
associatively.
affects the design of both the structural and
functional aspects of a program.
Example
Efficiency
1) What is an algorithm?
2) Explain the need for writing an algorithm?
3) What are the benefits of developing
flowchart/pseudocode?
4) Discuss decision structure and looping.
5) What is Data Structure?
6) Explain the role of data structures in
programming.