Скачать презентацию C Programming Language Lecture 2 Problem Analysis and

63b9398471c8fc8f29c18b68ebc9cc01.ppt

• Количество слайдов: 20

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation The Hashemite University Computer Engineering Department (Adapted from the textbook slides)

Outline n n n Program development cycle. Algorithms development and representation. Examples. The Hashemite University 2

Program Development Cycle n Program development cycle steps: n n n Problem definition. Problem analysis (understanding). Algorithm development: n Ways for algorithm representation: n n n n Human language Pseudocode. Flowcharts (also called UML activity diagram). Coding. Execution and testing. Maintenance. Recall that such cycle and all the techniques presented in this lecture are the same for any programming language you want to use not only for C++. The Hashemite University 3

Problem Definition n n To understand the problem is half the solution. Describe it by precise, up to the point statements that will make both analyzing and solving the problem easier and clearer. The Hashemite University 4

Problem Analysis n n n Determine the inputs, outputs, and the required operations. Explore all possible solutions. Pick the easiest, in terms of implementation cost (space, time) one. The Hashemite University 5

Algorithm Development n Algorithm is a procedure that determines the: n n n Actions to be executed. Order in which these actions are to be executed (which is called program control and in industry it is called work flow). So, it is a plan for solving the given problem. You must validate the developed algorithm, i. e. make sure that it solves the correct problem. You must verify the developed algorithm, i. e. make sure that it produces correct results. You must check the feasibility (in terms of the needed resources, ease of implementation, ease of understanding and debugging, its expected execution time, etc. ) of the developed algorithm. The Hashemite University 6

Algorithm Representation – Human Language n n Use your own language to represent the steps of the developed algorithm. Example: adding two integers: 1. Prompt the user to enter two numbers. 2. Add them and store the result. 3. Display the sum to the user on the screen. The Hashemite University 7

Algorithm Representation – Pseudocode n n n n Artificial, informal language used to develop algorithms. Kind of structured English for describing algorithms. Middle approach between human language and C++ code. It is convenient and user friendly. Not actually executed on computers Allows us to “think out” a program before writing the code for it Usually easy to convert into a corresponding C++ program Consists only of executable statements, i. e. no definitions or declarations. The Hashemite University 8

Pseudocode Notations I n Input: n n n Output: n n Input from keyboard: Get. Input from file or memory location: Read. Output to printer: Print. Output to file: Write. Output to screen: Display, Prompt (usually followed by Get). Values assignment: n n n Initial values: Initialize, Set. Results of computation: =, . Keeping variables for later use: Save, Store. The Hashemite University 9

Pseudocode Notations II n Arithmetic computation: n n n Control structures: n n Either use exact operators (+, *, /, -) or equivalent words of them (add, multiply, divide, subtract). Computations: either use Compute or represent the actual operation mathematically. E. g. Compute average or avg = sum/count. Use the actual words as it is: If, If – then – Else, While , do – While, For – to --, . . . Relational operators: n n Write them as words: greater then, less than or equal, etc. Or you can use their symbols: >, <=, etc. The Hashemite University 10

Pseudocode Examples n Adding two numbers: 1. 2. 3. 4. 5. 6. 7. n Prompt user for number 1 Get number 1 Prompt user for number 2 Get number 2 Add number 1 and number 2 Set sum to the result Display sum Other examples (on board): n Deciding the grade (A-F) of a student. The Hashemite University 11

Algorithm Representation – Flowcharts n n n Represent the algorithms or computer programs execution steps graphically. The American National Standards Institute (ANSI) published a standard for flowcharts that includes a definition of their symbols (see next slide). Pseudocode is preferred over flowcharts since: n n n More readable. Can be converted into C++ code easier. Flow charts are very similar to the UML (Unified Modeling Language) activity diagram with some differences in the used symbols. UML is an industry standard for modeling software systems. We will study flowcharts not activity diagrams in this The Hashemite University 12 course.

Flowcharts Symbols The Hashemite University 13

Flowchart Examples n Numbers addition example. The Hashemite University 14

Example III n n Write a program that reads three numbers from a file. If the multiplication of these numbers is greater than or equal their sum then print “Winner” on the screen”, otherwise print “Loser” on the screen. Solution: n On board. The Hashemite University 15

Example I n n Symbol grading system: Deciding the grade (A-F) of a student. Solution: n On board. The Hashemite University 16

Coding n n Writing the source code of your solution that is to convert the developed algorithm into code statements of the used language, i. e. C++. Some useful tips: n n Make sure of using correct syntax. Use meaningful identifiers to make your code more readable. Add suitable documentation and comments. Make your code modular or structured as possible. The Hashemite University 18

Execution and Testing n n Compilation and debugging. Types of errors: n Syntax errors (Compile time errors): n n Errors caught by compiler Logical errors (Runtime errors): n Errors which have their effect at execution time n n n Non-fatal logic errors n program runs, but has incorrect output Fatal logic errors n program exits prematurely Tracing to verify your program with different sets of inputs. The Hashemite University 19

Maintenance n n Not always applicable in education, i. e. highly required in real world jobs. Update your code based on: n n n Discovered and reported bugs. Customer feedback to make your application more efficient and flexible. Upgrade the code. The Hashemite University 20

Additional Notes n This lecture covers the following material from the textbook: n Fourth edition: n Chapter 2: Sections 2. 1 - 2. 3 The Hashemite University 21