dct 1123 problem solving & algorithms pseudocode & flowchart
TRANSCRIPT
DCT 1123 Problem Solving & AlgorithmsPseudocode & Flowchart
Con
tents
• How to write a pseudocode• Meaningful names• The structure theorem• Flowchart
SIX
basic co
mpu
ter
opera
tion
1. A computer can receive information
2. A computer can put out information3. A computer can perform arithmetic4. A computer can assign a value to a
variable or memory location5. A computer can compare two
variable and select one of two alternative actions
6. A computer can repeat a group of actions
Rece
ive In
form
atio
n
• When computer is required to receive information or input from a particular source, whether it be a terminal, a disk or any other device
• ‘Read’ and ‘Get’ are used in the pseudocode
• Read – receive input from a record/file
• Get – receive input from keyboard
Rece
ive In
form
atio
n
• For example:Read student nameGet system dateRead number1, number2Get tax code
Pu
t Ou
t Info
rmatio
n
• When a computer is required to supply information or output to a device, the verbs Print, Write, Output or Display are used
• Print – output is sent to the printer• Write – output is to be written to a
file• Put, Output and Display – output is
to be written to the screen
Pu
t Ou
t Info
rmatio
n
• For example:– Print ‘Program Completed’– Write customer record to master file– Put out name, address and postcode– Output total_tax– Display ‘End of Data’
Perfo
rm A
rithm
etic
• Most programs require the computer to perform some sort of mathematical calculation and apply a formula
• The following symbols can be written in pseudocode:– + for add– - for substract– * for multiply– / for divide– () for parantheses
• The verbs ‘Compute’ and ‘calculate’ also used in the pseudocode
Perfo
rm A
rithm
etic
• For example:– Divide total marks by student_count– Sales_tax = cost_price * 0.10– Compute C = (F – 32) * 5 / 9
Assig
n a
valu
e to
a
varia
ble
/ mem
ory
loca
tion
• There are 3 instances in which you may write a pseudocode to assign a value to a variable / memory location:1. To give data an initial value in
pseudocode, the verbs Initialize or Set are used
2. To assign a value as a result of some processing, the symbols ‘=‘ or ‘’ are written
3. To keep a variable for later use, the verbs Save or Store are used
Assig
n a
valu
e to
a
varia
ble
/ mem
ory
loca
tion
• For example:– Initialize total_price to zero– Set student_count to 0– Total_price = cost_price + sales_tax– Total_price cost_price + sales_tax– Store customer_no in last_customer_no
Com
pare
two v
aria
ble
s & se
lect
on
e o
r two a
ltern
ativ
e a
ction
• To represent this operation in pseudocode, special keywords are used: IF, THEN and ELSE
• The comparison of data is established in the IF clause
• The choice of alternatives is determined by the THEN or ELSE
Com
pare
two v
aria
ble
s & se
lect
on
e o
r two a
ltern
ativ
e a
ction
• For example:
IF attendance_status is part_time THENAdd 1to part_time_count
ELSEAdd 1 to full_time_count
ENDIF
Repeat a
gro
up
of a
ction
• When there is a sequence of processing steps that need to be repeated, two special keywords, DOWHILE and ENDDO are used in pseudocode
• The repetition condition is established in the DOWHILE clause and the actions to be repeated are listed beneath it
Repeat a
gro
up
of a
ction
• For example:
DOWHILE student_total < 50Read student recordPrint student name, address to reportAdd 1 to student_total
ENDDO
Mean
ing
ful N
am
es
• A programmer must introduce some unique names, which will be used to represent the variables or objects in the problem
• A name given to a variable is simply a method of identifying a particular storage location in the computer
• A name describes the type of data stored in a particular variable
• A variable may be one of three simple data types: an integer, a real number or a character
Mean
ing
ful N
am
es
• If more than one word is used in the variable name, the underscores are useful as word separator. For example student_number
• If underscore cannot be used, then words can be joined together with the use of a capital letter as a word separator. For example studentNumber
Flo
wch
art
• Flowchart is an alternative method of representing algorithms
• It is popular because they graphically represent the program logic through series of standard geometric symbols and connecting lines
Flo
wch
art S
ym
bols
Terminal SymbolIndicates the starting or stopping point in the logic. Every flowchart should begin and end with a terminal symbol.
Input / Output SymbolRepresents an input or output process in an algorithm, such as reading input or writing output
Flo
wch
art S
ym
bols
Process SymbolRepresents any single process in an algorithm, such as assigning a value or performing a calculation
Predefined Process SymbolRepresents an input or output process in an algorithm, such as reading input or writing output
Flo
wch
art S
ym
bols
Decision SymbolRepresents a decision in the logic involving the comparison of two values. Alternative paths are followed, depending on whether the decision symbol is true or false
FlowlinesConnect various symbols in a flowchart, and contain an arrowhead only when the flow control is not from top to bottom or left to right
Flow
chart C
ontro
l Stru
cture
• There are three basic control structures:1. Sequence
• Straightforward execution of one processing step after another
2. Selection• Presentation of a condition, and the choice
between two actions depending on whether the condition is true or false
3. Repetition• Presentation of a set of instructions to be
performed repeatedly, as long as condition is true
Flow
chart C
ontro
l Stru
cture
• Sequence flowchart example:
Statement A
Statement C
Input / Output
Flow
chart C
ontro
l Stru
cture
• Selection flowchart example
Condition p?
Statement A Statement B
True False
Flow
chart C
ontro
l Stru
cture
• Repetition Flowchart Example
Condition p?
Statement A
True
False
Su
mm
ary
• Six basic computer operations were listed – receive information, put out information, perform arithmetic, assign a value o a variable, decide between two alternative actions and repeat a group of actions
• The algorithm can be represented in the pseudocode or flowchart