Chap8 Flowchart
Chap8 Flowchart
8.1
CONCEPT
Informal definition
Informally, an algorithm is a step-by-step method
for solving a problem or doing a task.
An algorithm accepts an input list of data and
creates an output list of data.
!
Example
The algorithm uses the following five steps to find the largest
integer.
Defining actions in FindLargest algorithm
"
Refinement
Generalization
#
8.2
THREE CONSTRUCTS
Three constructs
A program is a combination of sequence constructs,
decision constructs, and repetition constructs.
$
8.3
ALGORITHM
REPRESENTATION
Flowcharts for three constructs
A flowchart is a pictorial representation of an
algorithm.
%% &'()*+ ,
START
STOP
!
) &&
START 1
1 STOP
0 & 1 2
Assignment statement
Input/output statement
Module call
Compound statement
"
& & &
variable expression
3 ' ) &
START AVRG
AVRG
RETURN
STOP
#
-, 1 &
F T
Condition
$
Pseudocode for three constructs
Pseudocode is an Englishlike representation of an
algorithm.
Example 1
Solution
See Algorithm 8.1 on the next slide.
START
Algorithm 8.1: Average of two
AverageOfTwo Input X, Y
Input: Two numbers
1. Add the two numbers Sum = X + Y
2. Divide the result by 2
3. Return the result of Step 2 Avg = Sum / 2
End
Output Avg
End
Example 5
Solution
See Algorithm 8.5 on the next slides.
!
START
+ ,) Input 1000 integers
Largest = 0
i=0
F
While i < 1000
T
integer[i] > Largest
F
T
Largest = integer[i]
i = i + 1
Output Largest
End
Algorithm 8.5: Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
"
8.4
MORE FORMAL
DEFINITION
SUBALGORITHMS
#
Concept of a subalgorithm
An algorithm can be broken into smaller units called
subalgorithms.
START gcd(a, b)
X, Y
i=gcd(X,Y)
.
i=1 .
.
“Greatest common “They are
divider = “, i mutual prime.”
RETURN
END
8.6
BASIC
ALGORITHMS
$
Summation
Bubble sort
Example of bubble sort
8 45
Example of bubble sort
8 78
8 45
!
Example of bubble sort
8 23
8 78
8 45
32 45
"
Example of bubble sort
32 78
32 45
Example of bubble sort
32 78
32 45
#
Example of bubble sort
Example of bubble sort
And so on …
!$
Search concept
Searching, a process to locate a target in a list of
data, is a basic algorithm.
Sequential search is used for unordered lists.
Binary search is used for ordered lists.
!
Example of a sequential search
!
Example of a sequential search
!
Example of Target: 22
a binary search
!!
Chapter 9:
Programming
Languages
!
9.1
EVOLUTION
!"
Evolution of computer languages
!
Program 9.1 Program in machine language
1 00000000 00000100 0000000000000000
2 01011110 00001100 11000010 0000000000000010
3 11101111 00010110 0000000000000101
4 11101111 10011110 0000000000001011
5 11111000 10101101 11011111 0000000000010010
6 01100010 11011111 0000000000010101
7 11101111 00000010 11111011 0000000000010111
8 11110100 10101101 11011111 0000000000011110
9 00000011 10100010 11011111 0000000000100001
10 11101111 00000010 11111011 0000000000100100
11 01111110 11110100 10101101
12 11111000 10101110 11000101 0000000000101011
13 00000110 10100010 11111011 0000000000110001
14 11101111 00000010 11111011 0000000000110100
15 00000100 0000000000111101
16 00000100 0000000000111101
!#
Note:
!
Evolution of computer languages
A symbolic language uses symbols to represent
various machine language instructions. Symbolic
languages are also called assembly languages.
A high-level language is portable from one computer
type to another and free the programmer from one
computer type to another and frees the programmer
from hardware concerns. BASIC, Pascal, Ada, C,
C++, and Java are high-level languages.
Natural language
$
Program 9.2 Program in symbolic language
1 Entry main, ^m<r2>
2 subl2 #12,sp
3 jsb C$MAIN_ARGS
4 movab $CHAR_STRING_CON
5
6 pushal -8(fp)
7 pushal (r2)
8 calls #2,read
9 pushal -12(fp)
10 pushal 3(r2)
11 calls #2,read
12 mull3 -8(fp),-12(fp),-
13 pushal 6(r2)
14 calls #2,print
15 clrl r0
16 ret
Program 9.3 Program in C++ language
1 /* This program reads two integer numbers from the
2 keyboard and prints their product.
3 */
4 #include <iostream.h>
5
6 int main (void)
7 {
8 // Local Declarations
9 int number1;
10 int number2;
11 int result;
12 // Statements
13 cin >> number1;
14 cin >> number2;
15 result = number1 * number2;
16 cout << result;
17 return 0;
18 } // main
9.2
BUILDING
A
PROGRAM
Building a program
The steps to
building a
program
include
writing,
editing,
compiling,
and linking
code.
!
9.3
PROGRAM
EXECUTION
Program execution
"
3 '- 6(
Close Book
You have seen all the English vocabularies in the textbook
or the homework, so do not ask the TA to explain the
questions.