You are on page 1of 53

Chapter 1:

PROBLEM-
SOLVING
DPS20043 –
INTRODUCTION TO
PROGRAMMING
Lesson Learning Outcome (LLO)
• Students will be able to:
• Define the concept of algorithms
• Explain eight algorithmic problem-solving steps
• Apply strategies for developing algorithms
• Control structures used in algorithms
• Flowcharts
• Pseudocodes
• Programming languages
ALGORITHM
• A step-by-step procedure to solve a given problem
consisting of a finite set of unambiguous rules
(instructions) which specify a finite sequence of operations
that provides the solution to a problem, or to a specific class
of problems for any allowable set of input quantities (if there
are inputs).
• The steps are normally "sequence“, "selection“, "iteration"
and a case-type statement.
• Uses human native language.
EXAMPLE OF ALGORITHM
• Instruct someone to add up a list of prices on a pocket calculator:

Turn on calculator
Clear calculator
Repeat the following instructions
Key in dollar amount
Key in decimal point(.)
Key in cents amount
Press addition(+) key
Until all prices have been entered
Write down total price
Turn off calculator
PROBLEM SOLVING
• Understand and analyze the problem
• Includes 3 components:

INPUT PROCESS OUTPUT

• List of things • Actions that • Desired


that is required need to be outcome
by the problem done to get
output
PROBLEM SOLVING
• A café needs a program to figure its weekly profitable menu. The
worker should input data consisting menu name, quantity sold and
its price. Calculate the sales amount for each menu. If the sales
amount of the menu is more than RM500, then the café is making
profit from the menu and shall continue to have it in the menu list.
The program should display the menu name that is profitable and
also the menu that is to be discontinued.
PROBLEM SOLVING
• What is the data that we need?
• Where do we get the data? From user or from file?
• How do we compute the sales amount?
• How do we make sure user key-in all the data?
• How do we tell the user about the profitable menu?
• How do we display the menu name?
..ALGORITHM
• Can be implemented by using either

OR

PSEUDOCODE FLOWCHART
8
FLOWCHART-DEFINITION
• Visual representation of each task and how the
tasks will flow to produce a full program.
• Shows the program logic as each task is
represented by certain symbols, which are then
connected by lines and arrows to show the flow.

9
FLOWCHART SYMBOLS
Symbol Purpose
Flowchart should always begin and
Terminal end with this symbol. It indicates
the starting or ending point of the
logic.
It symbolizes input (Read / Prompt
Input / / Get) or output (Print / Write / Put
Output / Output / Display) process in a
code.

10
FLOWCHART SYMBOLS
Symbol Purpose
Process Symbolizes any process in a
pseudocode. Example: Declare
variables, calculations and assign
values to variables
Predefined Indicates a module in an
process algorithm. In other words, the
module can be expended to
include few sub processes.
11
FLOWCHART SYMBOLS
Symbol Purpose
used when there are decisions to
Decision be made in the logic. There will be
options where the logic needs to
decide which path to take (either
true or false).

used to connect various symbols in


Flow lines flowchart. The line and arrowhead
indicate the logic flow of control.
12
THE BASIC CONTROL STRUCTURES
There are 3 basic control structures:

SEQUENTIAL

SELECTION

REPETITION
13
1. Sequential
• Straightforward execution of one processing step
after another
• Basic computer operations
1. Receive information
2. Assign values
3. Perform arithmetic
4. Put out information

14
How do you boil water on stove?

15
Add 2 numbers
START
1. Start
2. Declare variables, number_1, Declare variables
number_2, total number_1,number_2,total

3. total = number_1 + number_2 Get


number_1,number_2
4. Display total
5. End total=number_1+number_2

Display total

END
16
Exercise on Flow chart
• Draw a flow chart that will get two test results,
calculate the average and display the result of
calculation.

17
2. Selection
• Condition: based on the condition criteria,
certain actions will be executed and some will
not
• Condition can either be: true or false also yes
or no.
• We could also have multiple condition/
options.

18
2. Selection

If… else case

