1 / 40

CHAPTER 2: Problem Solving

CHAPTER 2: Problem Solving. CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I By Badariah Solemon. Topics. Using computers in solving problems Software development method (SDM) Specification of needs Problem analysis Design and algorithmic representation Implementation

shenson
Télécharger la présentation

CHAPTER 2: Problem Solving

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. CHAPTER 2: Problem Solving CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I By Badariah Solemon BS (May 2012)

  2. Topics • Using computers in solving problems • Software development method (SDM) • Specification of needs • Problem analysis • Design and algorithmic representation • Implementation • Testing and verification • Documentation BS (May 2012)

  3. Topic 1 Using computers in solving problem BS (May 2012)

  4. Problem Solving • Programming is a problem solving activity. • When you write a program, you are actually writing an instruction for the computer to solve something for you • Problem solving is a process of transforming the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools. Problem solving (within the context of programming) refers to analyzing a problem with the intention of deriving a solution for the problem. BS (May 2012)

  5. Topic 2 Software development method BS (May 2012)

  6. Software Development Method (SDM) • Programmers solve problems using SDM, which has the following steps: • Specify the problem requirements • Analyze the problem • Design the algorithm to solve the problem • Implement the solution • Test and verify the solution • Provide documentation BS (May 2012)

  7. Topic 2-1 Specify the problem Requirements BS (May 2012)

  8. Specification of Requirements • State the problem clearly and unambigously (single interpretation) to understand exactly: • what the problem is • what is needed to solve it • what the solution should provide • if there are constraints and special conditions. BS (May 2012)

  9. Example: Apples • Problem: • Compute and display the total cost of apples given the number of kilogram (Kg) of apples purchased and the cost per Kg of apples. BS (May 2012)

  10. Topic 2-2 Analyze the problem BS (May 2012)

  11. Problem Analysis • Identify the following: • Inputs to the problem, their form and the input media to be used • Outputs expected from the problem, their form and the output media to be used • Special constraints or conditions (if any) • Formulas or equations to be used BS (May 2012)

  12. Example: Apples • Problem: Compute and display the total cost of apples given the number of kilogram (Kg) of apples purchased and the cost per Kg of apples. • Input: • QtyApple: Quantity of apples purchased (in Kg - integer) • Cost: Cost per Kg of apples (in RM per Kg - float) • Output: • Total: Total cost of apples (in RM) • Formula: • Total = Cost X QtyApple • Constraint: • Quantity purchased must be more than zero • Cost per Kg apples must be more than zero (it’s not free) BS (May 2012)

  13. Topic 2-3 Design the algorithm BS (May 2012)

  14. Design algorithm • Purpose: to develop and verify algorithm • An algorithm is a list of steps to be executed with the right order in which these steps should be executed. • Writing the algorithm is a difficult task  use top-down design (divide and conquer approach) • You first list down the major tasks • For each major task, you further divide it into sub-tasks (refinement step) • When we write algorithm, write it from the computer’s point of view BS (May 2012)

  15. Top-down design • List themajor steps (most algorithm consist of at least the following): • Get the data • Perform the computations • Display the result • Perform algorithm refinement – the step(s) may need to be broken down into a more detailed list of steps • Verify that the algorithm works as intended – perform desk check. BS (May 2012)

  16. Example: Apples - Steps * To ease understand, the two constraints (refer slide 12 are not yet considered in this chapter) 1. Major steps: 1. Get the Quantity of apples purchased (in Kg) 2. Get the Cost per Kg of apples (in RM per Kg) 3. Compute the total cost of apples purchase 4. Display the computed total cost 2. Refined Steps: 1. Get the Quantity of apples purchased (in Kg) 2. Get the Cost per Kg of apples (in RM per Kg) 3. Compute the total cost of apples purchase 3.1 The total cost = Cost per Kg X Quantity 4. Display the computed total cost BS (May 2012)

  17. Algorithm • Must have output(s) • Should not be ambiguous (there should not be different interpretations to it) • Must be general (can be used for different inputs) • Must be correct and it must solve the problem for which it is designed • Must execute and terminate in a finite amount of time • Must be efficient enough so that it can solve the intended problem using the resource currently available on the computer BS (May 2012)

  18. Specifying Order • Specifying the order in which the steps are to be executed is important. • Example algorithm for getting out of bed and prepare to go to work: • If the same steps are performed in a slightly different order: What would be the result? Get out of bed Take off pajamas Take a shower Get dressed Get out of bed Take off pajamas Get dressed Take a shower BS (May 2012)

  19. Algorithm • May be represented using pseudocode or flowchart. • Any algorithm can be described using only 3 control program structures: • Sequence • Selection* • Repetition* • Explanation for the selection and repetition structures are detailed in Chapter 5: Decision Making and Looping BS (May 2012)

  20. Algorithm You may have more than one control structures in one program in order to solve a problem. BS (May 2012)

  21. Pseudocode • Pseudocode is a semiformal, English-like language with limited vocabulary that can be used to design and describe algorithms. • Criteria of a good pseudocode: • Easy to understand, precise and clear • Gives the correct solution in all cases • Eventually ends BS (May 2012)

  22. Flowchart • Flowcharts is a graph used to depict or show a step by step solution using symbols which represent a task. • The symbols used consist of geometricalshapes that are connected by flow lines. • It is an alternative to pseudocoding; whereas a pseudocode description is verbal, a flowchart is graphical in nature. BS (May 2012)

  23. Flowchart: Symbols BS (May 2012)

  24. Flowchart: Symbols BS (May 2012)

  25. Sequence Control Structure • A series of steps or statements that are executed in a specific order. • The beginning and end of a block of statements can be optionally marked with the keywords Begin and End. • The structure: Begin statement – 1 statement – 2 statement – n End BS (May 2012)

  26. Example: Coarse Algorithm Begin Begin Prompt and get birth date calculate Age (the difference between the birth date and current date) print out the exact age of the user End Prompt & get birth date Calculate Age (difference of birth date and current date) Print Age End BS (May 2012)

  27. Example: Refined Algorithm Begin Begin Prompt and get BirthDate from user Calculate: Age = CurrentDate - BirthDate Print Age End Prompt and get BirthDate Calculate: Age = CurrentDate – BirthDate Print Age End BS (May 2012)

  28. Example: Apples • 2. Refined Steps: • 1. Get the Quantity of apples purchased (in Kg) • 2. Get the Cost per Kg of apples (in RM per Kg) • 3. Compute the total cost of apples purchase • 3.1 The total cost = Cost per Kg X Quantity • 4. Display the computed total cost Begin Prompt and get QtyApple Prompt and get Cost Begin Prompt and get QtyApple Prompt and get Cost Compute: TotalCost = Cost * QtyApple Display TotalCost End Compute: TotalCost = Cost * QtyApple Print TotalCost ** Don’t forget to verify that the algorithm works as intended End BS (May 2012)

  29. Topic 2-4 Implement the solution BS (May 2012)

  30. Write a Program • The process of implementing an algorithm by writing a computer program using a programming language (Example, using C language). • The output of the program must be the solution of the intended problem. BS (May 2012)

  31. Example: Apples BS (May 2012)

  32. Topic 2-5 Test and verify the solution BS (May 2012)

  33. Testing Program • Program testing is the process of executing a program to demonstrate its correctness • Program verification is the process of ensuring that a program meets user-requirement • After the program is compiled, we must run the program and test/verify it with differentinputs before the program can be released to the public or other users (or to the instructor of this class) BS (May 2012)

  34. Example: Apples BS (May 2012)

  35. Topic 2-6 Provide the documentation BS (May 2012)

  36. Documentation • Contains details produced at all stages of the program development cycle. • Can be done in 2 ways: • Writing comments between your line of codes. • Creating a separate text file to explain the program. • Important not only for other people to use or modify your program, but also for you to understand your own program after a long time. (Believe me, you will forget the details of your own program after sometime!) BS (May 2012)

  37. Documentation • Documentation is so important because: • You may return to this program in future to use the whole of or a part of it again • Other programmer or end user will need some information about your program for reference or maintenance • You may someday have to modify the program, or may discover some errors or weaknesses in your program • Although documentation is listed as the last stage of software development method, it is actually an on-going process which should be done from the very beginning of the software development process. BS (May 2012)

  38. Example BS (May 2012)

  39. Exercises • Write a pseudocode and prepare a flowchart for a simple C program that computes and displays the average of three numbers. • Write a pseudocode and prepare a flowchart for a C program that read the value of the height, width and length of a box from the user and print its volume. BS (May 2012)

  40. Summary • This chapter introduced the concept of problem solving - a process of transforming the description of a problem into a solution. • Software Development Method (SDM) which is consists of 6 steps – requirements specification, problem analysis, design the algorithm to solve the problem, implementation, testing and verification, and documentation • Algorithm may be represented using pseudocode or flow chart. • Algorithm can be described using 3 control structures : sequential, selection and repetition BS (May 2012)

More Related