CH 1 UnderstandingAlgorithms Updated
CH 1 UnderstandingAlgorithms Updated
Problem Solving
CHAPTER (1)
UNDERSTANDING ALGORITHMS
2
What is an algorithm?
3
An algorithm is “a finite set of precise instructions for performing a computation or for
solving a problem”.
A program is one type of algorithm.
All programs are algorithms.
Not all algorithms are programs!
Directions to somebody’s house is an algorithm.
A recipe for cooking a cake is an algorithm.
The steps to compute the cosine of 90° is an algorithm.
An algorithm is a sequence of steps, which perform a specific task. In computing, algorithms are
usually represented as a program flowchart, or in pseudocode.
4
Successful Algorithms
Efficiency – it must solve the problem in the shortest possible time, using as few
5
Relationship between Algorithms & Programs
Algorithms and programs are closely related, but they are not the same. An algorithm is
6
Displaying an Algorithm
7
We carry out many everyday tasks using algorithms because we are following a set of
Written Descriptions
Flowcharts
Pseudocode
8
Algorithm for making a cup of coffee
9
Written Descriptions
Fill kettle with water
Turn on kettle
Stir
10
Activity-1 (Getting to School)
Produce a written description of an algorithm for getting to
school. It should start with leaving home and end with arriving
bus stop’.
11
1. Start
2. Leave the home
3. Walk to bus stop
4. Wait for bus
5. If bus came enter the bus
6. Bus going to school
7. Bus arrived at school
8. Everyone leaving the bus
9. Go to school
10. Stop
12
Flowcharts
Flowchart shows an algorithm as a diagram. Each
step in the algorithm is represented by a symbol.
Symbols are linked together with arrows showing
the order in which steps are completed.
13
A program flowchart is a pictorial representation of an algorithm. Program flowchart use the following symbols:
Is
Decision symbol
x > 8? No
Yes
14
15
Advantages of Using Flowcharts
16
Limitations of Using Flowcharts
17
18
19
Activity-2 (School Journey Flowchart)
Display the ‘journey to school’ algorithm that you created in
Activity 1 as a flowchart.
20
Arithmetic Operators
21
22
23
24
Example-1
Draw the flowchart to add two numbers entered by the user.
START
STOP
25
Example-2
Draw the flowchart to find the largest number among three different numbers entered by the user.
Start
End
26
Example-3
Draw the flowchart to determine the output whether Number N is Even or Odd.
Start
27
Example-4
Draw the flowchart to calculate the amount of the bank interest.
Start
Output Interest
End
28
Example- 5
Draw the flowchart to calculate the sum of first 50 numbers.
Start
End
29
Example- 6
Start
Read USD
amount
EUR amount
Sum = Sum=+USD
N
amount * 0.755
Display
EUR
amount
End
30
Activity-3 (Bath Flowchart)
A student has created a written algorithm for preparing a bath.
31
32
Assignment (Dec 6)
1. Try the written description and flowchart to find the largest
33
34
35
Pseudocode
• Pseudocode is an informal way of programming description that does not require any
strict programming language syntax or underlying technology considerations. It is used
for creating an outline or a rough draft of a program. Pseudocode summarizes a
program’s flow, but excludes underlying details. System designers write pseudocode to
ensure that programmers understand a software project's requirements and align code
accordingly.
• A constant is the opposite of a variable. It is a ‘container’ that holds a value that always stays
the same. Constants are useful for storing fixed information, such as the value of pi, the
number of litres in a gallon or the number of months in a year.
• Each variable and constant in an algorithm has to have a unique identifier. It is important to
choose descriptive names for identifiers. This will make your code much easier to read.
40
Naming Conventions for Variables and
Constants
• It is sensible to write identifiers in the same way throughout an algorithm. A common
method is to use ‘camel case’ for compound words (e.g. firstName, secondName) with
no space between words and the second word starting with a capital letter.
• Alternatively, you could capitalize the first letter of both words, e.g. FirstName,
SecondName, or separate the words with an underscore, e.g. first_name, second_name,
known as ‘snake case’.
41
Advantages of Pseudocode
• It enables the programmer to concentrate only on the algorithm part of the code
development.
• It cannot be compiled into an executable program. Example, Java code : if (i < 10) { i++; }
pseudocode :if i is less than 10, increment i by 1.
42
Add Two Numbers
Pseudocode Python code
43
44
SEND ……… TO DISPLAY
RECEIVE variable FROM KEYBOARD
SET variable TO formula
SEND variable TO DISPLAY
45
46
SEND “Enter first number.” TO DISPLAY
RECEIVE num1 FROM KEYBOARD
SEND “Enter second number” TO DISPLAY
RECEIVE num2 FROM KEYBOARD
SEND “Enter third number.” TO DISPLAY
RECEIVE num3 FROM KEYBOARD
IF num1 >= num2 and num1>=num3
SET largest_num = num1
SEND num1 TO DISPLAY
47
ELSE IF num2 >= num1 and num2 >= num3
SET largest_num = num2
SEND largest_num TO DISPLAY
ELSE IF num3 >= num1 and num3>=num2
SET largest_num = num3
SEND largest_num TO DISPLAY
END IF
48
Find the largest of three numbers
Pseudocode Python code
49
Activity 6
50
51
Strengthen S1
The specifics of this algorithm should reflect the procedure for borrowing books at the
student’s own local library or school library.
Strengthen S2
The table on page 9 describes the function of each of the arithmetic operators.
Strengthen S3 and S4
Variables and constants are explained on pages 9.
52
Strengthen S1
1. Go to the library .
2. Go to your favorite book section .
3. Take a book which u want to take .
4. Go to the library incharge and request him/her to borrow the book.
5. Take a schedule to return the book.
6. Go back home or sit in the library and read that book.
Strengthen S2
+, -, *, /, DIV, MOD, ^
Strengthen S3
Variables play an important role in algorithms and programming. The value stored by a
variable can change as a program is running. Variables are extremely useful in
programming because they make it possible for the same program to process different sets
of data. 53
Strengthen S4
Variables and constants are opposite. Variables and constants are ‘containers’ for storing
data. But the constant holds a value that always stays the same. The value stored by a
variable can change as program is running.
54
Checkpoint C1
55
Checkpoint C2
56
57
58