19
Example (life scenario)
• Say for example you are
trying to decide whether to
go watch a movie in IOI
City Mall, Putrajaya.
However, the weather is a
factor of concern for you. If
it rains, you decide to finish
up your assignment. If it
does not rain, you will go to
the mall and watch movie
at the cinema.

20
Exercise on Flow chart (life scenario)
 Draw a flow chart
for making a cup
of tea. If your guest
prefer milk, add
milk to the tea and
if he/she prefers
sugar, add sugar to
it.

21
Exercise on Flow chart (computer system)
• Draw a flow chart that will get two test results and
calculate the average. If the average result is more
than 80 marks, display the average result with a
message “You are awesome!”. If the result is equal to
or less than 80, display the average result with a
message “work harder, dude..”

22
3. Repetition

• Allows tasks to be done repeatedly which can


be controlled by setting certain condition
• When condition is false, the repetition stops
and the tasks will not be executed

23
Example
• I would like to add sugar into my cup of
coffee. However, that would depend on how
sweet it will taste after each additional spoon
of sugar.

24
Exercise on Flowchart
1. Draw a flow chart for a program that will get two test
results and calculate the average. If the average
result is more than 80 marks, display the average
result with a message “You are awesome!”. If the
result is equal to or less than 80, display the average
result with a message “work harder, dude..”.
Your program should be able to accept the result
as per user wish. In order to stop, user will be ask
by the program and they will have to press “Q” for
quit.

25
Exercise on Flowchart
2. Draw a flow chart for a program that will get the name
and final exam results for all DPS20043 students; and
calculate the total scores for all students, as long as the
result entered by user is not more than 100 or less than
0. If the entered result is not more than 100 or less than
0, the average result will be calculated and displayed
on screen.

26
PSEUDOCODE?
Really structured English that has been formalized and
abbreviated to look like the high-level computer
languages.

1. Start
2. Declare variables-width, length, size
3. Prompt user for width, length
4. Get width, length Remember!!!!!
5. size = width * length Another programmer will need
to understand the set of
6. Display size instructions you write.
7. End So, meaningful words &
phrases will make it easier
to translate pseudocode to
programming language later. 27
PSEUDOCODE – MEANINGFUL NAMES
1. To represent variables in our pseudocode/ flowchart
2. number1 vs n1 <- use meaningful naming
3. Majority of programming languages does not allow gap
(space) in variable names
E.g. Total Sales X
Underscore (_) is used if there are more than one word in the
variable or alternatively, use capital letters to separate
words (try Google “camel case”)
E.g. total_sales √
totalSales √

28
PSEUDOCODE-THE STRUCTURE
• Writing pseudocode is similar to how we write essays, where
we need beginning, body content and ending of an essay.
• Begin with ‘Start’
• End with ‘End’
• Body content are your processes that show sequence of tasks
in the program
• Pseudocode corresponds closely with the input, processes
and output identified at the beginning of problem solving
activity
PSEUDOCODE
• Still remember what is:
• INPUT?
• A list of source data provided to the problem.

• PROCESSES?
• A list of actions needed to produce the required outputs.

• OUTPUT?
• A list of the outputs required.
PSEUDOCODE KEYWORDS - INPUT
• ‘Prompt’ and ‘Get’:
• use to receive input from the keyboard.

• ‘Read’:
• use to receive input from a record on a file.
PSEUDOCODE
• A computer can:
• receive information
• put out information
• perform arithmetic calculation
• assign a value to a variable or memory location
• compare two variables and select one of two alternative
actions
• repeat a group of actions
COMPUTER CAN RECEIVE INFORMATION
PSEUDOCODE KEYWORDS - INPUT
• The difference between ‘Prompt’ and ‘Get’:
+ Usually ‘Prompt’ before ‘Get’.
+ ‘Prompt’ causes message to be sent to the
screen, asking user to respond…usually by
providing input.

Prompt for student_mark


Get student_mark

34
COMPUTER CAN PUT OUT INFORMATION

