0% found this document useful (0 votes)
4 views

Unit1_5

Chapter 5 discusses algorithms and software, emphasizing the problem-solving process on computers, which includes algorithm development, implementation, and maintenance. It covers the representation of algorithms through flowcharts and pseudocode, as well as the classification of computer software into system and application software. Additionally, it highlights various disciplines in computer science, such as information systems, artificial intelligence, and simulation.

Uploaded by

phuonglinhiutui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit1_5

Chapter 5 discusses algorithms and software, emphasizing the problem-solving process on computers, which includes algorithm development, implementation, and maintenance. It covers the representation of algorithms through flowcharts and pseudocode, as well as the classification of computer software into system and application software. Additionally, it highlights various disciplines in computer science, such as information systems, artificial intelligence, and simulation.

Uploaded by

phuonglinhiutui
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

Chapter 5

Algorithm and Software

1
Contents
5.1. Problem Solving on Computers
5.2. Algorithm.
5.3. Representation of algorithm
5.4. Programs and Programming Languages
5.5. Classification of Computer Software
5.6. Disciplines of Computer Science

2
Problem Solving
• The art of finding a solution to a perplexing
question
• How to solve it?
• Understanding the problem
• Devising a plan
• Carrying out the plan
• Looking back

3
Problem Solving on Computers
• Problem Solving
• Computer Problem-Solving
• The process of solving problem with computers

4
Computer Problem-Solving

Three important phases:


• Algorithm Development: analyze, propose
algorithm, test algorithm
• Implementation: code, test
• Maintenance:use, modify the program to meet
chaining requirement or to correct errors.

5
The process of solving problem with
computers

6
What’s an Algorithm?
• Shampoo Algorithm
Step 1: Wet hair
Step 2: Lather
Step 3: Rinse
Step 4: Repeat
• Is this enough information?

