Problem Solving Techniques


Published on

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Problem Solving Techniques

  1. 1. Program Design & Problem-Solving Techniques Management Student/ Freelance Developer University of Mauritius Module Pre-Requisites: None
  2. 2. Today’s Session <ul><li>Steps in program development </li></ul><ul><li>Algorithmic Problem Solving </li></ul><ul><li>How to write Pseudocode? </li></ul><ul><li>Meaningful names </li></ul><ul><li>Structure Theorem </li></ul><ul><li>Communication between modules </li></ul><ul><li>Module Cohesion and Coupling </li></ul><ul><li>Sequential File update </li></ul>
  3. 3. Problem Solving - Big Problemo!!!
  4. 4. How Do We Write a Program? <ul><li>A computer is not intelligent. It cannot analyze a problem and come up with a solution. A human (the programmer ) must analyze the problem, develop the instructions for solving the problem, and then have the computer carry out the instructions. </li></ul><ul><li>To write a program for a computer to follow, we must go through a two-phase process: problem solving and implementation </li></ul>
  5. 5. Problem-Solving Phase <ul><li>Analysis and Specification. Understand (define) the problem and what the solution must do. </li></ul><ul><li>General Solution (Algorithm). Specify the required data types and the logical sequences of steps that solve the problem. </li></ul><ul><li>Verify. Follow the steps exactly to see if the solution really does solve the problem. </li></ul>
  6. 6. Implementation Phase <ul><li>Concrete Solution (Program). Translate the algorithm (the general solution) into a programming language. </li></ul><ul><li>Test. Have the computer follow the instructions. Then manually check the results. If you find errors, analyze the program and the algorithm to determine the source of the errors, and then make corrections. </li></ul><ul><li>Once a program has been written, it enters a third phase: maintenance . </li></ul>
  7. 7. Maintenance Phase <ul><li>Use. Use the program. </li></ul><ul><li>Maintain. Modify the program to meet changing requirements or to correct any errors that show up while using it. </li></ul><ul><li>The programmer begins the programming process by analyzing the problem, breaking it into manageable pieces, and developing a general solution for each piece called an algorithm. The solutions to the pieces are collected together to form a program that solves the original problem. Understanding and analyzing a problem take up much more time than Figure 1.1 implies. They are the heart of the programming process. </li></ul>
  8. 9. Compiler and Interpreters
  9. 10. Steps in program development Ref:
  10. 11. Steps in program development <ul><li>Define the problem into three separate components: </li></ul><ul><ul><li>inputs </li></ul></ul><ul><ul><li>outputs </li></ul></ul><ul><ul><li>processing steps to produce required outputs. </li></ul></ul>
  11. 12. Steps in program development <ul><li>Outline the solution. </li></ul><ul><ul><li>Decompose the problem to smaller steps. </li></ul></ul><ul><ul><li>Establish a solution outline. </li></ul></ul><ul><ul><li>Initial outline may include: </li></ul></ul><ul><ul><ul><li>major processing steps involved </li></ul></ul></ul><ul><ul><ul><li>major subtasks </li></ul></ul></ul><ul><ul><ul><li>user interface </li></ul></ul></ul><ul><ul><ul><li>major control structures </li></ul></ul></ul><ul><ul><ul><li>major variable and record structures </li></ul></ul></ul><ul><ul><ul><li>mainline logic </li></ul></ul></ul>
  12. 13. Steps in program development <ul><li>Develop the outline into an algorithm. </li></ul><ul><ul><li>The solution outline is now expanded into an algorithm. </li></ul></ul><ul><ul><ul><li>What is an algorithm? – a set of precise steps that describe exactly the tasks to be performed and the order in which they are to be carried out. </li></ul></ul></ul><ul><ul><ul><li>Pseudocode will be used to represent the solution algorithm </li></ul></ul></ul>
  13. 14. <ul><li>Test the algorithm for correctness. </li></ul><ul><ul><li>Very important in the development of a program, but often forgotten </li></ul></ul><ul><ul><li>Major logic errors can be detected and corrected at an early stage </li></ul></ul><ul><ul><li>Go through the algorithm step-by-step with test data to ensure the program will actually do what it is supposed to do. </li></ul></ul>Steps in program development
  14. 15. <ul><li>Code the algorithm into a specific programming language. </li></ul><ul><ul><li>Start to code the program into a chosen programming language after all design considerations from Steps 1–4 are met. </li></ul></ul>Steps in program development
  15. 16. <ul><li>Run the program on the computer. </li></ul><ul><ul><li>This step uses a program compiler and programmer-designed test data to machine-test the code for </li></ul></ul><ul><ul><ul><li>syntax errors </li></ul></ul></ul><ul><ul><ul><li>logic errors </li></ul></ul></ul>Steps in program development
  16. 17. <ul><li>Document and maintain the program. </li></ul><ul><ul><li>Is really an ongoing task from the initial definition of the problem to the final test </li></ul></ul><ul><ul><li>Documentation involves: </li></ul></ul><ul><ul><ul><li>external documentation </li></ul></ul></ul><ul><ul><ul><li>internal documentation </li></ul></ul></ul>Steps in program development
  17. 18. <ul><li>Algorithmic Problem Solving </li></ul>
  18. 19. An introduction to algorithms and pseudocode <ul><li>What is an algorithm? </li></ul><ul><ul><li>Lists the steps involved in accomplishing a task (like a recipe) </li></ul></ul><ul><ul><li>Defined in programming terms as ‘a set of detailed and ordered instructions developed to describe the processes necessary to produce the desired output from a given input’ </li></ul></ul>
  19. 20. An introduction to algorithms and pseudocode <ul><li>What is an algorithm? </li></ul><ul><ul><li>An algorithm must: </li></ul></ul><ul><ul><ul><li>Be lucid, precise and unambiguous </li></ul></ul></ul><ul><ul><ul><li>Give the correct solution in all cases </li></ul></ul></ul><ul><ul><ul><li>Eventually end </li></ul></ul></ul>
  20. 21. An introduction to algorithms and pseudocode <ul><li>What is pseudocode? </li></ul><ul><ul><li>Structured English (formalised and abbreviated to look like high-level computer language) </li></ul></ul>
  21. 22. How to write pseudocode <ul><li>There are six basic computer operations: </li></ul><ul><ul><li>A computer can receive information </li></ul></ul><ul><ul><li>A computer can put out information </li></ul></ul><ul><ul><li>A computer can perform arithmetic </li></ul></ul><ul><ul><li>A computer can assign a value to a variable or memory location </li></ul></ul><ul><ul><li>A computer can compare two variables and select one of two alternate actions </li></ul></ul><ul><ul><li>A computer can repeat a group of actions </li></ul></ul>
  22. 23. Exercise Time <ul><li>Do some reading on the website below: </li></ul><ul><li> </li></ul>
  23. 24. Meaningful names <ul><li>When designing an algorithm, a programmer must introduce some unique names which represents variables or objects in the problem. </li></ul><ul><li>Names should be meaningful. </li></ul><ul><li>Names should be transparent to adequately describe variables (Number1, number2, etc.). </li></ul>
  24. 25. Meaningful names <ul><li>Underscore is used when using more than one word (sales_tax or word_count). </li></ul><ul><li>Most programming language does not tolerate a space in a variable as space would signal the end of a variable name. </li></ul><ul><li>Another method is to use capital letters as a word separator (salesTax or wordCount). </li></ul>
  25. 26. The Structure Theorem
  26. 27. The Structure Theorem <ul><li>Sequence </li></ul><ul><ul><li>Straightforward execution of one processing step after another </li></ul></ul><ul><ul><li>Represents the first four basic computer operations </li></ul></ul><ul><ul><ul><li>Receive information </li></ul></ul></ul><ul><ul><ul><li>Put out information </li></ul></ul></ul><ul><ul><ul><li>Perform arithmetic </li></ul></ul></ul><ul><ul><ul><li>Assign values </li></ul></ul></ul>
  27. 28. The Structure Theorem <ul><ul><li>A typical sequence statement in an algorithm might read: </li></ul></ul><ul><li>Add 1 to pageCount </li></ul><ul><li>Print heading line1 </li></ul><ul><li>Print heading line2 </li></ul><ul><li>Set lineCount to zero </li></ul><ul><li>Read customer record </li></ul><ul><ul><li>These instructions illustrate the sequence control structure as a straightforward list of steps written one after the other, in a top-to-bottom fashion </li></ul></ul>
  28. 29. The Structure Theorem <ul><li>Selection </li></ul><ul><ul><li>Presentation of a condition and the choice between two actions, the choice depending on whether the condition is true or false </li></ul></ul><ul><ul><li>Represents the decision-making abilities of the computer </li></ul></ul><ul><ul><li>Illustrates the fifth basic computer operation – compare two variables and select one of two alternate actions </li></ul></ul>
  29. 30. The Structure Theorem <ul><ul><li>In pseudocode, selection is represented by the keywords IF, THEN, ELSE and ENDIF </li></ul></ul><ul><ul><li>IF condition p is true THEN </li></ul></ul><ul><ul><li>statement(s) in true case </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li>statement(s) in false case </li></ul></ul><ul><ul><li>ENDIF </li></ul></ul><ul><ul><li>If condition p is true, then the statement in true case will be executed, and the statement in the false case will be skipped (vice versa) </li></ul></ul>
  30. 31. The Structure Theorem <ul><li>Repetition </li></ul><ul><ul><li>Presentation of a set of instruction to be performed repeatedly, as long as the condition is true </li></ul></ul><ul><ul><li>Block statement is executed again and again until a terminating condition occurs </li></ul></ul><ul><ul><li>Illustrates the sixth basic computer operation – to repeat a group of actions. </li></ul></ul>
  31. 32. The Structure Theorem <ul><li>Written in pseudocode as: </li></ul><ul><li>DOWHILE condition p is true </li></ul><ul><li>statement block </li></ul><ul><li>ENDDO </li></ul><ul><li>DOWHILE is a leading decision loop – condition is tested before any statements are executed </li></ul><ul><li>ENDDO triggers a return of control to the retesting of the condition </li></ul><ul><li>Condition is true, statements are repeated until condition is found false </li></ul>
  32. 33. Exercise Time <ul><li>In a complete program/ algorithm, show the 3 concepts of Structure Theorem </li></ul>
  33. 34. <ul><li>Necessary to consider flow of information between modules </li></ul><ul><li>This flow of information is called ‘ intermodule communication ’ and can be accomplished by the scope of the variable </li></ul>Communication between modules
  34. 35. <ul><li>Scope of a variable </li></ul><ul><ul><li>The portion of a program in which that variable has been defined and to which it can be referenced </li></ul></ul><ul><ul><li>Variables can be global where the scope of the variable is the whole program </li></ul></ul><ul><ul><li>Scope of the variable is simple the module which it is defined </li></ul></ul>Communication between modules
  35. 36. <ul><li>Global data </li></ul><ul><ul><li>Date that can be used by all the modules in a program </li></ul></ul><ul><ul><li>Every module in the program can access and change data </li></ul></ul><ul><ul><li>Lifetime of a global variable spans the execution of the whole program </li></ul></ul>Communication between modules
  36. 37. <ul><li>Local data </li></ul><ul><ul><li>Variable are defined within the submodule are called local variables </li></ul></ul><ul><ul><li>The scope of a local variable is simply the module in which it is defined </li></ul></ul><ul><ul><li>The lifetime of a local variable is limited to the execution of the single submodule in which it is defined </li></ul></ul>Communication between modules
  37. 38. Exercise time <ul><li>Show the difference between a global variable and a local variable via the use of a program, in any language of your choice. </li></ul>
  38. 39. <ul><li>Side effects </li></ul><ul><ul><li>Side effect is a form of a cross-communication of a module with other parts of a program, </li></ul></ul><ul><ul><li>Occurs when a subordinate module alters the value of a global variable inside a module </li></ul></ul>Communication between modules
  39. 40. <ul><li>Passing parameters </li></ul><ul><ul><li>Parameters are simply data items transferred from a calling module to its subordinate module at the time of calling </li></ul></ul><ul><ul><li>To pass parameters between modules, two things can happen: </li></ul></ul><ul><ul><ul><li>The calling module must name the parameters that it wants to pass to the submodule </li></ul></ul></ul><ul><ul><ul><li>The submodule must be able to receive those parameters and return them to the calling module if required </li></ul></ul></ul>Communication between modules
  40. 41. <ul><li>Formal and actual parameters </li></ul><ul><ul><li>Parameters names that appear when a submodule is defined are known as formal parameters </li></ul></ul><ul><ul><li>Variables and expressions that are passed to a submodule in a particular call are called actual parameters </li></ul></ul>Communication between modules
  41. 42. <ul><li>Value and reference parameters </li></ul><ul><ul><li>Parameters may have one of three function: </li></ul></ul><ul><ul><ul><li>To pass information from a calling module to a subordinate module </li></ul></ul></ul><ul><ul><ul><li>To pass information from a subordinate module to its calling module </li></ul></ul></ul><ul><ul><ul><li>To fulfil a two-way communication role </li></ul></ul></ul>Communication between modules
  42. 43. <ul><li>Value and reference parameters </li></ul><ul><ul><li>Value parameters </li></ul></ul><ul><ul><ul><li>Value parameters pass a copy of the value of a parameter from one module to another </li></ul></ul></ul><ul><ul><li>Reference parameters </li></ul></ul><ul><ul><ul><li>Reference parameter pass the memory address of a parameter from one module to another </li></ul></ul></ul>Communication between modules
  43. 44. Module cohesion <ul><li>Cohesion is a measure of the internal strength of a module </li></ul><ul><li>It indicates how closely the elements or the statements of a module are associated with each other </li></ul><ul><li>The more closely the elements of a module are associated with each other, the higher the cohesion of the module </li></ul>
  44. 45. Module coupling <ul><li>Coupling is a measure of the extent of information interchange between modules </li></ul><ul><li>Tight coupling implies large dependence on the structure of one module by another </li></ul><ul><li>Loose coupling is the opposite of tight coupling. Modules with loose coupling are more independent and easier to maintain </li></ul>
  45. 46. Sequential file update <ul><li>Master file </li></ul><ul><ul><li>Is a file that contains permanent and semi-permanent information about the data entities it contains </li></ul></ul><ul><li>Transaction file </li></ul><ul><ul><li>Contains all the data and activities that are included on the master file. These are transactions to: </li></ul></ul><ul><ul><ul><li>Add a new record </li></ul></ul></ul><ul><ul><ul><li>Update or change an existing record </li></ul></ul></ul><ul><ul><ul><li>Delete an existing record </li></ul></ul></ul>
  46. 47. Sequential file update <ul><li>Sequential update logic </li></ul><ul><ul><li>Sequential processing falls generally into three categories: </li></ul></ul><ul><ul><ul><li>If the key on the transaction record is less than the key on the old master record, the transaction is probably an add transaction </li></ul></ul></ul><ul><ul><ul><li>If they key on the transaction record is equal to the key on the old master record, the transaction is probably an update or delete transaction </li></ul></ul></ul><ul><ul><ul><li>If the key on the transaction record is greater that the key on the old master record, there is not matching transaction for that master record </li></ul></ul></ul>
  47. 48. The End – Thank you for your kind attention <ul><li>References: </li></ul><ul><ul><li>Simple Program Design – A step by step approach by L. A. Robertson </li></ul></ul>
  48. 49. Management Student/ Freelance Developer University of Mauritius BCS Student Member