250 likes | 332 Views
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. Types of Problems. Informal Problems
E N D
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. Pamela Brauda
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 Pamela Brauda
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.} Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
Solving a Problem • 3. Carry out the plan (implement)4. Look back • Did you really solve the problem? If not, return to Step 1 Pamela Brauda
7 Steps in Program Development • Define the problem • Use a Defining Diagram • Inputs • Outputs • Processing steps to produce the required output Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
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 Pamela Brauda
Calculator Turn on Press 1st number Press ‘+’ Press 2nd number Press ‘=‘ Computer Input 1st number Input 2nd number Add Print result (sum) Algorithm to add two numbers Pamela Brauda
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 Pamela Brauda
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} Pamela Brauda
Pseudocode • Getting results from the computer • Print ‘Program Completed’ {printer} • Write customer record to file {disk} • Display ‘End of Data’ {screen} Pamela Brauda
Pseudocode • Doing the math • Add number to total • total = total + number • Compute C = (F – 32) * 5/9 • Symbols okay to use: +, -, *, /, (, ) Pamela Brauda
Pseudocode • Assigning a value to a variable • Set Count to 0 • Initialize Totals to zero • Total = cost + tax Pamela Brauda
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 Pamela Brauda
Pseudocode • Repeat a group of actions: DO-WHILE, FOR Loop, DO Loop • WHILE student_total < 50 DO • Read student record • Print student name, address to report • Add 1 to student_total • ENDWHILE Pamela Brauda
Developing an algorithm • Define the problem Pamela Brauda