Program Design and Pseudocode

25
Program Design and Pseudocode

description

Program Design and Pseudocode. 7 Steps in Program Development. Programming can be defined as the development of a solution to an identified problem, and the setting up of a related series of instructions that will produce the desired results Step 1: Define the Problem - PowerPoint PPT Presentation

Transcript of Program Design and Pseudocode

Page 1: Program Design and Pseudocode

Program Designand Pseudocode

Page 2: Program Design and Pseudocode

2

7 Steps in Program Development

• Programming can be defined as the development of a solution to an identified problem, and the setting up of a related series of instructions that will produce the desired results

Step 1: Define the Problem

To help with initial analysis, the problem should be divided into three separate components:

• the inputs

• the outputs

• the processing steps to produce the required outputs

Page 3: Program Design and Pseudocode

3

Step 2: Outline the Solution

• This initial outline is usually a rough draft of the solution and may include:• 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

Page 4: Program Design and Pseudocode

4

Step 3: Develop the Outline into an Algorithm

• The solution outline developed in Step 2 is

expanded into an algorithm: a set of

precise steps that describe exactly the

tasks to be performed and the order in

which they are to be carried out

Page 5: Program Design and Pseudocode

5

Step 4: Test the Algorithm for Correctness

• 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

Page 6: Program Design and Pseudocode

6

Step 5: Code the Algorithm into a Specific Programming Language

• Only after all design considerations have

been met should you actually start to code

the program into your chosen

programming language

Page 7: Program Design and Pseudocode

7

Step 6: Run the Program on the Computer

• This step uses a program compiler and

programmer-designed test data to

machine test the code for syntax errors

and logic errors

Page 8: Program Design and Pseudocode

8

Step 7: Document and Maintain the Program

• Program documentation should not be listed as the last step in the program development process, as it is really an ongoing task from the initial definition of the problem to the final test result

• Documentation involves both external documentation and internal documentation that may have been coded in the program

Page 9: Program Design and Pseudocode

9

Procedural Programming and Top Down Development

• Procedural programming is based on a structured, top-down approach to writing effective programs

• The procedural approach concentrates on ‘what’ a program has to do and involves identifying and organizing the ‘processes’ in the program solution

Page 10: Program Design and Pseudocode

10

Top-Down Development and Modular Design

• In the top-down development of a program design, a general solution to the problem is outlined first. This is then broken down gradually into more detailed steps until finally the most detailed levels have been completed

• Procedural programming also incorporates the concept of modular design, which involves grouping tasks together because they all perform the same function

Page 11: Program Design and Pseudocode

11

How Do You Write Algorithms? 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

Page 12: Program Design and Pseudocode

12

Program Data

• Data within a program may be a single

variable, such as an integer or a character,

or a group item (sometimes called an

aggregate), such as an array, or a file

Page 13: Program Design and Pseudocode

13

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

• 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

Page 14: Program Design and Pseudocode

14

Choose Meaningful Variable Names

• All names should be meaningful

• A name given to a variable is simply a method of identifying a particular storage location in the computer

• The uniqueness of a name will differentiate it from other locations

• Often a name describes the type of data stored in a particular variable

• Most programming languages do not tolerate a space in a variable name, as a space would signal the end of the variable name and thus imply that there were two variables

Page 15: Program Design and Pseudocode

15

Elementary Data Items

• An elementary data item is one containing a single variable that is always treated as a unit

• A data type consists of a set of data values and a set of operations that can be performed on those values

• The most common elementary data types are:

— Integer — Character — Boolean

Page 16: Program Design and Pseudocode

16

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 — File

— Array — String

Page 17: Program Design and Pseudocode

17

• When designing a solution algorithm, you need to keep in mind that a computer will eventually perform the set of instructions written

• If you use words and phrases in the pseudocode which are in line with basic computer operations, the translation from pseudocode algorithm to a specific programming language becomes quite simple

How to Write Pseudocode

Page 18: Program Design and Pseudocode

18

Six Basic Computer Operations

1 A computer can receive information– When a computer is required to receive information or

input from a particular source, whether it is a terminal, a disk or any other device, the verbs Read and Get are used in pseudocode

2 A computer can put out information– When a computer is required to supply information or

output to a device, the verbs Print, Write, Put, Output, or Display are used in pseudocode

– Usually an output Prompt instruction is required before an input Get instruction

Page 19: Program Design and Pseudocode

19

Six Basic Computer Operations (continued)

3 A computer can perform arithmetic– Most programs require the computer to perform some sort of

mathematical calculation, or formula, and for these, a programmer may use either actual mathematical symbols or the words for those symbols

– To be consistent with high-level programming languages, the following symbols can be written in pseudocode:+ for Add - for Subtract

• for Multiply / for Divide ( ) for Parentheses

– When writing mathematical calculations for the computer, standard mathematical ‘order of operations’ applies to pseudocode and most computer languages

Page 20: Program Design and Pseudocode

20

Six Basic Computer Operations (continued)

4 A computer can assign a value to a variable or memory location

– There are three cases where you may write pseudocode to assign a value to a variable or memory location:

1. To give data an initial value in pseudocode, the verbs Initialize or Set are used

2. To assign a value as a result of some processing the symbols ‘=‘ or ‘’ are written

3. To keep a variable for later use, the verbs Save or Store are used

Page 21: Program Design and Pseudocode

21

Six Basic Computer Operations (continued)

5 A computer can compare two variables and select one or two alternate actions

– An important computer operation available to the programmer is the ability to compare two variables and then, as a result of the comparison, select one of two alternate actions

– To represent this operation in pseudocode, special keywords are used: IF, THEN, and ELSE

Page 22: Program Design and Pseudocode

22

Six Basic Computer Operations (continued)

6 A computer can repeat a group of actions

– When there is a sequence of processing steps that need to be repeated, two special keywords, DOWHILE and ENDDO, are used in pseudocode

– The condition for the repetition of a group of actions is established in the DOWHILE clause, and the actions to be repeated are listed beneath it

Page 23: Program Design and Pseudocode

23

The Structure Theorem

• The Structure Theorem states that it is possible to write any computer program by using only three basic control structures that are easily represented in pseudocode:

– Sequence

– Selection

– Repetition

Page 24: Program Design and Pseudocode

24

The Three Basic Control Structures

1 Sequence– The sequence control structure is the straightforward

execution of one processing step after another

– In pseudocode, we represent this construct as a sequence of pseudocode statements

2 Selection– The selection control structure is the presentation of a

condition and the choice between two actions; the choice depends on whether the condition is true or false

– In pseudocode, selection is represented by the keywords IF, THEN, ELSE, and ENDIF

Page 25: Program Design and Pseudocode

25

The Three Basic Control Structures (continued)

3 Repetition

– The repetition control structure can be defined as the presentation of a set of instructions to be performed repeatedly, as long as a condition is true

– The basic idea of repetitive code is that a block of statements is executed again and again, until a terminating condition occurs

– This construct represents the sixth basic computer operation, namely to repeat a group of actions