What is a Problem? A problem is a circumstance for which we need to develop a solution to get to some goal or provide the means to some end. 8/26 & 8/28/2002 Pamela Brauda 1 Types of Problems Informal Problems we do NOT necessarily find solutions by precisely specifying the initial conditions, the desired results, or the actions (process) by which we achieve the desired results 8/26 & 8/28/2002 Pamela Brauda 2 Types of Problems Formal Problems – the kind that can be solved by writing a program for a computer Problems of synthesis: have specific initial conditions and specific plans of actions (processes), but specify only the general form of the result {Trip: Jax, drive, West Coast} Problems of analysis: the initial conditions and results are known, but not a specific plan of action (process) {Trip: Jax, L.A.} 8/26 & 8/28/2002 Pamela Brauda 3 Types of Problems ***All problems solved with the aid of computers are problems of synthesis ***The programmer transforms the problem from one of analysis to one of synthesis 8/26 & 8/28/2002 Pamela Brauda 4 Solving a Problem 1. Understand the problem 2. Devise a plan – major steps to accomplish our goal Divide the problem into segments Design a solution Consider alternatives and refine solution 8/26 & 8/28/2002 Pamela Brauda 5 Solving a Problem 3. Carry out the plan (implement) 4. Look back Did you really solve the problem? If not, return to Step 1 8/26 & 8/28/2002 Pamela Brauda 6 7 Steps in Program Development Define the problem Use a Defining Diagram Inputs Outputs Processing steps to produce the required output 8/26 & 8/28/2002 Pamela Brauda 7 7 Steps in Program Development Outline the solution Break the problem up into smaller steps Major processing steps involved Major subtasks Major control structures (e.g. repetition loops) Major variables and record structures Mainline Logic Use a hierarchy or structure chart 8/26 & 8/28/2002 Pamela Brauda 8 7 Steps in Program Development Develop the outline into an algorithm An algorithm is a set of precise steps that describe exactly the tasks to be performed and the order in which they are to be carried out Think ‘recipe for making French toast as given to a robot cook’ Pseudocode is how we represent algorithms 8/26 & 8/28/2002 Pamela Brauda 9 7 Steps in Program Development Test the algorithm for correctness Desk checking to identify logic errors Walk through each step of the algorithm Keep track of all major variables Use sample data that tests boundary conditions 8/26 & 8/28/2002 Pamela Brauda 10 7 Steps in Program Development Code the algorithm into a specific programming language NEVER approach the keyboard without a plan Code-as-you-go results in wasted time and effort 8/26 & 8/28/2002 Pamela Brauda 11 7 Steps in Program Development Run the program with test data Syntax errors show up at compile time Typos, punctuation, grammar Logic errors appear at run time 2 + 3 = 6??? Sometimes undetected if testing isn’t thorough 8/26 & 8/28/2002 Pamela Brauda 12 7 Steps in Program Development Document and maintain the program External documentation Hierarchy or structure charts, pseudocode, test data and sample results Internal documentation ‘Flower Boxes’ at the beginning of each module or function Inline comments describing the variables and what should be happening Sample program following COBOL Standards 8/26 & 8/28/2002 Pamela Brauda 13 Structured Programming Top-down Development Start out with a general solution Break down the solution into more detailed steps (modules) Step-wise refinement – moving from general to specific 8/26 & 8/28/2002 Pamela Brauda 14 Structured Programming Modular Design Group similar tasks together Reading a file, checking for end of file, verifying the item read is valid Printing page headings or totals Enables several people to work as a team, with each person working on a separate function Input/Output Sorting, Updating Calculating 8/26 & 8/28/2002 Pamela Brauda 15 Structured Programming The Structure Theorem Eliminated the GOTO statement All programs can be written with 3 basic control structures Sequence Selection, or IF-THEN-ELSE Repetition, or DO-WHILE 8/26 & 8/28/2002 Pamela Brauda 16 Algorithm to add two numbers Calculator Computer Turn on Press 1st number Press ‘+’ Press 2nd number Press ‘=‘ 8/26 & 8/28/2002 Input 1st number Input 2nd number Add Print result (sum) Pamela Brauda 17 Pseudocode Structured English used to express an algorithm Use indentation for logical flow One instruction per line Six basic algorithm structures – roughly matching the six basic computer operations 8/26 & 8/28/2002 Pamela Brauda 18 Pseudocode Getting data into the computer Read student name {from a file} Get system date {from the system} Read number_1, number_2 {file} Get tax_code {from the keyboard} 8/26 & 8/28/2002 Pamela Brauda 19 Pseudocode Getting results from the computer Print ‘Program Completed’ {printer} Write customer record to file {disk} Display ‘End of Data’ {screen} 8/26 & 8/28/2002 Pamela Brauda 20 Pseudocode Doing the math Add number to total total = total + number Compute C = (F – 32) * 5/9 Symbols okay to use: +, -, *, /, (, ) 8/26 & 8/28/2002 Pamela Brauda 21 Pseudocode Assigning a value to a variable Set Count to 0 Initialize Totals to zero Total = cost + tax 8/26 & 8/28/2002 Pamela Brauda 22 Pseudocode Comparing two variables If-Then-Else IF Student is part-time THEN Add 1 to part-time count ELSE Add 1 to full-time count ENDIF 8/26 & 8/28/2002 Pamela Brauda 23 Pseudocode Repeat a group of actions: DOWHILE, FOR Loop, DO Loop WHILE student_total < 50 DO Read student record Print student name, address to report Add 1 to student_total ENDWHILE 8/26 & 8/28/2002 Pamela Brauda 24 Developing an algorithm Define the problem INPUT Processing Number_1 Number_2 Number_3 Read 3 numbers Total Add numbers together Print total number 8/26 & 8/28/2002 Pamela Brauda OUTPUT 25