200 likes | 439 Views
3. Developing an Algorithm. Simple Program Design Third Edition A Step-by-Step Approach. 3. Objectives. To introduce methods of analyzing a problem and developing a solution To develop simple algorithms using the sequence control structure
E N D
3 Developing an Algorithm Simple Program Design Third Edition A Step-by-Step Approach
3 Objectives • To introduce methods of analyzing a problem and developing a solution • To develop simple algorithms using the sequence control structure • To introduce methods of manually checking the developed solution
3 Defining the Problem • To help with this initial analysis, the problem should be divided into three separate components: 1. Input: a list of the source data provided to the problem 2. Output: a list of the outputs required 3. Processing: a list of actions needed to produce the required outputs
3 Defining the Problem • When dividing a problem into its three different components, you should simply analyze the actual words used in the specification, and divide them into those which are descriptive and those which imply actions • In some programming problems, the inputs, processes, and outputs may not be clearly defined • It is best to concentrate on the outputs required
3 Example 3.1 Add Three Numbers • A program is required to read three numbers, add them together, and print their total • By looking at the underlined nouns and adjectives you can see that the input for this problem is three numbers and the output is the total • It is helpful to write down these first two components in a simple diagram, called a defining diagram • By looking at the underlined words, you can see that the processing verbs are read, add together and print • Refer to the diagram at the bottom of page 17 of the textbook for an example of a defining diagram
3 Meaningful Names • It is a good idea to introduce some unique names, which will be used to represent the variables or objects in the problem and describe the processing steps • A name given to a variable is simply a method of identifying a particular storage location in the computer • Often a name describes the type of data in a particular variable • Underscores are useful when choosing variable names, as the underscore is used as a word separator: number_1 and word_count
3 Example 3.2 Find Average Temperature • A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the simple average temperature, calculated by (maximum temperature = minimum temperature)/2 • First establish the input and output components by underlining the nouns and adjectives in the problem statement
3 Example 3.2 Find Average Temperature • There is an example of a defining diagram using these names in the middle of page 19 of the textbook • Now establish the processing steps by underlining the verbs in the problem statement • Also on page 19 of the textbook, you’ll find a completed defining diagram • Remember that at this stage you are only concerned with the fact that the simple average temperature must be calculated, not how the calculation will be performed
3 Designing a Solution Algorithm • Designing a solution algorithm is the most challenging task in the life cycle of a program • Once the program has been properly defined, you usually begin with a rough sketch of the steps required to solve the problem • Look at what is required and, using these requirements and the three basic control structures defined in the Structure Theorem, attempt to establish how the processing will take place
3 Designing a Solution Algorithm • The first attempt at designing a particular algorithm usually does not result in a finished product • Pseudocode is useful in this trial and error process • If the algorithm is not correct, the program will never be correct either • There is some argument that the work of a programmer ends with the algorithm design • A coder or trainee programmer could take over and code the solution algorithm into a specific programming language
3 Example 3.4 Solution Algorithm for Example 3.1 • A Defining diagram (see the diagram on page 21 of the textbook) • B Solution algorithm Add_three_numbers Read number_1, number_2, number_3 total=number_1 +number_2+number_3 Print total END
3 Example 3.4 Solution Algorithm for Example 3.1 • B Solution algorithm • There are a number of points to consider in this solution algorithm: 1. A name has been given to the algorithm, namely Add_three_numbers 2. An END statement at the end of the algorithm indicates that the algorithm is complete 3. All processing steps between the algorithm name and the END statement have been indented for readability 4. Each processing step in the defining diagram relates directly to one or more statements in the algorithm
3 Checking the Solution Algorithm • After a solution algorithm has been constructed it must be tested for correctness • It is easier to detect errors in pseudocode than in the corresponding program code • Desk checking involves tracing through the logic of the algorithm with some chosen test data • You walk through the logic of the algorithm exactly as a computer would, keeping track of all major variable values on a sheet of paper • This playing computer not only helps to detect errors early, but also helps you become familiar with the way the program runs
3 Selecting Test Data • When selecting test data to desk check an algorithm, you must look at the program specification and choose simple test cases only, based on the requirements of the specification, not the algorithm • To desk check the algorithm, you need only a few simple test cases which will follow the major paths of the algorithm logic
3 Steps in Desk Checking an Algorithm 1. Choose simple input test cases that are valid 2. Establish what the expected result should be for each test case 3. Make a table of the relevant variable names within the algorithm on a piece of paper 4. Walk the first test case through the algorithm, keeping a step-by-step record of the contents of each variable in the table as the data passes through the logic 5. Repeat the walk-through process using the other test data cases, until the algorithm has reached its logical end 6. Check that the expected result established in Step 2 matches the actual result developed in Step 5
3 Example 3.7 Desk check for Example 3.1 • A Solution algorithm Add_three_numbers Read number_1, number_2, number_3 total=number_1+number_2+number_3 Print total END • B Desk checking (see the diagram on page 25 of the textbook for examples of each step) (i) Choose two sets of input test data (ii) Establish the expected result for each test case
3 Example 3.7 Desk check for Example 3.1 • B Desk checking (iii) Set up a table of relevant variable names and pass each test data set through the solution algorithm, statement by statement (iv) This desk check, which should take no more than five minutes, indicates that the algorithm is correct
3 Summary • The first section of this chapter was devoted to methods of analyzing and defining a programming problem • The method suggested was to analyze the actual words used in the specification with the aim of dividing the problem into three separate components: • input • output • processing
3 Summary • The second section of this chapter was devoted to the establishment of a solution algorithm • After the initial analysis of the problem you must attempt to find a solution and express this solution as an algorithm • To do this you must use correct pseudocode statements, the three basic control structures, and the defining diagram which had previously been established • The third section was concerned with checking the algorithm for correctness