Chapter 2 Problem Solving, abstraction and stepwise refinement Basic steps in programming An algorithm is a series of step-by-step instructions that produces a solution to a problem. The programmer’s algorithm Define the problem Plan the problem solution Code the program Test and debug the program Document the program Defining the Problem What is the program suppose to do? What output should it produce? What data do you need? How is the data to be entered into the machine(input)? How should the data be processed? Planning the Solution. Outline the solution using psuedocode. Psuedocode is an informal set of Englishlike statements that are generally accepted within the computer industry to denote common computer programming operations. Psuedocode statements are used to describe the steps in a computer algorithm. Good psuedocode is easily translated into the statements of a formal programming language like Java. The psuedocode can also serve as part of program documentation. Coding the Program. Translate psuedocode into programming language statements. These statements must obey strict rules. Programming languages really are languages and obey a strict grammar. No you can’t use your own grammar. Debugging the Program. A program bug is an error somewhere in the program. An old computer joke is that there has never been a completely bug free program. This probably seems a bit cynical, but is a handy way to think. Anticipate the worst so you can avoid it. The basic steps of debugging are Realizing you have an error Locating and determining the cause of the error Fixing the error. A common error is not anticipating the sort on entries a user will make. Desk-checking the Program Go through the program mentally to try to make sure the program logic is correct. Does it do what you think it should do? What will happen if a user enters invalid data? Example: the user enters a negative number in a program which takes the square root of that number. Compiling the Program The compiler will check the program for syntax errors and type errors. A syntax error is a violation of the programming language’s rules. A type error occurs when you try to combine different types of data, such as adding an integer number to a character, as in 2 + ‘A’. Running the Program Two errors are possible here run time errors– this is the sort of error that occurs when you try to do something impossible. For example, the program may try to divide something by zero. This usually generates an error condition and often the program will crash. Better it crash than continue running with wrong results. Other errors than can occur (and often do) is that the program gets into an infinite loop, repeating the same instruction over and over forever. This will not raise an error condition Logic errors are the most insidious errors of all. Compilers and OS’s cannot detect logic errors. An example of a logic error is when you try to add two number like 2+3 but you type 2-3. The program will execute but give the wrong answer. Another common error is to create an infinite loop – the program continues to execute the same instruction over and over forever. Documentation. I have never met anyone who likes to document programs. All documentation does is to explain the steps of the program. The programmer doesn’t want to do this because she knows exactly what she did, so why explain what you already know? Well other people may need to use the program and perhaps modify it (to remove one of the bugs she overlooked). Without documentation they may find it very difficult to try to figure out what she did. Also, when she comes back to the program six months from now, she won’t be able to remember what she did without adequate documentation. Frankly, I frequently curse myself for failure to document my own programs and then later on try to figure out what exactly I did. See Staugaard, page 40 for a description of documentation. Also see examples in your text for documentation. I will base part of your project grade on documentation. Problem solving using algorithms Algorithm to shampoo hair Apply to wet hair Gently massage lather through hair Rinse, keeping lather out of hair Repeat It is an example of an algorithm. An algorithm is a step-by-step process to produce a result. Unfortunately this algorithm contains an infinite loop. The last instruction should read Repeat until hair is clean. Problem Abstraction and Stepwise Refinement Program abstraction provides for generalization in problem solving by allowing you to view a problem in general terms, without worrying about the details of the problems solution. State what you want the program to do in English rather than immediately writing code Stepwise refinement is the process of gradually adding detail to a general problem solution until it can easily be coded in a computer language. Example: Problem solving in action. Pages 46—49 in Staugaard. Problem: Develop a set of algorithms to calculate the amount of sales tax and the total cost of a sales item, including tax. Assume the tax rate is 7 percent and the user will enter the cost of the sales item. Defining the Problem Determine input, output and processing Output: The total cost of the sales item, including sales tax to be displayed on the system monitor. Input: The cost of the sales item to be entered by the user on the system keyboard. Processing: tax = 0.07*cost TotalCost = cost + tax Planning the Solution Start with an initial algorithm in psuedocode Initial algorithm main() BEGIN Read the cost of the sales item from the user Calculate the sales tax and total cost of the sales item. Write the total cost of the sales item, including sales tax on the system monitor. END. Note this is in English, not computer code. First Level of Refinement Expand the steps of the initial algorithm. Explain in greater detail what each of the steps does. readData() BEGIN Write a program description message to the user Write a user prompt to enter the cost of the item(cost) Read(cost) END. calculateCost() BEGIN Set tax to (0.07*cost) Set totalCost to (cost + tax) END. displayResults() BEGIN Write(totalCost) to the system monitor. END. There can be several levels of refinement Terms like readData(), calculateCost(), totalCost are examples of programming style. These are not typos. More about programming style in later chapters. Putting it together main() BEGIN readData() calculateCost() displayResults() END. Note: We break the task down into small individual tasks (readData() etc) and combine the small tasks into a program