0% found this document useful (0 votes)
74 views58 pages

CH 1 UnderstandingAlgorithms Updated

The document provides information about algorithms and their relationship to programs. It discusses what algorithms are, examples of algorithms, and characteristics of successful algorithms. Algorithms can be expressed through written descriptions, flowcharts, or pseudocode. Common elements that algorithms should have are accuracy, consistency, and efficiency. The key differences between algorithms and programs are that algorithms are designs for solutions while programs implement those designs. Later sections provide examples of algorithms expressed as written descriptions and flowcharts.

Uploaded by

Ni Htwe
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
74 views58 pages

CH 1 UnderstandingAlgorithms Updated

The document provides information about algorithms and their relationship to programs. It discusses what algorithms are, examples of algorithms, and characteristics of successful algorithms. Algorithms can be expressed through written descriptions, flowcharts, or pseudocode. Common elements that algorithms should have are accuracy, consistency, and efficiency. The key differences between algorithms and programs are that algorithms are designs for solutions while programs implement those designs. Later sections provide examples of algorithms expressed as written descriptions and flowcharts.

Uploaded by

Ni Htwe
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 58

Unit -1

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

Three points to consider when deciding whether an algorithm is successful or not.

 Accuracy – it must lead to the expected outcome

 Consistency – it must produce the same result each time it is run.

 Efficiency – it must solve the problem in the shortest possible time, using as few

computer resources as possible.

5
Relationship between Algorithms & Programs

Algorithms and programs are closely related, but they are not the same. An algorithm is

a detailed design for a solution; a program is when that design is implemented.

6
Displaying an Algorithm

7
We carry out many everyday tasks using algorithms because we are following a set of

instructions to achieve an expected result.

An algorithm can be expressed in different ways.

 Written Descriptions

 Flowcharts

 Pseudocode

8
Algorithm for making a cup of coffee

9
Written Descriptions
 Fill kettle with water

 Turn on kettle

 Place coffee in cup

 Wait for water to boil

 Pour water into cup

 Add milk and sugar

 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

at school. For example, the algorithm could start with ‘Walk to

bus stop’.

 Check your algorithm with other members of the group. Would

your algorithm work for others? Are there any general

statements that are common to all algorithms?

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:

Start/stop symbol START

Process symbol Initialise


Variables

Input/ Output INPUT (e.g.,


symbol Player Name)

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

•Step 1: Read the Integer A.


•Step 2: Read Integer B.
•Step 3: Perform the addition by
using the formula: C= A + B.
•Step 4: Print the Integer C.

STOP

25
Example-2
Draw the flowchart to find the largest number among three different numbers entered by the user.

Start

•Step 1: Enter the Integer A, B and C.


•Step 2: Read the Integer A, B and C. Declare variables a, b and c

•Step 3: Compare A is greater than B


•Step 4: If Yes, compare whether A is Read a, b and c

also greater than C and if true, print A.


•Step 5: else print C. False True
Is a>b?
•Step 6: If No, compare whether B is
False False
greater than C and if true, print B and Is b>c? Is a>c?
else print C. True True
print b print c print a

End
26
Example-3
Draw the flowchart to determine the output whether Number N is Even or Odd.

Start

•Step 1: Read number N.


•Step 2: Set remainder as N modulo 2.
•Step 3: If the remainder is equal to 0,
then number N is even, else number N
is odd.
•Step 4: Print output.

27
Example-4
Draw the flowchart to calculate the amount of the bank interest.
Start

•Step 1: Read amount. Input Amount


•Step 2: Read years.
•Step 3: Read rate. Input Years

•Step 4: Calculate the interest with the formula


Input Rate
Interest=Amount * Years * Rate/ 100.
•Step 5: Print interest.
Interest = Amount*Years*Rate/100

Output Interest

End
28
Example- 5
Draw the flowchart to calculate the sum of first 50 numbers.

Start

•Step 1: Declare number N= 0 and N=0


Sum = 0
sum= 0.
•Step 2: Determine ‘N’ by N= N+1. N = N+1

•Step 3: Calculate the sum by the


formula: Sum= N + Sum. Sum = Sum + N

•Step 4: Add a loop between steps 2


Is No
and 3 until N= 50. N=50?
•Step 5: Print Sum. Yes
Print
Sum

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.

Working with a partner, display the following as a flowchart.

You may need to change the order or add actions.

 Put int the plug.

 Fill the bath to the correct level.

 Check the temperature is OK.

31
32
Assignment (Dec 6)
1. Try the written description and flowchart to find the largest

number for two integers entered by the user.

2. Try the written description and flowchart to determine

Whether a Temperature is Below or Above the Freezing

Point (0˚ C or 32˚ F).

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.

• Pseudocode is not an actual programming language. So it cannot be compiled into an


executable program. It uses short terms or simple English language syntaxes to write
code for programs before it is actually converted into a specific programming language.
36
37
38
39
Variables and Constants
• 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.

• 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

• Pseudocode is understood by the programmers of all types.

• 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

print ("SUM: ", sum)

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

 Ask the user to enter their username.


 Repeat until an existing username is entered.
 Next ask the user to enter their password.
 Repeat until the correct password is entered.

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

You might also like