1 / 24

DCT 1123 PROBLEM SOLVING & ALGORITHMS

DCT 1123 PROBLEM SOLVING & ALGORITHMS. Chapter 2: PROBLEM SOLVING. Steps in Program Development. Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into specific programming language

lynch
Download Presentation

DCT 1123 PROBLEM SOLVING & ALGORITHMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DCT 1123PROBLEM SOLVING & ALGORITHMS Chapter 2: PROBLEM SOLVING

  2. Steps in Program Development • Define the problem • Outline the solution • Develop the outline into an algorithm • Test the algorithm for correctness • Code the algorithm into specific programming language • Run the program on the computer • Document and maintain the program

  3. How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a good programmer. • Problem-solving methods are covered in many subject areas: - Business students learn to solve problems with a systems approach - Engineering and Science students use the engineering and science methods - Programmers use the Software Development Method

  4. Problem Solving Process • Phase 1 - Analyze the problem • Outline the problem and its requirements • Design steps (algorithm) to solve the problem (Natural language, Flow chart, pseudo code, …) • Algorithm tracing • Phase 2 - Implement the algorithm • Implement the algorithm in code (in Programming Language  Program) • Verify that the algorithm works (Verification and validation) • Phase 3 - Maintenance • Use and modify the program if the requirements changes

  5. Problem Solving and Programming Strategy • Programming is a process of problem solving. • The problem is solved according to the problem domain (e.g. students, money). • To be a good problem solver and hence a good programmer, you must follow good problem solving technique.

  6. Problem Specification Specifications can include the following: - Does the problem require interaction with the user? - Does the problem manipulate data? • What is the input data & how it is represented? - Does the problem produce output? How the results should be generated and formatted. - What are the required formula for solution - Is there any constraints on problem solution?

  7. Problem Specification An Example for Problem Specifications: Problem Statement: Determine the total cost of apples given the number of kilos of apples purchased and the cost per kilo of apples. We can summarize the information contained in the problem statement as follows:

  8. Problem Input: - Quantity of apples purchased (in kilos) - Cost per kilo of apples (in dinars per kilo) • Problem Output: - Total cost of apples (in dinars) • Formula: Total cost = Number of kilos of apples × Cost per kilo

  9. Problem Solving Strategies • Working backwards • Reverse-engineer • Once you know it can be done, it is much easier to do • What are some examples? • Look for a related problem that has been solved before • Java design patterns • Sort a particular list such as: David, Alice, Carol and Bob to find a general sorting algorithm • Stepwise Refinement • Break the problem into several sub-problems • Solve each subproblem separately • Produces a modular structure • K.I.S = Keep It Simple!

  10. Stepwise Refinement • Stepwise refinement is a top-down methodology in that it progresses from the general to the specific. • Bottom-up methodologies progress from the specific to the general. • These approaches complement each other • Solutions produced by stepwise refinement posses a natural modular structure - hence its popularity in algorithmic design.

  11. Analyze the Problem (1) Outline the problem and its requirements • Understand problem requirements • Does program require user interaction? • Does program manipulate data? • What is the output? • If the problem is complex, divide it into subproblems • Analyze each subproblem as above

  12. Analyze the Problem (1) Design Algorithm The most widely used notations for developing algorithms are flowcharts and pseudo-code. These are independent of the programming language to be used to implement the algorithm.

  13. Analyze the Problem (1) Algorithm Tracing Find all possible paths Check each path with appropriate input data

  14. Analyze the Problem (2) Problem Example. Convert a student mark from decimal mode to ABC mode. Understand problem requirements Does program require user interaction? Input the mark Does program manipulate data?  convert mark What is the output? The mark converted to A or B or C or error Is there subproblem? No

  15. Analyze the Problem (2) Design Algorithm  Flowchart & Pseudocode

  16. Implement the algorithm • Use any programming language (C++) to write a program to solve the problem. Where program is a sequence of statements.

  17. Implement the algorithm • The algorithm would consist of at least the following tasks: 1- Input (Read the data) 2- Processing (Perform the computation) 3- Output (Display the results)

  18. Implement the algorithm Computer Stage: after completing the program, the computer must do the following: Compilation: the computer compile the program. If it has errors, then an error list is produced to be corrected by the programmer. Execution: when the program become don’t has errors, the computer execute it to produce the ……output.

  19. Implement the algorithm Verify that the algorithm works  source of errors Many errors made in: analyzing the problem, developing an algorithm, and/or coding the algorithm, only become apparent when you try to compile, run, and test the resulting program.

  20. Implement the algorithm Errors are of three types: . syntax errors . run-time errors . logic errors • Syntax errors: detected by the C compiler . source code does not conform to one or more of C’s grammar rules . examples of syntax errors: undeclared variable, … • Often one mistake leads to multiple error messages – can be confusing

  21. Implement the algorithm Run-time errors. detected and displayed by computer during execution. - Occur when program directs computer to perform illegal operation. Example: int x=y/0; - will stop program execution and display message • Logic errors. - Includes errors that do not prevent execution of program - sign of error: incorrect program output cure: thorough testing and comparison with expected results

More Related