Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

FBT0015

STRUCTURED
ALGORITHM &
PROGRAMMING
INTRODUCTION TO PROBLEM SOLVING
FLOWCHART
LEARNING OUTCOME
By the end of this chapter, you will be able
to:
+ List the component in problem solving
+ Define algorithm
+ Differentiate between pseudocode and flowchart
+ Solve a given problem by applying flowchart
+ Compare and contrast control structures with its
flowchart
+ Solve a given problem by applying pseudocode
technique
+ Perform desk-checking 2
PROBLEM SOLVING
• Understand and analyze the problem
• Includes 3 components:

INPUT PROCESS OUTPUT

• List of • Actions • Outcome


things that that need desired
is required to be done
by the to get
problem output

3
PROBLEM SOLVING
A café needs a computerized program to find its weekly
profitable menu. The worker should key in data that
includes menu name, quantity sold and its price. Then, the
program should be able to calculate the total sale for each
menu ordered by the customer. If the total sale 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

Which data that we need?


Where do we get the data? Should we get it from user
by asking them to key in the data or download the
data from file?
If data is from user, how do we make sure user key-in
all required data?
How do we compute the total sale for each menu?
How do we share the information about the profitable
menu with user ?Should we display on screen or print
it on paper?
How do we display the profitable menu name? Drop
down list? Clickable buttons?
ALGORITHM

✕A step-by-step procedure to solve a given


problem, consist 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. 7
EXAMPLE OF ALGORITHM
How to add up a list of item prices by using a calculator?

Switch on the 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
Press equal (=) key to view the total amount
Switch off calculator

8
..ALGORITHM
Can be implemented by using either:

OR

PSEUDOCODE FLOWCHART
9
FLOWCHART

• 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.
FLOWCHART SYMBOLS

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

Symbol Purpose
Process Symbolizes any process in an
algorithm. Example of processes:
Declare variables, perform
calculations and assign values to
variables.
Predefined Indicates a module in an algorithm.
process In other words, the module can be
expended to include few sub
processes.
FLOWCHART SYMBOLS

Symbol Purpose
to be use 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).

use to connect various symbols in


Flow lines flowchart. The line and its arrowhead
indicate the logic flow of control.
BASIC CONTROL STRUCTURES

There are 3 basic control structures:

SEQUENTIAL
SELECTION
REPETITION
1. SEQUENTIAL

• straightforward execution of one processing


step after another
• basic computer operations
1. receive information
2. assign values
3. perform arithmetic
4. display information
How do you boil water on stove??

Any other
possible ways??
1. SEQUENTIAL
How to add 2 numbers START
by using a computer
program? Declare variables
Find the IPO.. num_1,num_2,total

1. Start
Get num_1,num_2
2. Declare variables, num_1,
num_2, total
total=num_1+num_2
3. Get num_1 + num_2
4. total = num_1 + num_2 Display total

5. Display total
END
6. End
1. SEQUENTIAL

Think Time-Flow chart

Draw a flow chart for a computer program that will get two
test results from the user, calculate the average of the test
results and display the calculation result .
2. SELECTION
• Condition:
• based on the condition criteria, certain actions will be
executed, and some will not
• Condition can either be:
• true or false
• yes or no
• We could also have multiple conditions/ options
2. SELECTION
• How to have decision/ selection in flowchart?

If else Case statement


2. SELECTION

Example (life scenario)


You are trying to decide on what
to do over the weekend. Since
you only own a bicycle, weather
is a factor of concern to you. If it
rains, you decide to finish up your
assignment. If it does not rain,
you will cycle to D’ Mall, Seri
Iskandar and watch movie at the
cinema.
2. SELECTION
Think Time-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.
2. SELECTION

Think Time-Flow chart (computer


system)
Draw a flow chart that will get result for test 1 and test 2
from a user. Then, calculate the average of both results. 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..”
24
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 to be repeated will not be executed anymore
3. REPETITION
• I would like to
add sugar into
Start
my cup of
coffee.
However, that Coffee is FALSE
would depend Not
on how sweet it Sweet?
will taste after TRUE
each additional
spoon of sugar. Add Sugar

End
26
3. REPETITION

Think Time Flowchart


Draw a flow chart that will get result for test 1 and test 2
from a user. Then, calculate the average of both results. 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 multiple set of test results as per
user wish. The program will prompt the user whether they
wish to stop entering result or not. User will have to
press “Q” for quit.
3. REPETITION

Think Time Flowchart


