Additional Chapter 1

advertisement
DCT 1123
Problem Solving &
Algorithms
Pseudocode & Flowchart
Contents
•
•
•
•
How to write a pseudocode
Meaningful names
The structure theorem
Flowchart
SIX basic computer operation
1.
2.
3.
4.
A computer can receive information
A computer can put out information
A computer can perform arithmetic
A computer can assign a value to a
variable or memory location
5. A computer can compare two
variable and select one of two
alternative actions
6. A computer can repeat a group of
actions
Receive Information
• 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
• For example:
Receive Information
Read student name
Get system date
Read number1, number2
Get tax code
Put Out Information
• 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
• For example:
Put Out Information
–
–
–
–
–
Print ‘Program Completed’
Write customer record to master file
Put out name, address and postcode
Output total_tax
Display ‘End of Data’
Perform Arithmetic
• 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
• For example:
Perform Arithmetic
– Divide total marks by student_count
– Sales_tax = cost_price * 0.10
– Compute C = (F – 32) * 5 / 9
Assign a value to a variable /
memory location
• 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
Assign a value to a variable /
memory location
• 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
Compare two variables & select
one or two alternative action
• 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
Compare two variables & select
one or two alternative action
• For example:
IF attendance_status is part_time THEN
Add 1to part_time_count
ELSE
Add 1 to full_time_count
ENDIF
Repeat a group of action
• 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 group of action
• For example:
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO
Meaningful Names
• 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
Meaningful Names
• 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
Flowchart
• 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
Flowchart Symbols
Terminal Symbol
Indicates the starting or stopping point in
the logic. Every flowchart should begin
and end with a terminal symbol.
Input / Output Symbol
Represents an input or output process in
an algorithm, such as reading input or
writing output
Flowchart Symbols
Process Symbol
Represents any single process in an
algorithm, such as assigning a value or
performing a calculation
Predefined Process Symbol
Represents an input or output process in
an algorithm, such as reading input or
writing output
Flowchart Symbols
Decision Symbol
Represents 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
Flowlines
Connect various symbols in a flowchart,
and contain an arrowhead only when
the flow control is not from top to bottom
or left to right
Flowchart Control Structure
• 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
Flowchart Control Structure
• Sequence flowchart example:
Statement A
Input / Output
Statement C
Flowchart Control Structure
• Selection flowchart example
True
Statement A
Condition
p?
False
Statement B
Flowchart Control Structure
• Repetition Flowchart Example
Condition
p?
True
Statement A
False
Summary
• 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
Download