Professional Documents
Culture Documents
Example pseudocode
6
Six Basic Computer Operations
7
Six Basic Computer Operations
9
Six Basic Computer Operations
10
The Structure Theorem
1. Sequence
• The sequence control structure is the straightforward
execution of one processing step after another
• In pseudocode, we represent this construct as a
sequence of pseudocode statements
12
The Three Basic Control Structures
2. Selection
• The selection control structure is the presentation of a
condition and the choice between two actions; the
choice depends on whether the condition is true or
false
• In pseudocode, selection is represented by the
keywords IF, THEN, ELSE, and ENDIF
13
The Three Basic Control Structures
3 Repetition
14
Checkpoint
18 COMP1105
Problem solving and software development
• Software Development
• Small programs (less than 5000 lines) can be reliably developed and
tested by one person
19 COMP1105
Problem solving and software development
• Software Engineering
• Software engineering (SE) is concerned with developing and
maintaining software systems that behave reliably and
efficiently, are affordable to develop and maintain, and
satisfy all the requirements that customers have defined for
them.
20 COMP1105
Problem solving process
• You need good problem solving skills to develop software
• This is NOT about learning things by rote
• To survive you MUST be able to tackle large complex problems
• If do not design and document now, you will fail in the workplace
• Imagine Microsoft Office written with no planning, organisation,
and documentation (30+ million lines of code)
• Parts written in C++
• Maintenance and debugging --> sleepless nights deciphering code!
21 COMP1105
Problem Solving: A typical software life cycle
The Waterfall
Model
23 COMP1105
Problem Solving: How we develop software
26 COMP1105
Algorithms
• Must be:
• Precise and unambiguous
• Give the correct solution in all cases
• Eventually end
Example: Describe the steps involved in going to the airport
from UWI
27 COMP1105
Algorithms
• Must be:
• Precise and unambiguous
• Give the correct solution in all cases
• Eventually end
Example: Describe the steps for passing CXC English
28 COMP1105
Algorithms
• Must be:
• Precise and unambiguous
• Give the correct solution in all cases
• Eventually end
Example: Give the algorithm for calculating simple interest on a loan
29 COMP1105
Algorithm Representation
• Words
• Pseudocode
• Diagrams
• Flow Charts
30 COMP1105
How to Write Pseudocode
How to get from inputs to outputs: the extra information we need (and can
work out) is that 1 square metre = 1.196 square yards.
So the processing is to multiply 1.196 by the input number
32 COMP1105
Pseudocode (Example 1)
An accountant needs a program to read 2 numbers, add them and
print their total.
Defining Diagram
Algorithm
Algorithm to add 2 numbers and print their sum
Begin
Define variables Num1, Num2, Sum as whole numbers
Print “Please enter two integers”
Get Num1
Get Num2
Sum <- Num1 + Num2
33
Display Sum
End COMP1105
Pseudocode (Example 2)
The same accountant needs to print the larger of the two numbers (if any).
Begin
Get Num1 from the keyboard
Get Num2 from the keyboard
IF Num1 is greater than Num2 THEN
print Num1
ELSE
IF Num2 is greater than Num1 THEN
print Num2
ELSE
Display “The numbers are equal”
ENDIF
34 ENDIF COMP1105
End
Flowchart
• Graphical representation of an algorithm using special-purpose
symbols connected by arrows (flowlines)
• Parallelogram symbol –
Input/Output operations Prompt user for Num2, Num1
Stop
if Selection structure****
Choose among alternative courses of action
• Example:
◙ Pseudocode statement to determine whether a student
has passed the course:
IF (student’s coursework grade is greater than OR equal to 50) AND
(student’s exam grade is greater than OR equal to 50)
THEN
Print “Passed”
• If the condition is true
• “Passed” is printed, control continues on to next statement
• If the condition is false
• Print statement ignored, control continues to statement after the if
• NB: Indenting makes algorithm easier to read
36 COMP1105
if Selection structure
If - Performs action if
condition true
• Flowchart of previous if/else – Performs
pseudocode statement Different actions if conditions
true or false
Example:
false
Let’s Look at an Algorithm
Strongest student in the class
Write an algorithm to record the strongest
Begin Algorithm
Declare variables
Prompt user for number of people in class
START
Read people
counter = 1
no
yes
oldest = age counter <= people Read age age > oldest
no yes
print oldest
oldest = age
END
41 COMP1105
Your turn
Tallest person in the class
Solve this problem
43 COMP1105
A Guide
For writing Pseudocode
Guidelines for pseudocode
• Declaration
• Format: Declare <variable name> of type <variable type>
• Examples
Declare variable total of type integer
Declare variable length of type float
Declare variable name of type string
Declare variable continue of type character
45 COMP1105
Guidelines for pseudocode
• Assignment
• Formatvariable = expression
• Examples
total = 0
y=x*x+z/4-1
name = "Joan Smith"
validLength = True
46 COMP1105
Guidelines for pseudocode
• Input
• Format: Input variable, variable, ...
• Examples
• Input custName
Input distance, speed
• Read custName
Read distance, speed
47 COMP1105
Guidelines for pseudocode
• Display
• Format: Display value, value, ...
Examples
• Display "Hello World"
Display totalPrice, taxPayable
Display "Customer Number: ", custNum, "Name: ", custName
48 COMP1105
Guidelines for pseudocode
• If –Else format
• IF condition THEN
statement
statement
...
ENDIF
• IF condition THEN
statements
ELSE
IF condition THEN
statements
ENDIF
ENDIF
49 COMP1105
Guidelines for pseudocode
• Examples
• IF lineNumber > 50 THEN
Display ""
Display "Student Number", "Student Name"
lineNumber = 0
ENDIF
50 COMP1105
Guidelines for pseudocode
• While
• WHILE condition DO
statement
statement
...
ENDWHILE
• Example
• count = 1
WHILE count <= 10 DO
Display count
Add 1 to count
ENDWHILE
52 COMP1105