advertisement

Chapter 5 Algorithms and Programming Languages 1 Outline Concepts – Definition – How to develop an algorithm – Essential features Three Constructs Algorithm Representation – Pseudocode – Flowchart – Structure chart Basic algorithms Recursion 2 Outline • • • • Evolution Categories of languages Building programs Elements of programming languages 3 Objectives • Define algorithms. • List the 5 essential properties of an algorithm. • Define and use the three constructs for developing algorithms: sequence, decision, and repetition. • Understand and use three tools to represent algorithms: flowchart, pseudocode, and structure chart. • Understand the concept of modularity and subalgorithms. • List and comprehend common algorithms, such as sorting and searching. • Differentiate between recursion and iteration. 4 Objectives • Have a vision of computer language evolution. • Distinguish between machine, assembly, and highlevel languages. • Distinguish between the different categories of languages: procedural, object-oriented, functional, declarative, and special. • Understand the process of creating and running a program. • Become familiar with elements of programming languages. 5 part 1 DEFINITION 6 Informal definition 7 Example--Finding the largest integer among five integers 8 Defining actions 9 Refinement • 2 problems with the previous method: – The action in the first step is different from the ones for the other steps. – The wording is not the same in steps 2 to 5. 10 Generalization 11 Formal definition • Algorithm: an ordered set of unambiguous steps that produces a result and terminates in a finite time. • It is a step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps. • It is a precise, systematic method for producing a specified result. 12 Specification • The specification of an algorithm must be “precise”. • To write a precise algorithm, we need to pay attention to three things: – Capability. Make sure that the computer knows what and how to do the operations. – Language. Ensure that the description is unambiguous—that is, it can only be read and understood one way. – Context. Make few assumptions about the input and execution setting. 13 5 essential properties of algorithms • To write algorithms that are specified well enough for a computer to follow, make sure every algorithm has five essential properties: – – – – – Inputs specified Outputs specified Definiteness Effectiveness Finiteness 14 Input specified • The inputs are the data that will be transformed during the computation to produce the output. • We must specify the type of the data, the amount of data, and the form that the data will take. • Suppose the algorithm is a recipe. We must list the ingredients(type of inputs), their quantities(amount of input), and their preparation, if any(form of inputs), as in “1/4 cup onion, minced.” 15 Outputs specified • The outputs are the data resulting from the computation, the intended result. • Often the description is given in the name of the algorithm, as in “Algorithm to compute a batting average.” • As with inputs, we must specify the type, amount, and any form of the outputs. “3 dozen 3-inch chocolate chip cookies.” • A possible output for some computation is a statement that there can be no output—that is, no solution is possible. 16 Definiteness • Algorithms must specify every step. • Definiteness means specifying the sequence of operations for transforming the inputs into the outputs. • Every detail of each step must be spelled out, including how to handle errors. • Definiteness ensures that if the algorithm is performed at different times or by different agents (people or computers) using the same data, the output will be the same. 17 Effectiveness • It must be possible for the agent to execute the algorithm mechanically without any further inputs, special talent, clairvoyance, creativity, help from Superman, and so on. • Whereas definiteness specifies which operations to do, effectiveness means that they are doable. • Examples of ineffectiveness: “Enter the amount of income you would have received this year had you worked harder”, “Print the length of the longest run of 9s in the decimal expansion of π” 18 Finiteness • An algorithm must have finiteness; it must eventually stop, either with the right output or with a statement that no solution is possible. • If no answer comes back, we can’t tell whether the algorithm is still working on an answer or is just plain “stuck”. • Finiteness becomes an issue for computer algorithms because if the algorithm doesn’t specify when to stop the repetition, the computer will continue to repeat the instructions forever. For example, divide 3 into 10. • Any process having these five properties will be 19 called an algorithm. part 2 3 CONSTRUCTS 20 3 Constructs • Computer scientists have defined three constructs for a structured program or algorithm. • The idea is that a program must be made up of a combination of only these three constructs: sequence, decision (selection), and repetition. 21 Sequence • An algorithm, and eventually a program, is a sequence of instructions, which can be a simple instruction or either of the other two constructs. • A program is an algorithm that has been customized to solve a specific task under a specific set of circumstances in a specific language. 22 Decision • Some problems can not be solved with only a sequence of instructions. • Sometimes you need to test a condition. • If the result of testing is true, you follow a sequence of instructions; if it is false, you follow a different sequence of instructions. • This is called the decision (selection) construct. 23 Repetition • In some problems, the same sequence of instructions must be repeated. • You handle this with the repetition (loop) construct. 24 part 3 Algorithm Representation 25 Algorithm Representation • We can use different tools to represent algorithms: – Pseudo-code – Flowchart – Structure chart 26 Pseudocode • It is an English-like representation of the code required for an algorithm. It is part English and part structured code. – English part provides a relaxed syntax that is easy to read. – Code part consists of an extended version of the basic algorithmic constructs (sequence, decision and repetition). • Until now, there is no standard for pseudocode. 27 Pseudocode example Algorithm: Finding Smallest Purpose: This algorithm finds the smallest number among a list of numbers. Pre: List of numbers Post: None Return: The Smallest 1. Set smallest to the first number 2. loop (not end of list) 2.1 if (next number < smallest) 2.1.1 set smallest to next number 2.2 end if 3. end loop 4. Return smallest End finding Smallest 28 Pseudocode • An algorithm written in pseudocode can be decomposed into several elements and constructs. • Header: name the algorithm. • Purpose, precondition, postcondition and return: – The purpose is a short statement about what the algorithm does; – The precondition lists any requirements; – The postcondition identifies any effect created; – The return shows what is returned from the algorithm. If there is nothing to be returned, the null should be specified. • Statement numbers: list execution order. • Statement constructs: based on three constructs. 29 Pseudocode 30 Example 1 Write an algorithm in pseudocode that finds the average of two numbers Solution AverageOfTwo Input: Two numbers 1. Add the two numbers 2. Divide the result by 2 3. Return the result of step 2 End Example 2 Write an algorithm to change a numeric grade to a pass/no pass grade. Solution Pass/NoPassGrade Input: One number 1. if (the number is greater than or equal to 60) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “nopass” End if 2. Return the grade End Example 3 Write an algorithm to change a numeric grade to a letter grade. Solution LetterGrade Input: One number 1. if (the number is between 90 and 100, inclusive) then 1.1 Set the grade to “A” End if 2. if (the number is between 80 and 89, inclusive) then 2.1 Set the grade to “B” End if … See page 148 Example 4 Write an algorithm to find the largest of a set of numbers. You do not know the number of numbers. Solution 1. 2. 3. FindLargest Input: A list of positive integers Set Largest to 0 while (more integers) 2.1 if (the integer is greater than Largest) then 2.1.1 Set largest to the value of the integer End if End while Return Largest End Example 5 Write an algorithm to find the largest of 1000 numbers. Solution 1. 2. 3. 4. FindLargest 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 Flowchart • A flowchart is a pictorial representation of an algorithm. • It shows the logic flow of a program. In a programming environment, it can be used to design a complete program or just part of a program. • It hides all of the details of an algorithm in an attempt to give the big picture; it shows how the algorithm flows from beginning to end. 36 Flowchart Symbols terminal input / output decision n process notation flow lines connector 1 begin 3 2 Y N 3 3 1 end 2 37 Flowchart 38 Flowchart Three basic constructs in flowchart: Y A N B A B sequence decision A A while Y until N N Y repetition 39 Flowchart example 1 begin 1×2×3 ×4×5= ? 1→p ; 2 →i Assume: p、i p×i→p p × i → p； i +1→i i +1→i S1: 1 → p ; 2 → i S2: p×i → p S3: i + 1 → i S4: if i≤5 go to S2 else go to S5 S5: output Loop body i≤5 Y N result end 40 Flowchart example2 how do we get the greatest common divisor of two integers: m and n? For example: m=12, n=8; or m=100, n=36 ? begin Input m, n Get remainder M%n -> r r=0? N Y output n n -> m; r-> n end 41 Exercise 1. Rewrite the GCD example in pseudocode. 2. Rewrite the Algorithm 8.4 Find largest (page149) example in flowchart. 42 ATM Example • Write the algorithm for a simple ATM machine. It should include two functions: deposit and withdrawal. 43 ATM Example 1. Get the password from the user. 2. If the password is not valid, construct an error message and skip to step 6. 3. Get the inputs. 3.1 Get the transaction type and the amount from the user. 3.2 Get the current balance from the bank. 4. 5. If the transaction type is deposit, add amount to the current balance. If the transaction type is withdrawal, check the current balance. 5.1 If amount is larger than balance, error and skip to step 6. 5.2 If amount is equal to or less than balance, subtract. 6. 7. Output the error message or the cash, and the current balance. Ask the user whether to repeat step 3 though 6 for another transaction. 44 ATM Example Flowcharts for two functions of ATM machine Get password Get deposit amount Add amount to balance Print the receipt Get password Get cash amount Subtract amount from balance Dispense the cash 45 ATM Example Get password Password right? Complete ATM machine flowchart Y Get type and amount N deposit Add amount to balance withdrawal Deposit or withdrawal? N Amount < balance Y Subtract amount from balance Give response or error 46 Subprograms • The principles of structured programming require that an algorithm be broken into small units called subalgorithms (the term subprograms, subroutines, procedures, functions, methods, and modules are also used). • Each subalgorithm is in turn divided into smaller subalgorithms. • The process continues until the subalgorithm become intrinsic (understood easily). 47 Subprograms • We can divide “Algorithm Find Largest” into a main algorithm and a subalgorithm. • In each iteration, the algorithm finds the larger of two integers. • We can separate this part of the task and create a small subtask out of it. 48 Subprograms • In line 2.1, FindLargest is suspended and FindLarger begins. • FindLarger finds the larger number between the current Largest value and the current integer value. This is known as a function call. • Note that in each iteration, FindLarger is called once. • The advantages of using subalgorithms: – It is more understandable. – A subalgorithm can be called many times in different parts of the main algorithm without being rewritten. 49 Subprograms 1. 2. 3. 1. FindLargest Input: A list of positive integers Set Largest to 0 while (more integers) 2.1 FindLarger End while Return Largest End FindLarger Input: Largest and current integer if (the integer is greater than Largest) then 1.1 Set Largest to the value of the integer End if End 50 Structure chart • A structure chart is a high-level design tool that shows the relationship between different modules in an algorithm. • It is mostly used at the design level. In other words, it is created before you start writing your program. 51 Structure Chart Symbols used in the structure chart: Function Common function Condition Loop Condition loop (+) Exclusive OR Data flow Flag 52 Structure Chart Using the structure chart to write the algorithm for a simple ATM machine in top-down design. Simulate an ATM machine Get inputs Perform all calculations Give outputs First-level ATM structure chart 53 Structure Chart Get inputs Second-level structure chart for ATM input Get password Get Get transaction transaction type amount Give outputs Print error Print balance Second-level structure chart Dispense for ATM cash output 54 Structure Chart Perform calculations Handle deposits Add amount to balance Check balance Handle withdrawals Subtract amount from balance Give error Second- and third-level structure chart for ATM calculations 55 Example 1. Write an algorithm that will calculate and print the average grade on three tests for an entire class. (in pseudocode, structure chart and flowchart) Pseudocode: 1. Get the 3 test scores; 2. Add the scores together; 3. Divide the sum by 3; 4. Print the average; 5. Repeat step1 through 4 for each student. 56 Example (con.) Structure chart: Calculate grades Get inputs Get 3 scores Calculate Average grade Add 3 scores Print grade Divide by 3 Note The structure chart gives no indication of looping. The iteration is shown in the pseudocode and flowchart. 57 Example (con.) Flowchart: Get 3 scores Add the scores together Divide the sum by 3 Print the average More students? N Y 58 Example 2. Write an algorithm for a simple calculator (in pseudocode and structure chart). Pseudocode: 1. Get two numbers and the operation desired. 2. Check the operation: 2.1 If operation is addition, the result is the first + the second; 2.2 If operation is subtraction, the result is the first the second; 2.3 If operation is multiplication, the result is the first * the second; 2.4 If operation is division, check the second number: 2.4.1 If zero, send error message. 2.4.2 If not zero, the first / the second. 3. Print out the result or the error message. 59 Example (con.) Structure chart: perform inputs 2 numbers calculator operation addition subtraction outputs result division Check 2nd for zero error multiplication Create error 60 Example 3. Write an algorithm in pseudocode for a program to calculate the area of simple shapes (rectangle, right triangle and circle). 1. 2. Get the shape desired. If it is a rectangle: 2.1 get the length and width. 2.2 calculate the area as length * width. 3. If it is a right triangle: 3.1 get the base and height. 3.2 calculate the area as base* height / 2. 4. If it is a circle: 4.1 get the radius. 4.2 calculate the area as radius * radius * 3.14. 5. Print out the area. 61 part 4 Basic Algorithms 62 Summation 63 Product 64 Sorting • Sorting is the process by which data are arranged according to their values. • There are three fundamental sorting algorithms: – Selection sort – Bubble sort – Insertion sort 65 Selection sort • In selection sort, the list is divided into 2 sublists—sorted and unsorted—which are divided by an imaginary wall. • You find the smallest element from the unsorted sublist and swap it with the element at the beginning of the unsorted data. • After each selection and swapping, the imaginary wall moves one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. • Each time you move one element from the unsorted sublist to the sorted sublist, you have completed a sort pass. • A list of n elements requires n-1 passes to completely rearrange the data. 66 67 68 69 Bubble sort • In bubble sort method, the list is divided into 2 sublists— sorted and unsorted. • The smallest element is bubbled from the unsorted sublist and moved to the sorted sublist. • After the smallest element has been moved to the sorted list, the wall moves one element ahead. • Each time an element moves from the unsorted sublist to the sorted sublist, one sort pass is completed. • Given a list of n elements, bubble sort requires up to n-1 passes to sort the data. 70 71 Insertion sort • In insertion sort method, the list is divided into 2 sublists—sorted and unsorted. • In each pass, the first element of the unsorted sublist is picked up, transferred to the sorted list, and inserted at the appropriate place. • Note that a list of n elements will take at most n-1 passes to sort the data. 72 73 Searching • Searching is the process of finding the location of a target among a list of objects. • In the case of a list, searching means that given a value, you want to find the location (index) of the first element in the list that contains that value. 74 Sequential search • Sequential search is used if the list being searched is not ordered. • In a sequential search, you start searching for the target from the beginning of the list. • You continue until you either find the target or you are sure that it is not in the list (because you have reached the end of the list). 75 76 Binary search • If the list is sorted, you can use a more efficient algorithm called binary search. • A binary search starts by testing the data in the element at the middle of the list. • This determines if the target is in the first half or the second half of the list. • With every pass, we eliminate half of the list. 77 part 5 Recursion 78 Recursion • In general, there are two approaches to writing algorithms for solving a problem: – Iteration – Recursion • Recursion is a process in which an algorithm calls itself. 79 Iterative definition of factorial • An algorithm is iterative whenever the definition does not involve the algorithm itself. 80 Recursive definition of factorial • An algorithm is defined recursively whenever the algorithm appears within the definition itself. 81 Recursion • The recursive solution for a problem involves a two-way journey. • First you decompose the problem from top to bottom, and then you solve it from bottom to top. 82 Iterative solution of factorial 1. 2. 3. 4. Factorial Input: A positive integer num Set FactN to 0 Set i to 1 while (i is less than or equal to num) 3.1 Set FactN to FactN x I 3.2 Increment i End while Return FactN End 83 Recursive solution of factorial Factorial Input: A positive integer num 1. if (num is equal to 0) then 1.1 return 1 else 1.2 return num x Factorial (num – 1) End if End 84 part 6 Evolution 85 Evolution • A computer language is a set of predefined words that are combined into a program according to predefined rules (syntax). • Computer languages have evolved from machine language to natural languages. 86 Machine languages • Machine language, the lowest level of language, is the basic language of the computer, representing data as 1s and 0s. • Machine programs vary from computer to computer; that is, they are machine dependent. • The only language understood by a computer is machine language. • These binary digits correspond to the on and off electrical states of the computer. 87 Machine languages 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 00000000 00000100 0000000000000000 01011110 00001100110000100000000000000010 11101111 000101100000000000000101 11101111 10011110 0000000000001011 11111000 10101101 11011111 0000000000010010 0110001011011111 0000000000010101 11101111 00000010 11111011 0000000000010111 11110100 1010110111011111 0000000000011110 0000001110100010 11011111 0000000000100001 11101111 00000010 11111011 0000000000100100 01111110 11110100 10101101 11111000 10101110 110001010000000000101011 0000011010100010 11111011 0000000000110001 11101111 00000010 11111011 0000000000110100 00000100 0000000000111101 00000100 0000000000111101 • Advantage: it works efficiently for computer. • Disadvantage: it is not convenient for people to read and use.88 Symbolic languages • Symbolic language, also called assembly language, is a low-level language that allows a programmer to use abbreviations or easily remembered words instead of numbers. • Assembly language varies from computer to computer; it’s machine dependent. • A programmer can write instructions in assembly language more quickly than in machine language; however, it’s still not an easy one to learn, and it’s so tedious to use that mistakes are frequent. 89 Symbolic languages 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 00000000 00000100 0000000000000000 Entry main, ^m<r2> 01011110 00001100 11000010 0000000000000010 subl2 #12,sp 11101111 00010110 0000000000000101 jsb C$MAIN_ARGS 11101111 10011110 0000000000001011 movab $CHAR_STRING_CON 11111000 10101101 11011111 0000000000010010 01100010 11011111 0000000000010101 pushal -8(fp) 11101111 00000010 11111011 0000000000010111 pushal (r2) 11110100 10101101 11011111 0000000000011110 calls #2,read 00000011 10100010 11011111 0000000000100001 pushal -12(fp) 11101111 00000010 11111011 0000000000100100 pushal 3(r2) 01111110 11101000 10101101 calls #2,read 11111000 10101110 11000101 0000000000101011 mull3 -8(fp),-12(fp),00000110 10100010 11111011 0000000000110001 pushal 6(r2) 11101111 00000010 11111011 0000000000110100 calls #2,print 00000100 0000000000111101 clrl r0 00000100 0000000000111101 ret 90 Language translators • A special program called an assembler is used to translate symbolic code into machine language. • A language translator is a type of system software that translates a program written in a second- or higher-generation language into machine language. 91 Language translators • Languages translators are of three types: – Assemblers – Compilers – Interpreters Source Code Language Translator Object Code 92 High-level languages • A high level language is an English-like language that allows users to write in a familiar notation, rather than numbers or abbreviations. • Most high-level languages are not machine dependent; they can be used on more than one kind of computers. • They are portable, allowing the programmer to concentrate on the application rather than the intricacies of the computer. 93 High-level languages • The translator for high-level languages is depending on the language, either a compiler or an interpreter. • Compiler—execute later. It’s software that looks at an entire high-level program before translating it into machine language. Examples are C and COBOL. • Interpreter—execute immediately. It’s software that converts high-level language statements into machine language one at a time, in succession. Examples are BASIC and LISP. 94 Natural languages • Natural languages are of two types. – The first are ordinary human languages (such as Chinese, English, Spanish and so on). – The second are programming languages that use human language to give people a more natural connection with computer. • Natural languages are part of the field of study known as artificial intelligence (AI). 95 part 7 Categories of languages 96 Categories of languages • We divide computer languages into five categories: 97 Procedural languages • A procedural language is a set of instructions that are executed one by one from beginning to end unless an instruction forces the control elsewhere. • When programmers need to solve a problem using one of the procedural languages, they should know the procedure to follow. • Because each instruction of procedural languages is a command to the computer system to do some specific task, procedural languages sometimes called imperative languages. 98 Procedural languages • FORTRAN (FORmula TRANslation) • COBOL (Common Business-Oriented Language • Pascal • C • Ada 99 Object-oriented languages • Unlike C and Pascal are procedural oriented languages, forcing the user to follow a predetermined path from step A to Step B and so on, object-oriented languages are event driven—that is, they respond to input from the user or other programs at unregulated times and thus are driven by user choices. 100 Object-oriented languages • If we think of a data item in a programming language as an object, a program can be thought of as a series of operations that you want to perform on the object. • In procedural languages, the objects are passive. They do not have operations defined for them. The programmers define the operations and apply them to the objects. • In object-oriented programming, the objects and the operations to be applied to them are tied together. The objects are active. 101 Object-oriented languages • C++ – Encapsulation – Inheritance – Polymorphism • Java – Application vs. applet – Multithreading 102 Part 8 Building a program 103 Building a program • It is the job of a programmer to write the program and then turn it into an executable (machine language) file. • There are 3 steps in this process: – Writing and editing the program – Compiling the program – Linking the program with the required library modules 104 Edit program Compile program *.c Edit Link program C Library Compile System *.exe *.obj Compile Link Execute *.obj 105 106 part 9 Elements of programming languages 107 Elements of Programming Languages • There are variables or objects, in which you can store the pieces of data that a program is working on. • There are expressions, which compute new values from old ones. • There are assignments which store values (of expressions, or other variables) into variables. 108 Elements of Programming Languages • There are conditionals which can be used to determine whether some condition is true, such as whether one number is greater than another. • Variables and expressions may have types, indicating the nature of the expected values. • There are statements which contain instructions describing what a program actually does. 109 Elements of Programming Languages • There are control flow constructs which determine what order statements are performed in. • An entire set of statements, declarations, and control flow constructs can be lumped together into a function (also called routine, subroutine, or procedure) which another piece of code can then call as a unit. • A set of functions, global variables, and other elements makes up a program. The source code for a program may be distributed among one or more source files. 110 Elements of Programming Languages • Many of these elements exist in a hierarchy. • A program typically consists of functions and global variables; a function is made up of statements; statements usually contain expressions; expressions operate on objects. functions statements expressions objects Program global variables 111 112 113 Skills Needed in Programming • The first hard thing about programming is to learn, become comfortable with, and accept some artificial mechanisms, whether they make “sense” to you or not. • Attention to detail • Stupidity • Good memory • Ability to abstract, think on several levels 114 Attention to detail • In programming, the details matter. • You can't be vague; you can't describe your program 3/4 of the way and then say “You know what I mean?” and have the compiler figure out the rest. • You have to dot your i's and cross your t's. • If the language says you have to declare variables before using them, you have to. …… 115 Stupidity • Computers are incredibly stupid. • They do exactly what you tell them to do: no more, no less. • When you're programming, it helps to be able to “think” as stupidly as the computer does, so that you're in the right frame of mind for specifying everything in minute detail, and not assuming that the right thing will happen unless you tell it to. 116 Good memory • Things to remember while programming: – the syntax of the language – the set of prewritten functions and their parameters – what variables and functions you've defined in your program and how you're using them – techniques you've used or seen in the past – bugs you've had in the past. 117 Ability to abstract • One of the most powerful techniques for managing the complexity of a software system (or any complex system) is to compartmentalize it into little ''black box'' processes which perform useful tasks but which hide some details so you don't have to think about them all the time. • Think about the mechanics of a design hierarchy, while also use that hierarchy to avoid having to think about every detail of it at every level. 118 Objectives • Define algorithms. • List the 5 essential properties of an algorithm. • Define and use the three constructs for developing algorithms: sequence, decision, and repetition. • Understand and use three tools to represent algorithms: flowchart, pseudocode, and structure chart. • Understand the concept of modularity and subalgorithms. • List and comprehend common algorithms, such as sorting and searching. • Differentiate between recursion and iteration. 119 Objectives • Have a vision of computer language evolution. • Distinguish between machine, assembly, and highlevel languages. • Distinguish between the different categories of languages: procedural, object-oriented, functional, declarative, and special. • Understand the process of creating and running a program. • Become familiar with elements of programming languages. 120 That’s all for this chapter! 121