Ch. 13~15 – Basic Programming Concepts Supplementary Notes Problem Solving Procedures (Ch.13) Systematic ways of solving problems (p.82) “Systematic”: Developing step-by-step procedures to solve a problem “Divide-and-Conquer” strategy Break down a problems into smaller sub-problems to solve (modular approach) Increase the chance of finding the best solution, as it is easier to solve smaller problems Easier to trace mistakes (narrow down error into a module) Six steps (pp.84-92) Problem Identification: To precisely define a problem. Problem Analysis: Identify given input (data) and expected output (result) of a problem Identify required processing steps Algorithm Design: Algorithm: procedures to solve the problem Algorithm is represented by flowchart or pseudo-code (See. Ch.15 for details) Developing a Solution (Implementation): Top-down approach: Breaking down a problem into sub-problems (modules) and solve them. (Stepwise refinement) Bottom-up approach: Putting together smaller systems to form a bigger system. (the beginnings are small but eventually grow in complexity and completeness.) Ref: http://en.wikipedia.org/wiki/Top-down_and_bottom-up_design Testing and Debugging: Testing: To make sure that a program operates as it is designed. Test cases (test data) are required to ensure a program works without errors. General test cases (Entering valid data / Normal use) Extreme test cases (Entering invalid data intentionally / Entering data that may causes problem) Debugging: To fix errors found in a program. Three common types of errors: syntax error, runtime error and logical error. Documentation: User manual = User guide (e.g. how to install and use the program) Program manual = Technical details of the program Requirement specifications of the problem to solve Flowchart or pseudo-code: Describe the algorithm used in the program Required inputs and expected outputs of the program Source program (source code) listing Comment in the program: For other programmers to understand the program S5 CIT Ch.13~15 Supplementary Notes P.1/4 Ch. 13~15 – Basic Programming Concepts Supplementary Notes Common Types of Errors (p.88) Type Syntax Error Description Some program statement not in correct syntax (grammar rule) Example Missing punctuation; Misspelling keywords or commands; Incorrect order of commands. Discover Error discovered by compiler Error (see Ch.14) Ease of Easy Debug (Line numbers of problematic statements are given) Remark Debugging tool (i.e. debugger) can help. Runtime Error Error encountered during runtime. Division by zero; Reading an inexistent file. Insufficient system resources (e.g. memory) Logical Error Improper use of control structure (See Ch.15). The program gives always/sometimes wrong results. Error discovered by testing the program Difficult (By tracing the program) Error discovered by testing the program Very Difficult (By tracing the program or reviewing the algorithm) Debugger can help us trace the Debugger can help us trace the program. program. Programming Languages (Ch.14) A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Ref: http://en.wikipedia.org/wiki/Programming_Languages After an algorithm is designed, we develop (implement) the solution by writing computer programs, which instruct the computers to carry out suitable operations. Low-level VS High-level (programming languages) Low-level Programming Languages Feature Direct instructions to CPU (In binary data or op-code/mnemonic) Comparison Disadvantages - Machine dependent = Not portable = Not cross-platform (depending on the instruction set of the type of CPU) - Program code is long - Difficult to learn, understand, write, debug, maintain, etc. Example High-level Programming Languages More English-like (Closer to human language) Advantages - Machine independent = Portable = Cross-platform - Program code is shorter (because one statement may be translated to multiple machine instructions) - Easier to learn, understand, write, debug, maintain, etc. Advantages - Programs are more efficient (run faster) (direct instructions to CPU, I/O ports or hardware devices) Disadvantages - Programs are less efficient (less fast) (translated machine instructions have to be optimized by compiler) First Generation: Machine Language Second Generation: Assembly Language Third Generation: C, Pascal, C++, Java, etc. Fourth Generation: SQL S5 CIT Ch.13~15 Supplementary Notes P.2/4 Ch. 13~15 – Basic Programming Concepts Supplementary Notes Programming Languages (pp.109-111) First Generation (1GL): Machine Language Machines instructions in binary code Directly instructs the CPU to work Second Generation (2GL): Assembly Language Using op-code/mnemonic to represent machine instructions One instruction in assembly language corresponds to one machine instruction Translator (assembler) is required to translate source code (in assembly language) into object program (machine language) Third Generation (3GL): Procedural Language For general purpose Statements as procedures to tell the computer what to do Translator (compiler) is required to translate source code into object program Fourth Generation (4GL): Declarative Language For specific purpose Oriented toward problem solving Example: Structured Query Language (SQL) – An SQL statement generates results based on given conditions and required output format Fifth Generation (5GL)? More high-level Note: from 2GL onwards, source code are (mostly) in plaintext and can be edited by text editor Translators (pp.112-114) They are software tools Assembler (see “Assembly Language” above) Complier: Convert high-level language program (source program) into object program (e.g. executable file, written in machine language) After the object program is generated, the source program and compiler are not longer required for execution The compiler checks for syntax errors, and tell the programmer which lines may contain errors. It CANNOT check runtime errors and logical errors. An Integrated Development Environment (IDE) contain both editor and compiler. Interpreter: There are interpreters for high-level and low-level languages The interpreter reads a statement from the source code, translates it into machine instructions, and then executes them immediately. No object program is generated! (No compilation) The source program and the interpreter are required for execution. Execution is less efficient for complicated tasks, because it involves runtime translation of program statements. However, it’s easier to trace a program. S5 CIT Ch.13~15 Supplementary Notes P.3/4 Ch. 13~15 – Basic Programming Concepts Supplementary Notes Using High-level Programming Language (Ch.15) Variable represents a memory location which stores a piece of data. Basic statements (pp.124-126) Input statement obtains input from user and stores it in a variable. Output statement gives output to user (e.g. on screen). Assignment statement assigns a value (or the result of an expression) to a variable. Note that the variable being assigned must be written on the left hand side. Example Statement Input A Input 3 Output "Hello!" Output X Output 3 A = B + 3 A = A + 1 X + 3 = Y 3 = K Meaning Ask user for input and store the value in A Syntax Error! 3 is a number but not a variable Output a piece of text “Hello!” Output the value of the variable X Output the number 3 Calculate B+3 and store the result in A Add 1 to A and store the result in A Syntax Error! Left hand side must be a variable to store result Syntax Error! Left hand side must be a variable to store result Control structure (or control flow) refers to the order in which statements are executed. Sequence: Statements are executed one by one Selection / Choice (pp.129-130): A conditional statement performs different operations based on whether a condition is true or false. If-Then: If a condition is true, the then-part is executed. If-Then-Else: If a condition is true, the then-part is executed, or else the else-part is executed. Iteration / Loop (pp.131-135): A loop is a sequence of statements which is specified once but which may be carried out several times in succession. Infinite Loop If the condition of a loop is logically wrong, it may lead to “infinite loop” the program cannot escape from the loop Count-control loop (For loop): The loop is repeated for a certain number of times. Condition-controlled loop: While loop: It is a pre-test loop. A condition (“entry condition”) is checked before executing the loop body. If the condition is false at first, the loop body is completely skipped. Do-while loop: It is a post-test loop. A condition is checked after executing the loop body once. If the condition is true, the loop body is repeated again. Repeat-until loop: It is also a post-test loop. However, the condition is an “exit condition” because the loop stops if it is true. Tracing program: Dry run refers to a testing process by tracing a program manually (or running the program mentally / in mind). Echo checking refers to the technique of outputting intermediate results on the screen for checking the logic of a program. S5 CIT Ch.13~15 Supplementary Notes P.4/4