Draw a flow chart for a program that will get the name and final
exam results for all FBT0015 students (474); and calculate the total
scores for all students. After all results were entered, the average
result will be calculated and displayed on screen.
3. REPETITION

Think Time Flowchart


Draw a flow chart for a program that will receive the name and
final exam results for all FBT0015 students; as long as the result
entered by user is not more than 100 or less than 0.Then, calculate
the total scores of all results. If the entered result is more than 100
or less than 0, the average will be calculated and displayed on
screen.
FBT0015
STRUCTURED
ALGORITHM &
PROGRAMMING
INTRODUCTION TO PROBLEM SOLVING
PSEUDOCODE
LEARNING OUTCOME

✕ By the end of this chapter, you will be able to:


+ Solve a given problem by applying pseudocode
technique
+ Perform desk-checking

3
0
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
6. Display size to understand the set of
instructions you write.
7. End So, meaningful words &
phrases will make it easier
to translate pseudocode to
programming language later.
31
PSEUDOCODE

MEANINGFUL NAMES
✕ Meaningful names should be used to represent
variables in our pseudocode/flowchart
✕ number1 vs n1<-use meaningful naming
✕ majority of programming languages does not
allow gap (space) in variable names
Eg. Total Sales X

✕ Underscore_is used if there are more than one


word in the variable or alternatively, use capital
letters to separate words.
Eg. total_sales √
totalSales √ 32
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
33
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. 34
PSEUDOCODE
✕ A computer can:
+ receive information To be discussed in this
+ put out information chapter

+ perform arithmetic calculation


+ assign a value to a variable or memory
location
+ compare two variables and select one To be
discussed in
of two alternative actions the next few
chapters
+ repeat a group of actions
**all keywords discussed here are also
applicable to flowchart 35
COMPUTER CAN
RECEIVE
INFORMATION
14
PSEUDOCODE
KEYWORDS FOR INPUT
✕ ‘Prompt’ and ‘Get’:
use to indicate that input will be
receive from the keyboard.

✕ ‘Read’:
use to indicate that input will be
retrieve from record in file.

37
PSEUDOCODE

KEYWORDS FOR 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

38
COMPUTER
CAN PUT OUT
INFORMATION

17
PSEUDOCODE

KEYWORDS for 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’
*to print to printer the quoted words
Output total_tax
*to display the value of variable total_tax. Not to
display the word total_tax.
COMPUTER
CAN PERFORM
ARITHMETIC
CALCULATION
Source: www.odyssey-resources.com
1
9
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 20
PSEUDOCODE
EXAMPLES
& DESK CHECKING
HOW TO WRITE PSEUDOCODE

Problem
You need to write pseudocode for a program that
will add up two numbers

STEP 1. Identify the Input, Process and Output (IPO)

Input Process Output


number_1 Get two numbers total
number_2 Add the numbers together
Display the total
44
HOW TO WRITE PSEUDOCODE
✕ 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.

45
YOUR TURN….

✕A program is to receive three numbers from


user, multiply all three numbers and display
the result. Write the pseudocode..

✕ INPUT: ???
✕ PROCESS: ???

✕ OUTPUT: ?

24
STEP 1. Identify the Input, Process &
Output

1 2 3
INPUT: PROCESS: OUTPUT:
declare variables,
number1, multiplication result
get three numbers,
number2, multiply all numbers,
number3 display multiplication
result
STEP 2. Write pseudocode.
1. Start
2. Declare variables, number1,
number2,number3, result
3. Prompt and Get number1,
number2,number3
4. result = number1 * number2 * number3
5. Display result
6. End.
start

Declare Avg, StudentName, ExamScore, TotalExamScore, TotalStudent

Set TotalStudent 0, TotalExamScore as 0

TotalStudent< No
492
Yes Yes
CAN YOU WRITE THE Prompt & Get StudentName,
PSEUDOCODE FOR ExamScore
THIS FLOW CHART?
Calculate Avg=TotalExamScore/
TotalStudent
Increase TotalStudent

Calculate
TotalExamScore=TotalExamScore
+ExamScore

Print AverageScore
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

28
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
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
2. Declare variables, pass
number_1, number_2,
1,2,3 5 18
total
3. Prompt & Get 4 23
number_1, number_2 5,6 display
4. Calculate total = Second
number_1 + number_2 pass
5. Display total
6. End.
1,2,3 7 33
4 40
5,6 display30
YOUR TURN

✕ Do the desk checking for the “multiplication”


pseudocode…

31
MORE
EXAMPLES

32
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: ?
33
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

56
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.

57

You might also like