1 / 32

Problem Solving & Algorithms (DCT 1123)

Problem Solving & Algorithms (DCT 1123). Chapter 3: Problem Analysis. Contents. Algorithm Discovery Algorithm Design Strategies Stepwise Refinement Control Requirements Variable Data type Sample Problem & Solution. The Key Features of an Algorithm. Sequence (aka process)

ponce
Download Presentation

Problem Solving & Algorithms (DCT 1123)

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. Problem Solving & Algorithms(DCT 1123) Chapter 3: Problem Analysis

  2. Contents • Algorithm Discovery • Algorithm Design Strategies • Stepwise Refinement • Control Requirements • Variable • Data type • Sample Problem & Solution

  3. The Key Features of an Algorithm Sequence (aka process) Decision (aka selection) Repetition (aka iteration or looping)

  4. Sequence Each step or process in the algorithm is executed in the specified order Each processes must be in a correct place otherwise the algorithm will most probably fail

  5. The Decision construct If…then, If…then…else • The outcome of a decision is either true or false • It is based on some condition that can only result in a true or false for example: • If today is Friday then Friday Prayer • The decision can also be stated as: • If proposition then process 1 else process 2 • For example: If male then wear a bajumelayu else wear a bajukurung

  6. The Repetition Constructs • The repeat loop is used to iterate or repeat a process or sequence of process until some condition becomes true • The general form: • Repeat • Process 1 • Process 2 • Process n • Until proposition • Example: • Repeat • Put water in kettle • Until kettle is full

  7. Algorithm Design Strategies Step 1: Investigation step Identify the process Identify the major decision Identify the loops Identify the variable

  8. Algorithm Design Strategies Step 2: Preliminary algorithm step Devise a high level algorithm Step through the algorithm. Does this “walk-through” reveal any major problem? If it does, correct the problem

  9. Algorithm Design Strategies Step 3: Refining the algorithm step Incorporate any refinements indicated in step 2 Group together processes where appropriate Group together where appropriate Test the algorithm again by stepping through it

  10. Stepwise Refinement aka Top Down Approach A way of developing a computer program by first describing general functions, then breaking each function down into details which are refined in successive steps until the whole program is fully defined. Stepwise refinement was first introduced by Wirth in 1971, applying it to pseudo-code, flowchart, block diagrams, formal specifications and used in every phase of software development.

  11. Stepwise Refinement • Example: • Brush Teeth • find toothbrush • find toothpaste tube • open toothpaste tube • Put thumb and pointer finger on cap • turn fingers counter-clockwise • repeat prior step until cap falls off • squeeze tube onto toothbrush • (details omitted) • clean teeth • put brush on teeth • move back and fourth vigorously • repeat above step 100 times • clean up • rinse brush • turn on water • put head of brush under running water for 30 seconds • turn off water • put cap back on toothpaste • put all items back in cabinet

  12. Variable Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize also the number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now -for example- subtract and obtain 4 as result. The whole process that you have just done with your mental memory is a similar of what a computer can do with two variables.

  13. Variable a = 5; b = 2; a = a + 1; result = a - b;

  14. Variable Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them. Therefore, we can define a variable as a portion of memory to store a determined value.

  15. Data Types When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way. The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255).

  16. Data Types

  17. Defining the problem • Carefully reading and rereading the problem until you understand completely what is required • The problem should be divided into three separate components: • Input: a list of source data provided to the problem • Output: a list of the outputs required • Processing: a list of actions needed to produce the required output

  18. Sample Problem & Solution – defining problem Problem: A program is required to read three numbers, add them together and print their total Tackle this problem in two stages: • Underline the nouns and adjectives used in the specification. This will establish the input & output components • Nouns is “three numbers” • Adjectives is “total” • The input is three numbers and the output is the total

  19. Sample Problem & Solution – defining diagram

  20. Sample Problem & Solution At this stage, we have to use a meaningful name to represent the variables or objects in the problem and describe the processing steps. Now all nouns & verbs in the specification have been considered and the defining diagram is complete We understand the input to the problem, the output to be produced and the processing steps required to convert the input to the output When it comes to writing down the processing steps in an algorithm, you should use words that describe the work to be done in terms of single, specific tasks or functions

  21. Sample Problem & Solution – Solution Algorithms Add_three_numbers Read number1, number2, number 3 Total = number 1 + number 2 + number 3 Print total End

  22. Sample Problem & Solution - Flowchart Start Input number 1, number 2, number 3 Total = number 1 + number 2 + number 3 Display Total Stop

  23. Sample Problem & Solution –Checking the Solution Algorithm Once the solution algorithm has been constructed, it must be tested for correctness. This step is necessary, because most major logic errors occur during the development of algorithm It is much easier to detect errors in pseudocode than in the corresponding program code. It is because once programming begins, u usually assume that the logic of the algorithm is correct Desk checking is a method to trace the mistake in our algorithms It involves tracing through the logic of the algorithm with some chosen test data

  24. Sample Problem & Solution –Selecting Test Data When selecting test data to desk check an algorithm, you must look at the program specification and choose simple test cases only To desk check the algorithm, you need only a few simple test caes which will follow the major paths of the algorithm logic

  25. Sample Problem & Solution –Steps in desk checking an algorithm There are six simple steps to follow when desk checking an algorithm: Choose simple input test cases that are valid. Two or three test cases are usually sufficient Establish what the expected result should be for each test case. This is one of the reason for choosing simple test data. It is much easier to determine the total of 10,20 and 30 than 3.75, 2.89 and 5.31 Make a table of the relevant variable names within the algorithm on a piece of paper Walk the first test case through the algorithm, keeping a step-by-step record of the contents of each variable in the table as the data passes through the logic Repeat the walkthrough process using the other test cases, until the algorithm has reached its logical end Check that the expected result established in Step 2 matches the actual result developed in Step 5

  26. Sample Problem & Solution –Solution Algorithms Add_three_numbers Read number1, number2, number 3 Total = number 1 + number 2 + number 3 Print total End

  27. Sample Problem & Solution – Desk Check i. Choose TWO sets of input test data. The THREE numbers selected will be 10, 20 and 30 for the first case and 40, 41 and 42 for the second case ii. Establish the expected results for each case

  28. Sample Problem & Solution – Desk Check iii. Set up a table of relevant variable names, and pass each test data set thru the solution algorithm, statement by statement. Line numbers have been used to identify each statement within the program

  29. Sample Problem & Solution – Desk Check Check that the expected result (60 and 123) match the actual results (the total column in the table) The desk check indicates that the algorithm is correct. We can now proceed to code the algorithm into a programming language. If at the end of a desk check, the actual results do not match the expected results, the solution algorithm probably contains logic error. In this case, the programmer needs to go back to the solution algorithms

  30. Conclusion You must fully understand the problem before you can attempt to find a solution The method suggested was to analyze the actual words used in the specification with the aim of dividing the problem into three separate components; input, output & processing Then, you must attempt to find a solution and express this solution as an algorithm. To do this you must use the defining diagram, correct pseudocode statements and flowcharts Afterwards, check the algorithm for correctness

  31. Reference http://users.evtek.fi/~jaanah/IntroC/DBeech/3gl_algorithm1.htm Simple Program Design. A Step by Step Approach. Lesley Anne Robertson. Thomson Course Technology http://www.cplusplus.com/doc/tutorial/variables/

More Related