High-level software (Ch.9)
Algorithms and data structures
Low-level software
Components
Circuits
Gates
Transistors
Remember: Decision-making instructions
2
BR Set PC to operand unconditionally
BRLT i Set PC to operand if A < 0
BREQ i Set PC to operand if A = 0
6.5 Algorithms and Pseudocode
Algorithm = A sequence of steps for solving a problem
Muḥammad ibn Mūsā al-Khwārizmī (780-850A.D.)
4
Problems with flowcharts: • They’re hard to draw in electronic documents
8
Not in text
Our text uses only pseudocode
6.5 Algorithms and Pseudocode
Pseudocode = A mixture of English and formatting to make the steps in an algorithm explicit
There are no syntax rules in pseudocode!
Pseudocode is not case sensitive!
Example: Repeated-division algorithm (convert base-10 number to other bases):
9
While ( the quotient is not zero )
Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with the quotient
… but pseudocode is more precise
11
IF concerned about cholesterol
Put butter substitute in a pot
ELSE
Put butter in a pot
Turn on burner
Put pot on the burner
WHILE (NOT bubbling)
Leave pot on the burner
Put other ingredients in the blender
Turn on blender
WHILE (more in pot)
Pour contents into lender in slow steam
Turn off blender
Pseudocode functionality
Pseudocode has all of the concepts encountered in any high-level programming language, only the syntax is informal:
– Variables
– Assignment
– I/O
– Selection / decision
– Repetition / loop
– Boolean expressions
12
We can test a pseudocode algorithm w/pencil & paper, a.k.a. “desk checking”
14
What is 93 in base 8?
93/8 gives 11 remainder 5
11/6 gives 1 remainder 3
1/ 8 gives 0 remainder 1
answer 1 3 5
While ( the quotient is not zero )
Divide the decimal number by the new base
Make the remainder the next digit to the left in the answer
Replace the original decimal number with
15
Organizing the solution in a computer-like way gives us
better idea of what is required for the computer to
execute it, e.g. two numbers need to be entered, etc.
Pseudocode for Complete Computer Solution (in Python)
Write "Enter the new base"
Read newBase
Write "Enter the number to be converted"
Read decimalNumber
Set quotient to 1
WHILE (quotient is not zero)
Set quotient to decimalNumber DIV newBase
Set remainder to decimalNumber REM newBase
Make the remainder the next digit to the left in the answer
Set decimalNumber to quotient
Write "The answer is "
Write answer
16
How to Develop an Algorithm in Pseudocode
Problem: Read in pairs of positive numbers and print each pair in order.
First version:
17
WHILE (not done)
Write "Enter two values separated by blanks"
Read number1
Read number2
Print them in order Do you see any problems with this algorithm?
In order to be able to write a computer program to implement this algorithm, several extra questions need to be settled.
We say that the algorithm is not concrete yet.
18
WHILE (not done)
Write "Enter two values separated by blanks"
Read number1
Read number2
Print them in order
19
Pseudocode – Refining the algorithm
How do we know when we’re done?
Let the user tell us how many
Where do we store the user’s response?
In a variable. Let’s call it numberOfPairs
How do we keep track of how many pairs have been entered?
With another variable! Let’s call it numberRead
How do we print the numbers in order?
If first number is smaller
print first, then second
If first number if larger
print second, then first
20
Write "How many pairs of values are to be entered?"
Read numberOfPairs
Set numberRead to 0
WHILE (numberRead < numberOfPairs)
Write "Enter two values separated by a blank; press return"
Read number1
Read number2
IF(number1 < number2)
Print number1 , " " , number2
ELSE
Print number2 ," " , number1
Make numberRead equal to itself plus 1
Second version:
Is it concrete yet?
Desk checking
Data Fill in values during each iteration 3 numberRead number1 number2
55 70
2 1
33 33
numberOfPairs
21
What is the output?
22
Translating Pseudocode into a Computer Program
What kind of program?
Machine language – Nooooooo!
Assembly language
Very detailed and time consuming, but
sometimes it needs to be done
High-level language
Python!
Read and take notes in notebook:
Software Piracy and Copyrighting
Have you every "borrowed" software
from a friend?
Have you ever "lent" software to a
friend?
Did you know that about 100,000 jobs are
lost in the US every year due to such
"borrowing" and "lending?"
24
Chapter Review Questions
• List the operations that a computer can perform
• Describe the important features of the Pep/8 virtual machine
• Distinguish between immediate addressing mode and direct addressing mode
• Write a simple machine-language program
• Distinguish between machine language and assembly language
25
Chapter Review Questions
• Describe the steps in creating and running an assembly-language program
• Write a simple program in assembly program
• Distinguish between instructions to the assembler (a.k.a. directives) and instructions to be translated into machine code (a.k.a. executable instructions)
• Distinguish between following an algorithm and developing one
26
Chapter Review Questions
• Describe the pseudocode constructs used in expressing an algorithm
• Use pseudocode to express and algorithm
27