Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
Instructor: Engr. Bernardith Batoy-Cruz (Ms. Badeth)
Email:
[email protected]FB Messenger: MsBerna Kruz (My profile pic has Siena’s SAFE frame)
LESSON 1:
WHAT IS AN ALGORITHM?
➢ A finite set of instructions that specify a sequence of operations to be carried out in
order to solve a specific problem or class of problems.
➢ A recipe for solving a problem.
Criteria for Algorithms
A procedure that does not have the properties outlined below is not an algorithm and
will not, in general, give the desired result when a program based on it is presented to a
computer. Every algorithm must satisfy the following criteria:
1. Input There are zero or more quantities which are externally supplied.
2. Output At least one quantity is produced.
3. DefinitenessEach instruction must be clear and unambiguous.
4. Finiteness If we trace out the instructions or an algorithm, then for all cases,
the algorithm will terminate after a finite number of steps.
5. Effectiveness Every instruction must be sufficiently basic that it can, in principle,
be carried out by a person using only pencil and paper.
Representing Algorithms
Algorithms are often represented using a pseudo-language which is basically a
combination of the constructs of a programming language together with informal English
statements. Almost any common programming language could be used to represent
algorithms.
Procedures
A function (sometimes called a procedure or subroutine) is just a way of
packaging a useful piece of program so that it can be used as a step in another function.
Procedures, an essential tool in programming, generalize the notion of an
operator. Instead of being limited to the built-in operators of a programming language
(addition, subtraction, etc.).
DATA STRUCTURES
Data Types and Data Structures
Data type is the term used to refer to the kinds of data that a variable may “hold” in a
given programming language.
Data structure, on the other hand, refers to a collection of variables, possibly of several
different data types connected in various ways.
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 1 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
PSEUDOCODES
The flowchart is a traditional graphical tool using standardized symbols.
The pseudocode, on the other hand, is the textual presentation of an algorithm’s
flowchart.
A pseudocode is a kind of stylized English that could be translated more or less readily
into a program.
Pseudocodes serve to arrive at the ready-to-code algorithm in a process of top-down
program design, called stepwise refinement.
IF Statement
The IF statement is a conditional statement. Figure 1a illustrates how the IF statement
may appear in a flowchart. Figure 1b presents the translation of the flowchart representation
into pseudocode format.
True False IF (Condition) then
{
Statement 1
Condition }
Statement 2
Statement 1
Figure 1b Pseudocode Representation
Statement 2 of the IF Statement
Figure 1a Flowchart Representation of
the IF Statement
IF-ELSE Statement
The IF-ELSE statement is a conditional statement that provides the computer with a
choice between two options. Figure 2a illustrates how the IF-ELSE statement may appear in a
flowchart. Figure 2b presents the translation of the flowchart representation into pseudocode
format.
True False IF (Condition) then
{
Statement 1
Condition }
ELSE
{
Statement 1 Statement 2 Statement2
}
Statement 3 Statement 3
Figure 2a Flowchart Representation of Figure 2b Pseudocode Representation of
the IF-ELSE Statement the IF-ELSE Statement
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 2 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
SWITCH Statement
The SWITCH statement is basically an expansion of the IF-ELSE statement. It provides a
mechanism for choosing an option among several possibilities. Figure 3a illustrates how the
SWITCH statement may appear in a flowchart. Figure 3b presents the translation of the
flowchart representation into pseudocode format.
Var False Var False
Value 1 Value 2 ……. Statement
n
True True
Statement Statement
1 2
Figure 3a Flowchart Representation of the SWITCH Statement
SWITCH (Var)
{
CASE (Value 1)
{
Statement 1
}
CASE (Value 2)
{
Statement 2
}
………
DEFAULT
{
Statement n
}
}
Figure 3b Pseudocode Representation of the SWITCH Statement
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 3 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
WHILE Statement
The WHILE statement allows the computer to repeatedly perform a series of statements
based on a particular condition. Figure 4a illustrates how the WHILE statement may appear in
a flowchart. Figure 4b presents the translation of the flowchart representation into pseudocode
format.
WHILE (Condition)
{
True Statement 1
Condition Statement 1 }
Statement 2
False
Statement 2
Figure 4a Flowchart Representation of the WHILE Statement Figure 4b Pseudocode Representation
of the WHILE Statement
RECURSION
➢ The process by which a procedure calls itself.
There are two types of recursion:
• Direct Recursion – recursion wherein procedures directly invoke themselves.
• Indirect Recursion – recursion wherein a chain invocation of procedures is
permitted resulting in a closed circle (ex. Procedure A calls procedure B, which
calls procedure C, which calls procedure A)
Factorial (N)
Examples of Recursion: {
If (N = 0)
Factorial {
Return (1)
Note: -The algorithm assumes that }
Else
no number less than 0 is {
passed as parameter. Return (N * (Factorial (N -1) ) )
}
- Factorial 0 is 1. }
Base Value
➢ The point wherein no recursive reference to the procedure is made.
Example: FACTORIAL (0)
Depth of Recursion
➢ Refers to the number of times the procedure is called recursively in the process of
evaluating a given argument. This means that the depth of recursion is always equal to
the number of Passes minus one.
Example: FACTORIAL (4), the depth of recursion is 4.
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 4 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
Example: Flowchart Representation
Start
Fill kettle with water
Turn on stove
Put kettle on stove
Water is True
not
boiling?
False
Turn off stove
Fill mug with boiled water
Put 1 teaspoon of coffee in mug
With Yes Put 2 teaspoons of sugar in mug
sugar
No
With Yes Put 1 tablespoon of milk in mug
milk?
No
Stir contents of mug
End
Figure 5 Flowchart Representation of How to Make a Cup of Coffee
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 5 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
ACTIVITY 1: Flowcharting (30 pts.)
1. The flow chart on the right is meant to show the steps for stopping working on a
computer and shutting it down. Place the instructions below in the flow chart.
Some of the instructions are not required - you should only include those which are
relevant to the task. (10 pts.)
• Quit the program.
• Switch off the computer.
• Finish working on your document.
• Start a new document.
• Check your email.
• Turn on the computer.
• Select “shut down”.
• Save your work.
2. The flow chart on the right is meant to show the steps for safely crossing the road. There
is a decision box in this flow chart. Place the steps below in the flow chart. (10 pts.)
• Cross the road carefully.
• Look left and right.
• Is the road clear both ways?
• Stop at the sidewalk.
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 6 of 7
Lesson 1 - Introduction to Data Structures Data Structures & Algorithms
3. The flow chart on the right is meant to show the steps for making a move in a 'Snakes
and Ladders' game. Place the steps below in the flow chart. Some instructions are
decision, so you need to decide which is which. (10 pts.)
• Climb up ladder.
• Landed on snake head?
• Throw dice.
• At bottom of ladder?
• Move the counter.
ACTIVITY 2: Flowcharting (30 pts.)
1. Draw a flowchart showing the steps that you use to brush your teeth. (10 pts.)
2. Construct a flowchart that will ask the user to enter the scores in 4 subject areas. The
average of the scores will be computed and the corresponding letter mark of the
average score will be identified. Display the average score and the letter mark. Use the
table for reference. (20 pts.)
Score Letter Mark
95-100 A
85-94 B
75-84 C
74 and below D
________________________________________
Prepared by Engr. Badeth Batoy-Cruz Page 7 of 7