Chapter 1 Program Design Objectives • In this chapter you will be able to: • Describe the steps in the program development process • To introduce current program design methodology • Introduce procedural and object-oriented programming • Introduce algorithms • Describe program data Simple Program Design, Fourth Edition Chapter 1 2 Program Design • Most good programs require good planning before they are written. This is called Program Design. Simple Program Design, Fourth Edition Chapter 1 3 Steps in Program Development The steps in program design are similar to the steps in problem solving. They are: 1. Define the problem 2. Outline the solution 3. Develop the outline into an algorithm 4. Test the algorithm for correctness 5. Code the algorithm into a specific programming language 6. Run the program on the computer 7. Document and maintain the program Simple Program Design, Fourth Edition Chapter 1 4 Steps in Program Development 1 Define the Problem To help with initial analysis, the problem should be divided into three separate components: – the inputs - What information is given to you to use to solve the problem? – the outputs - What will the solution of the problem, look like – the processing steps - What calculations (processes), will be used to change the input information into the desired output? • Tool: Defining diagram Simple Program Design, Fourth Edition Chapter 1 5 2 Outline the Solution • Break the problem into the steps of what must be done to solve the problem: – The major processing steps involved – The major subtasks (if any) – The user interface (if any) – The major control structures (e.g. repetition loops) – The major variables and record structures – The mainline logic Tools: Hierarchy or Structure charts Simple Program Design, Fourth Edition Chapter 1 6 3 Develop the Outline into an Algorithm • The solution outline developed in Step 2 is expanded into an algorithm: • It should list all the steps that need to be done, in the correct order they need to be done in. Tools: Pseudocode, Flowcharts, NassiSchneiderman diagrams Simple Program Design, Fourth Edition Chapter 1 7 4 Test the Algorithm for Correctness • In this step, you pretend to be a computer and execute the steps in the algorithm. This is called "Desk Checking." • This step is one of the most important in the development of a program, and yet it is the step most often forgotten • The main purpose of desk checking the algorithm is to identify major logic errors early, so that they may be easily corrected Simple Program Design, Fourth Edition Chapter 1 8 5 Code the Algorithm into a Specific Programming Language • Once the algorithm, or complete outline works correctly, you need to translate the algorithm into a computer language that your computer understands. – This is only 5 to 10% of the total programming process! Simple Program Design, Fourth Edition Chapter 1 9 6 Run the Program on the Computer • This step uses a program compiler and programmerdesigned test data to machine test the code for syntax errors (at compile time) and logic errors (at run time) • You enter the program into the computer's memory, enter the input data, and have the computer execute the program. If you get the correct answer to the problem, the program works! Otherwise, look for errors in the program and try again. Simple Program Design, Fourth Edition Chapter 1 10 7 Document and Maintain the Program • Warning! The Documentation of the program started by writing down "What is the Problem!" It continues through each step of the design process. • Documentation involves: – external documentation – internal documentation that may have been coded in the program Simple Program Design, Fourth Edition Chapter 1 11 Program Design Methodology • Recently, a number of different approaches to program design have emerged, the most common being: – Procedure-driven – Event-driven – Data-driven Simple Program Design, Fourth Edition Chapter 1 12 Procedure-Driven Program Design • The procedure-driven approach to program design is based on the idea that the most important feature of a program is ‘what’ it does – that is, its processes or functions Simple Program Design, Fourth Edition Chapter 1 13 Procedure-Driven Program Design Example • A program to execute a sales order may be divided into: – order entry module – data verification module – Inventory update module Simple Program Design, Fourth Edition Chapter 1 14 Procedure-Driven Program Design • In procedure-driven programming, the sequence of operations for an application is determined by a central controlling program (e.g., a main procedure). – The program determines in advance what will be done and in which order. The program starts at the beginning, occasionally calls subroutines Simple Program Design, Fourth Edition Chapter 1 15 Event-Driven Program Design • The event-driven approach to program design is based on the idea that an event or interaction with the outside world can cause a program to change from one known state to another • In event-driven programming the code responds to a system-generated event, such as a buttonpush or a clock cycle. Simple Program Design, Fourth Edition Chapter 1 16 Data-Driven Program Design • The data-driven approach to program design is based on the idea that the data in a program is more stable than the processes involved • It begins with an analysis of the data and the relationships between the data, in order to determine the fundamental data structures • The choice between procedure-driven, eventdriven, or data-driven program design methodologies is usually determined by the selection of a programming language Simple Program Design, Fourth Edition Chapter 1 17 Procedural versus Object-Oriented Programming • Procedural programming is based on a structured, topdown approach to writing effective programs • In the top-down development of a program design: 1. a general solution to the problem is outlined first 2. the general solution is then broken down gradually into more detailed steps until finally the most detailed levels have been completed Simple Program Design, Fourth Edition Chapter 1 18 Modular Design • Procedural programming also incorporates the concept of modular design, which involves grouping tasks together because they all perform the same function • Modular design is connected directly to top-down development Simple Program Design, Fourth Edition Chapter 1 19 Object-Oriented Programming • Object-oriented programming is also based on decomposing the problem; however, the primary focus is on the things that make up the program Simple Program Design, Fourth Edition Chapter 1 20 Quick Quiz 1. Name and define the second step in program development. • ANSWER: Outline the solution- is the initial outline usually in a rough draft of the solution which may include: major processing steps involved, major subtasks, user interface, major control structures, major variables and record structures, and the mainline logic. Simple Program Design, Fourth Edition Chapter 1 21 Quick Quiz 2. The __________-driven approach to programming is based on the idea that an interaction with the outside world can cause a program to change from one known state to another. • ANSWER: event Simple Program Design, Fourth Edition Chapter 1 22 Quick Quiz 3. The choice between procedure-driven, eventdriven, or data-driven program design methodologies is usually determined by the selection of a(n) __________. • ANSWER: programming language Simple Program Design, Fourth Edition Chapter 1 23 Quick Quiz 4. __________ programming is based on a structured, top-down approach to writing effective programs. • ANSWER: Procedural Simple Program Design, Fourth Edition Chapter 1 24 Quick Quiz 5. ___________ programming is also based on decomposing the problem; however, the primary focus is on the things that make up the program. • ANSWER: Object-oriented Simple Program Design, Fourth Edition Chapter 1 25 An Introduction to Algorithms and Pseudocode • A program must be systematically and properly designed before coding begins • This design process results in the construction of an algorithm Simple Program Design, Fourth Edition Chapter 1 26 What Is an Algorithm? • An algorithm is like a recipe: it lists the steps involved in accomplishing a task • It can be defined in programming terms as a set of detailed, unambiguous and ordered instructions developed to describe the process necessary to produce the desired output from a given input Simple Program Design, Fourth Edition Chapter 1 27 What Is Pseudocode? • Pseudocode, flowcharts, and Nassi-Schneiderman diagrams are all popular ways of representing algorithms • Flowcharts and Nassi-Schneiderman diagrams are covered in Appendices 1 and 2, while pseudocode has been chosen as the primary method of representing an algorithm because it is easy to read and write and allows the programmer to concentrate on the logic of the problem • Pseudocode is really structured English – It is English that has been formalized and abbreviated to look like high-level computer languages Simple Program Design, Fourth Edition Chapter 1 28 Understanding Program Data • Information processed by the computer is data. 1. a single variable, such as an integer or a character, or 2. a group item (sometimes called an aggregate), such as an array, or a file Simple Program Design, Fourth Edition Chapter 1 29 Variables, Constants, and Literals • A variable is the name given to a collection of memory cells, designed to store a particular data item • It is called a variable because the value stored in those memory cells may change or vary as the program executes • Example total = num1 + num2 Simple Program Design, Fourth Edition Chapter 1 30 Variables, Constants, and Literals • A constant is a data item with a name and a value that remain the same during the execution of the program • A literal is a constant whose name is the written representation of its value Simple Program Design, Fourth Edition Chapter 1 31 Data Types • At the beginning of a program, the programmer must clearly define the form or type of data to be collected • The data types can be elementary data items or data structures Simple Program Design, Fourth Edition Chapter 1 32 Elementary Data Items • An elementary data item is one containing a single variable that is always treated as a unit • The most common elementary data types are: — Integer: holds a whole number — Real: holds a decimal value, or is a number with a fractional part — Character: holds a symbol such as a through z, 1 through 9, or !, @,#,$,%,^, etc — Boolean: holds either a value of true or false. Simple Program Design, Fourth Edition Chapter 1 33 Data Structures • A data structure is an aggregate of other data items • The data items that it contains are its components, which may be elementary data items or another data structure • The most common data structures are: — Records: a collection of data items or field related to each other — File: a collection of records — Array: a collection of data items, all of the same type! — String: a collection of zero or more characters. Simple Program Design, Fourth Edition Chapter 1 34 Files • There are two different methods of storing data on files: – Sequential or text files – Direct or random-access files Simple Program Design, Fourth Edition Chapter 1 35 Data Validation • Data should always undergo a validation check before it is processed by a program • Different types of data require different checks – for example: – Correct type — Correct range – Correct length — Completeness – Correct date Simple Program Design, Fourth Edition Chapter 1 36 Summary • The steps in program development were introduced and briefly described below: 1. Define the problem 2. Outline the solution 3. Develop the outline into an algorithm 4. Test the algorithm for correctness 5. Code the algorithm into a specific programming language 6. Run the program on the computer 7. Document and maintain the program Simple Program Design, Fourth Edition Chapter 1 37 Summary • Three different approaches to program design were introduced, namely procedure-driven, eventdriven, and data-driven • An algorithm was defined as a set of detailed, unambiguous and ordered instructions developed to describe the processes necessary to produce the desired output from the given input • Pseudocode is an English-like way of representing the algorithm Simple Program Design, Fourth Edition Chapter 1 38 Quick Quiz 1. A(n) _____________ lists the steps involved in accomplishing a task. • ANSWER: algorithm Simple Program Design, Fourth Edition Chapter 1 39 Quick Quiz 2. ___________ is structured English that has been formalized and abbreviated to look like high-level computer languages. • ANSWER: Pseudocode Simple Program Design, Fourth Edition Chapter 1 40 Quick Quiz 3. A(n) __________ is the name given to a collection of memory cells, designed to store a particular data item. • ANSWER: variable Simple Program Design, Fourth Edition Chapter 1 41 Quick Quiz 4. Name three of the most common elementary data types. • ANSWER: Integer, Real, Character, and Boolean Simple Program Design, Fourth Edition Chapter 1 42 Quick Quiz 5. ___________ files may be opened to read or to write, but not both operations on the same file. • ANSWER: Sequential Simple Program Design, Fourth Edition Chapter 1 43