8
What’s an Algorithm?
• Which step will be repeated? How many times do
we need to repeat it?
• Shampoo Algorithm (Revision #1):
Step 1: Wet hair
Step 2: Lather
Step 3: Rinse

.-.
Step 4: Repeat Steps 1-4
• How many times do we repeat step 1? Infinitely? Or
at most 2 times?

9
What’s an Algorithm?
• Keep a count of the number of times to repeat the steps
• Repeat the algorithm at most 2 times
• Shampoo Algorithm (Revision #2)
Step 1: Set count = 0
Step 2: Wet hair
Step 3: Lather

.-.
Step 4: Rinse
Step 5: Increment count (increase its value by 1)
Step 6: If count < 2 repeat steps 2-6
Step 7: EXIT
• Will execute steps 2-6 two times

10
Algorithm
• Unambiguous instructions for solving a problem or
subproblem in a finite amount of time using a finite
amount of data
• Simple algorithms can be expressed using
• Flowchart
• Pseudo code

.-.
• Natural Language
• Programming Language

11
Characteristics of algorithms

• Exactness
• Effectiveness
• Guaranteed termination
• Generality

.-.
12
Representation of Algorithm
• Flowchart
• Pseudocode

13
Algorithm Building Blocks
All problems can be solved by employing any one of
the following building blocks or their combinations

1. Sequences
2. Conditionals
3. Loops

.-.
14
Start or stop

Review of Process

Flowchart
Elements Input or output

Decision

Flow line

.-. 15
Sequences
A sequence of instructions that are executed in the precise
order they are written in:

statement block 1
statement block 1
statement block 2

.-.
statement block 2
statement block 3
statement block 3

16
Conditionals
Select between alternate courses of action depending
upon the evaluation of a condition

True False
If ( condition = true ) condition

statement block 1
Else statement statement

.-.
block 1 block 2
statement block 2
End if

17
Loops
Loop through a set of statements as long as a condition is
true

Loop while ( condition = true )

.-.
statement block
statement
End Loop condition
block True

False

18
Directions for pseudo code
• No rule for writing pseudo code
• A Pascal-like language
• Use syntax structures of Pascal together with
descriptions in natural languages.

.-.
19
Pseudo code for assignment
• Purpose:
• Set a value for a variable
• Expression:
Max := a1
Max  a1
n n+1

20
Pseudo code for Conditionals
if <condition> then <action>
endif

or
if < condition > then < action >
else < action >
endif

21
Pseudo code for Loops
• while <condition> do
<actions>
end while
• repeat
<actions>
until <condition>
• for <variable> <initial value> to <last value> do
<action>
end for
• for <variable>  <initial value> downto <last value> do
<action>
end for
22
Pseudo code for Unconditional Jump

Jump to statement with label L

goto L

23
Pseudo code Functions/procedures
• Function declaration
Function <identifier>(<list of parameters>)
action with parameters
return <value>
End Function
• Call a function
[Call] <Function>(< list of parameters >)

24
Algorithm

Convert a decimal number into binary

.-.
25
Convert 75 to Binary
2 75 remainder
2 37 1
2 18 1
2 9 0
2 4 1
2 2 0
2 1 0
0 1

1001011
26
Algorithm in English
Input: Decimal integer x, x > 0
Output: binary string y equivalent to x
Method:
Step1: Input x
Step 2: y :=‘’
Step 3: if (x=0) go to step 9
Step 4: quotient := x div 2
Step 5: remainder:= x mod 2
Step 6: y:= CONCAT (remainder,y)
Step 7: x:= quotient
Step 8: go to step 3
Step 9: print y
Step10: Stop

27
Notation
• ‘’ Empty string
• div integer division
• mod modulus (return remainder of a division)
• CONCAT concatenation of two strings, for
instance, concatenation of string ‘park’ and ‘ing’ is

.-.
‘parking’
• := is the assignment

28
Start
Flowchart of Decimal
Get x
y:=‘’ to Binary Conversion

Yes quotient := x div2 y = CONCAT(remainder, y)


x >0? remainder:= x mod 2
x = quotient
No
x is the decimal number
Print y y is the binary equivalent

Stop .-. 29
Solution in Pseudo Code
Input: Decimal integer x, x > 0
Output: binary string y equivalent to x
Method:
input x
y := ‘’
repeat
quotient := x div2

.-.
remainder:= x mod 2
y := CONCAT( remainder, y )
x := quotient
until x= 0
print y
stop

30
Q: Is this the only possible algorithm for converting a
decimal number into a binary representation?

If not, then is this the best?


In terms of speed?
In terms of memory requirement?
In terms of ease of implementation?

You must ask these questions after writing any


algorithm!

.-. 31
Tips on Writing Good Pseudo Code

• Use indention for improved clarity


• Do not put “code” in pseudo code – make your pseudo
code language independent
• Don’t write pseudo code for yourself – write it in an

.-.
unambiguous fashion so that anyone with a reasonable
knowledge can understand and implement it
• Be consistent
• Prefer formulas over English language descriptions
32
Questions
1. Does the flowchart depict the “correct” algorithm?
2. What do we mean by “correct”, or better yet, what
do we check for “correctness”?
3. One way is to check the algorithm for a variety of
inputs
4. Does it perform satisfactorily for:

.-.
• x=0?
• negative numbers?
• numbers with fractional parts?

33
Another Example: Sorting

Sort the following objects w.r.t. their heights

.-.
34
Expected Result

.-.
35
Strategy
There are many strategies for solving this problem. We
demonstrate a simple one:
Repeat the following steps while the list is un-sorted:
Start with the first object in the list

.-.
Swap it with the one next to it if they are in the wrong order
Repeat the same with the next to the first object
Keep on repeating until you reach the last object in the list

36
Back to the Objects to be Sorted

.-.
37
Sorting: Step A1

.-.
38
Sorting: Step A1

Swap? Yes

.-.
39
Sorting: Step A2

.-.
40
Sorting: Step A2

Swap? Yes

.-.
41
Sorting: Step A3

.-.
42
Sorting: Step A3

Swap? No

.-.
43
Sorting: After Step A7

.-.
44
Sorting: Step B1

.-.
45
Sorting: Step B1

Swap? Yes

.-.
46
Sorting: Step B2

.-.
47
Sorting: Step B2

Swap? No

.-.
48
Sorting: After Step B6

.-.
49
Sorting: Step C1

.-.
50
Sorting: Step C1

Swap? No

.-.
51
Sorting: After Step C5

.-.
52
Sorting: After Step D4, E3, F2

.-.
53
STOP
.-. 54
Flowchart for the Sorting Process
a is an
Input N and
array the list a1, a2,…,aN
containing
the heights MN
N is the
T Print
total M<2
the sorted list
number of F
objects in M  M – 1, i  0
the list
ii+1
F
ai  ai+1
T F
i>M ai > ai+1
T
55
Is this the only possible algorithm for sorting a list?

Certainly not! In fact this one (called the “Bubble sort”) is


probably the worst (reasonable) algorithm for sorting a list
– it is just too slow

You will learn a lot more about sorting in your future

.-.
courses

56
Pros and Cons of Flowcharts
• Symbols are quite intuitive and almost universally
understood
• Graphical nature makes the process of explaining an
algorithm quite straightforward

.-.
• The process of writing an algorithm is too cumbersome
• Converting graphical form into code is not straight forward
• Another kind of flowcharts – Structured Flowcharts

57
Pros and Cons of Pseudo Code
• Closer in form to real code
• Use pseudo code as a starting point or outline for
writing real code

• Converting each line into real code is not easy

.-.
• No standard rules exist for writing pseudo code

58
Program and Programming
Languages
• Program
• Programming languages
• Software Development Cycle

59
Program

• A sequence of instructions written to perform a

.-.
specified task

60
Programming Language
• An artificial language that can be used to control the
behavior of a machine, particularly a computer.
• Is defined through the use of syntactic and semantic
rules
• Facilitate the task of organizing and manipulating
information

.-.
• Express algorithms precisely.

61
Machine Language
• System of instructions directly executed
by a computer's CPU
• The lowest-level of abstraction
• Instructions are patterns of bits

.-.
corresponding to different commands of
the machine.

62
Assembly

• Low-level language
• Implements a symbolic representation of the numeric
machine codes of a particular CPU architecture.
• Is specific to a certain physical or virtual computer
architecture

.-.
• Assembler : a program to translate assembly language
statements into the target computer's machine code.

63
High-level languages
• More abstract, easier to use,
• More portable across platforms.
• Examples: Pascal, C, Visual Basic, SQL, . . .
.
• Abstract away CPU operations

.-.
• They have been implemented by translating
to machine languages

64
5.5. Classification of Computer Software

• System software
• Application software

.-. 66
Classification of Computer Software

• Application software
• Programs that help us to solve real-world problems

• System software
• Programs that manage the computer system and
interact with the hardware

.-.
67
Application Software
• Application software is the software that
has made using computers indispensable
and popular

• Common application software

.-.
• Word processors
• Electronic spreadsheet
• Media players
• Database management systems
• Drawing programs

68
System Software

• Operating systems
• Translation systems
• Utilities

.-.
69
Translation System
• Set of programs used to develop software
• A key component of a translation system is a
translator
• Some parts of translators
• Compiler
• Converts from one language to another
• Linker

.-.
• Combines resources
• Examples
• Microsoft Visual C++, CBuilder
• Performs compilation, linking, and other activities.

70
Types of Translators
• Compiler is a program that translate source code
from a high-level programming language to a lower
level language (e.g., assembly language or machine
language)

• Interpreter is a program that translates and

.-.
executes source language statements one line at a
time

71
Utilities
• Virus Scanning software
• Backup Software
• Scan Disk
• Disk Defragmenter

.-.
72
5.6.Disciplines of Computer
Science

• Information Systems
• Artificial Intelligence
• Simulation

.-.
73
Information Systems
• Software that helps us manage and analyze data
• Data reflect almost every aspect of our lives
• Database : a structured set of data
• Typical tools to construct information systems :
electronic spreadsheets , database management

.-.
systems

74
Artificial Intelligence (AI)
• The study of computer systems that model and
apply the intelligence of the human mind
• Aspects of AI
• Knowledge representation
• Expert Systems
• Neural networks

.-.
• Natural Language Processing
• Robotics

75
Simulation
• Developing a model of a complex system and
experimenting with the model to observe the result
• Examples:
• Queuing Systems
• Weather Forcasting
• Computer Aid Design (CAD)

.-.
• Embedded System

76

You might also like