CS101 Lecture 03 - Prob Solving
CS101 Lecture 03 - Prob Solving
PROGRAMMING
Lecture 3 (online)
Introduction to Computers & Programming
Week 04 May – 08 May 2020
Zoubeir Aungnoo
Lecture Aims
■ Methodology
■ Algorithm
– Pseudocode
– Flowchart
Introduction to Problem Solving
3
Software Development Method (SDM)
1. Specification of needs
2. Problem analysis
3. Design and algorithmic representation
4. Implementation
5. Testing and verification
6. Documentation
4
Specification of Needs
■ To understand exactly:
– what the problem is
– what is needed to solve it
– what the solution should provide
– if there are constraints and special
conditions.
5
Problem Analysis
6
Design and Algorithmic Representation
7
Design and Algorithmic Representation cont..
8
Control Structure
9
Pseudocodes
10
Pseudocodes: The Sequence control structure
■ A series of steps or statements that are executed in the order they are written in
an algorithm.
■ The beginning and end of a block of statements can be optionally marked with
the keywords begin and end.
■ Example 1: Calculate Age of A Person
Begin
Read the birth date from the user.
Calculate the difference between the birth date
and today’s date.
Print the user age.
End
11
Pseudocodes: The Selection control structure
12
Pseudocodes: The Selection control structure
■ Sometimes in certain situation, we may omit the else-part.
Example 3: Determine if a number is odd
if number is odd number
print “This is an odd number”
end_if
■ Nested selection structure: basic selection structure that contains other
if/else structure in its then-part or else-part.
Example 4: display the word equivalent of a number
if number is equal to 1
print “One”
else if number is equal to 2
print “Two”
else if number is equal to 3
print “Three”
else
print “Other”
end_if
13
Pseudocodes: The Repetition control structure
while condition
loop-body
end_while
14
Pseudocodes: The Repetition control structure
■Example 5: Summing up 1 to 10
15
Pseudocodes: The Repetition control structure
■ Subsequently, we can write the previous pseudocodes (example
5) with something like this.
■ Note that in this algorithm, we are using both the sequence and
repetition control structure
16
Pseudocodes: The Repetition control structure
■ Example 7: from a list of 10 people find out how many are “retired” and
“working person”
Begin
number of users giving his birth date = 0
while number of users giving his birth date < 10
begin
Read the birth date from the user.
Calculate the difference between the birth date
and today’s date.
Print the user age.
if the age is greater than 65
print “Retired”
else
print “Working Person”
end_if
number of user giving his birth date + 1
end
end_while
End
17
Pseudocodes: The Repetition control structure
■ Example 8:
18
Pseudocodes: The Repetition control structure
■ Example 9:
19
Flowcharts
20
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.
Process symbol - shows an instruction other than
input, output or selection.
Input-output symbol - shows an input or an output
operation.
21
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.
22
Flowchart – sequence control structure
Statement 1
Statement 2
Statement 3
23
Flowchart – selection control structure
No Yes
Condition
else- then-
statement(s) statement(s)
24
Flowchart – repetition control structure
yes Loop
Condition
Statement(s)
no
25
Flowchart – example 1 –
Calculate Age of A Person
Begin
Calculate
Age = current year – birth date
Display
age
End
26
Flowchart – example 2 - Find out
if a person is retired
Begin
Read age
End
27
Flowchart – example 5 –
Summing Up 10 Numbers
Begin
sum = 0
current_number = 1
NO
current_number <= 10? print sum
YES
End
sum = sum + current_number
current_number = current_number + 1
28
Exercise – Pseudocodes &
Flowcharts
■ Exercise 1: Write pseudo code & design flowchart that reads
two numbers and multiplies them together and print out their
product.
■ Exercise 2: Write pseudo code & design flowchart that tells a
user that the number they entered is not a 5 or not a 6.
■ Exercise 3: Write pseudo code & design flowchart that
performs the following: Ask a user to enter a number. If the
number is between 0 and 10, write the word blue. If the number
is between 10 and 20, write the word red. if the number is
between 20 and 30, write the word green. If it is any other
number, write that it is not a correct color option.
29
Exercise – Pseudocodes &
Flowcharts
■ Exercise 4: Write pseudo code & design flowchart to print all
multiples of 5 between 1 and 100 (including both 1 and 100).
■ Exercise 5: Write pseudo code & design flowchart that will
count all the even numbers up to a user defined stopping point.
■ Exercise 6: Write pseudo code & design flowchart that will
perform the following.
– a) Read in 5 separate numbers.
– b) Calculate the average of the five numbers.
– c) Find the smallest (minimum) and largest (maximum) of
the five entered numbers.
– d) Write out the results found from steps b and c with a
message describing what they are
30
Flowchart - exercises
■ Now draw the equivalent flowchart for each of the examples
given in pseudocode earlier, i.e.
Example 3 - slide 13
Example 4 – slide 13
Example 7 – slide 17
Example 8 – slide 18
31
Implementation
32
Testing and Verification
33
Documentation
34
Documentation cont…
35
Volume calculation
36
Calculating Electricity Bills
The unit for electricity usage is kWh. For domestic usage, the
monthly rate is 21.8 cents/unit for the first 200 unit, 25.8
cents/unit for the next 800 units and 27.8 cents/unit for each
additional units. Given the amount of electricity units (in kWh)
used by a customer, calculate the amount of money needs to be
paid by the customer to CEB. A bill statement needs to be
printed out. The currency is MUR.
37
Sum of 1 to n
38
Summary
39