BUILDING A PROGRAM 1 Building a program q
BUILDING A PROGRAM 1
Building a program q The steps to building a program include writing, editing, compiling, and linking code. 2
PROGRAM EXECUTION 3
Program execution 4
Algorithms + Data Structures = Programs - Niklaus Wirth, 1975. 5
CONCEPT 6
Informal definition q Informally, an algorithm is a step-by-step method for solving a problem or doing a task. q An algorithm accepts an input list of data and creates an output list of data. 7
THREE CONSTRUCTS 8
Three constructs q A program is a combination of sequence constructs, decision constructs, and repetition constructs. 9
Flowcharts for three constructs q A flowchart is a pictorial representation of an algorithm. 10
Appendix : Flowcharts SYMBOL n NAME APPLICATION Terminal Shows the beginning or end of an algorithm Flow Lines Show the action order in an algorithm Connector Shows the continuity of the algorithm on the next page 11
START and STOP START STOP 12
Connectors START 1 1 STOP 13
Sequence Symbols Assignment statement Input/output statement Module call Compound statement 14
Assignment statement variable ← expression 15
Module-Call Statement START AVRG RETURN STOP 16
Two-Way Selection F False Statement Condition T True Statement 17
for Loop End of Initialization loop action Limit test F T 18
Example 1 Write an algorithm that finds the average of two numbers 19
Algorithm 1: Average of two Average. Of. Two Input: Two numbers 1. Add the two numbers 2. Divide the result by 2 3. Return the result of Step 2 End START Input X, Y Sum = X + Y Avg = Sum / 2 Output Avg End 20
Example 2 Write an algorithm to find the largest of 1000 numbers. 21
Flow Chart START Input 1000 integers Largest = 0 i=0 While i < 1000 T integer[i] > Largest F F T Largest = integer[i] i = i + 1 Output Largest End 22
Algorithm 2: 1. 2. 3. 4. Find largest of 1000 numbers Find. Largest Input: 1000 positive integers Set Largest to 0 Set Counter to 0 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 Return Largest End 23
Example 3: Prime Number Test n Given a natural number N, where N > 1. q q If there exists an integer i, 1 < i < N, such that i can evenly divide N, then N is a composite number. Otherwise, N is a prime number. 24
START N is. Prime = true i=i+1 i=2 i<N i evenly divide N Y is. Prime = false N 25
Y is. Prime N is a prime number N N is NOT a prime number END 26
Conditional Operator n You may implement the above structure as q n if (is. Prime) cout << n << " is a prime number. " << endl; else cout << n << " is NOT a prime number. " << endl; or with a conditional operator q cout << N << " is" << ( (is. Prime)? " ": " NOT " ) << "a prime number. " << endl; 27
SUB-ALGORITHMS 28
Concept of a subalgorithm q An algorithm can be broken into smaller units called subalgorithms. START gcd(a, b) X, Y i=gcd(X, Y) i=1 “Greatest common divider = “, i . . . “They are mutual prime. ” RETURN END 29
BASIC ALGORITHMS 30
Summation 31
Bubble sort 32
Example of bubble sort 8 45 33
Example of bubble sort 8 78 8 45 34
Example of bubble sort 8 23 8 78 8 45 The smallest number is moved to the head. 35
Example of bubble sort 32 45 36
Example of bubble sort 32 78 32 45 37
Example of bubble sort 32 78 32 45 The second smallest number 38
Example of bubble sort 39
Example of bubble sort And so on … 40
Search concept q Searching, a process to locate a target in a list of data, is a basic algorithm. q Sequential search is used for unordered lists. q Binary search is used for ordered lists. 41
Example of a sequential search 42
Example of a sequential search 43
Example of a binary search Target: 22 44
Another Example The algorithm uses the following five steps to find the largest integer. 45
Defining actions in Find. Largest algorithm 46
Refinement 47
Generalization 48
- Slides: 48