35
PSEUDOCODE KEYWORDS - OUTPUT
‘Display’, ‘Output’ or ‘Put’:
used when we want output to be displayed
to the computer screen.
‘Print’:
enables output to be sent to a printer.
‘Write’:
enables output to be written to a file.
Print ‘Program Completed’
Output total_tax
36
COMPUTER CAN PERFORM ARITHMETIC
CALCULATION

Source: www.odyssey-resources.com

37
PSEUDOCODE -ARITHMETIC SYMBOLS &
EXAMPLES
Symbols that you can use to write pseudocode:
Symbol Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
() Parentheses
divide total_marks by student_count
sales_tax = cost_price * 0.10
compute C = (F - 32) * 5/9
Total_ price is equal to qty* price 38
HOW TO WRITE PSEUDOCODE-EXAMPLE
Problem:
You need to write pseudocode for a program that
will add two numbers together

STEP 1. Identify the Input, Process and Output

Input Process Output


number_1 Get two numbers total
number_2 Add the numbers together
Display the total
39
HOW TO WRITE PSEUDOCODE-EXAMPLE

STEP 2. Write pseudocode.


1. Start

2. Declare variables, number_1, number_2,


total
3. Prompt and Get number_1, number_2

4. Calculate total = number_1 + number_2

5. Display total

6. End.

40
YOUR TURN….
• A program is to read three numbers from
user, multiply all three numbers and display
the result. Write the pseudocode..

• INPUT: ???
• PROCESS: ???
• OUTPUT: ?

41
STEP 1. Identify the Input, Process &
Output
• INPUT: number1, number2, number3

• PROCES: get three numbers, multiply all


numbers, display result

• OUTPUT: result
STEP 2. Write pseudocode.
1. Start
2. Declare variables, number_1, number_2,
number_3, result
3. Prompt and Get number_1,
number_2,number_3
4. result = number_1 * number_2 * number_3
5. Display result
6. End.
DESK CHECKING??
• Desk checking: process of tracing through
the pseudocode with some test data
• Mimic what the computer would do based
on the pseudocode written
• Help eliminate errors but will not be able to
prove that your algorithm is correct

44
DESK CHECKING
• Should have at least 2 sets of input data for
testing.
• Write the desired outcome from each set
• Based on our example:
First set Second set
number_1 5 7
number_2 18 33
total 23 40

45
DESK CHECKING
• Create a table with the variables that are
involved in the program and pass each test
data set through the pseudocode.
Statement number_1 number_2 total
number
1. Start First pass
2. Declare variables, 1, 2, 3 5 18
number_1, number_2,
4 23
total
3. Prompt & Get 5,6 display
number_1, number_2 Second
4. Calculate total = pass
number_1 + number_2 1, 2,3 7 33
5. Display total
6. End.
4 40
5,6 display
46
YOUR TURN

• Do the desk checking for the “multiplication”


pseudocode…

47
MORE
EXAMPLES

48
YOUR TURN..

• Write a pseudocode for a program that


requires three numbers from the user. The
program shall add all three numbers and
display the result. Perform the desk check for
the program with 2 pass.

INPUT: ???
PROCESS: ???
OUTPUT: ?
49
ADD THREE NUMBERS – PSEUDOCODE

1. Start
2. Declare variables, number_1, number_2,
number_3, total
3. Get number_1, number_2, number_3
4. total = number_1 + number_2 +
number_3
5. Display total
6. End.

50
ADD THREE NUMBERS – DESK CHECKING
Statement number_1 number_2 number_3 total
number
First pass
1,2,3 10 20 30
4 60
5,6 display
Second pass
1,2,3 40 41 42
4 123
5,6 display
YOUR TURN (AGAIN…)
Complete the pseudocode and the desk check for a
program that will calculate a land price based on the
width and length of the land (keyed- in by the user).
1. Start
2. Declare variables, width, length, size, price
3. Prompt user for _________________
4. Get________________
5. size = ________________
6. price = ________* 5.40
7. Display___________
8. End
52
SUMMARY
• Algorithm = a finite step by step of
instructions to solve a problem.
• Algorithm can be implemented by using
pseudocode or flowchart.
• Desk-checking = a process of tracing
through the algorithm with several sample
data.

53

You might also like