1 / 47

Problem solving vs. programming

Problem solving vs. programming. Problem solving vs. algorithm development vs. programming. Problem Solving. understand and define the problem what is it that needs to be solved Identify an approach the basic equations, relations, etc Develop and specify an algorithm

tanner
Télécharger la présentation

Problem solving vs. programming

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 vs. programming • Problem solving vs. algorithm development vs. programming

  2. Problem Solving • understand and define the problem • what is it that needs to be solved • Identify an approach • the basic equations, relations, etc • Develop and specify an algorithm • sequence of steps to be executed • specify how data is organized

  3. . • Evaluate the algorithm • always provide correct results ? efficient ? • Coding (or Programming)

  4. . Test and validate • Debug program • confirm -- gives expected results? • confirm efficiency

  5. . • Documentation • problem, its solution, algorithm, program, testing procedure • Program maintenance • Make necessary changes if errors are found • Ensure that it meets user changing requirements

  6. Problem Understanding and definition • Perhaps the most important step • Understanding is based on interaction with customer • From vague statements to full specification • Starts with “develop software that helps me with inventory control” • Ultimately develop a problem definition

  7. . • Input data • List of input parameters • Parameter type ( Integer, real, character…) • Special symbols to indicate end of list, for instance

  8. . • Output data • List of parameters, format, headings, etc. • Required processing • understand and specify how output is related to input • Identify special processing to handle error conditions, corner cases e.g. “list is too long”, “invalid parameter value”

  9. Algorithms • steps needed to solve the problem • Definition of an algorithm • an ordered sequence of well-defined and effective operations which, when executed, will terminate in a finite amount of time and produce correct result

  10. . • Ordered sequence • Well-defined and effective • Terminate ( always) • Produce correct results

  11. Correct Result ? • Given area of a rectangle A, and one of the sides d, find the other side s. s = A / d Input A, d and get output s If input d= 0, then what happens? Take care in the algorithm itself.

  12. Well defined?. compute largest prime compute square root of x

  13. Termination • To find complement of given set of angles • Ask for first value of angle A • Read the value as entered by user • Print 90 – A • Repeat for next value of A • When does this program terminate?

  14. Finding Square root • Given a number m • find n • Such that n x n = m • Paper-pencil method ?? • on computer • 3 different ways

  15. Algorithm 1 • Given m, guess n such that n x n = m • If not, try next nearest number • Consider m = 70 • n lies between 8 and 9 • Try 8.5, 8.5 x 8.5 = 72.25 • Try between 8 and 8.5 • 8.2 x 8.2 = 67.24 • 8.35 x 8.35 = 69.7225 • 8.37 x 8.37 = 70.0569

  16. Algorithm 2 • or try systematically • 8.01, 8.02, …… • Initial guessvery crucial • Step size also important • Why?? • Better algorithm should be independent of initial guess and step size

  17. Better algorithm m = j x k root n lies between j & k • Let m = 36, • Choose any j, say 9 • j = 9 , so k = m / j = 4 • 9 x 9 = 81; 4 x 4 = 16 • Root definitely lies between 9 and 4 • Let j = ( 9 + 4 ) / 2 = 6.5

  18. 6.5 x 6.5 is more than 36 • k = 36 / 6.5 = 5.53 • k x k = 5.53 x 5.53 = 30.58 • Better estimate j = ( 6.5 + 5.53) / 2 • j = 6.015 • Now j x j almost 36 • Can improve k = 36 / 6.015 = 5.985 • Next j = (6.015 + 5.985 )/2 = 6

  19. May not yield exact root • But gets very close • Converging to actual root • stop when difference two roots less than certain error • Does not heavily depend on initial guess

  20. Method • Initial guess : can be m/2 • Let g1 and g2 be the two factors • let g2 = m / 2 • Repeat the following • g1 = g2 • g = m / g1 • g2 = ( g1 + g ) /2 • until g1 and g2 are very close

  21. Another algorithm Smallest divisor of an integer

  22. Find smallest exact Divisor • 23 1 no other number • 625 5 other than 1 • 242 2 • 711 9 • 323 ? Find out • Keep on dividing by 2,3,4,5,…..,n –1 • Make a note of no. of divisions needed • 17

  23. improvements • All divisors necessary? • Consider 127 • Divisors : 2,3,4,5,6,7,8,9,10,11 • If no. is even, then 4,6,8,10…no need. • if no. is odd then also not needed • So even numbers : no testing needed • 247568 ( smallest divisor is 2 ) • Examine only odd numbers

  24. More improvements • Potential divisors: 3,5,7,9,11,13,… • Till what number ? • Consider 323 • 3,5,7,………..,313,315,317,319,321 • Consider 48 • 2x24, 3 x16, 4x 12, 6x 8 • 8 x 6, 12 x 4, 16 x 3, 24 x 2 (repetition) • Can stop test at sq. root of 48

  25. Holds for any factorizable number • Should hold true for prime nos. as well • Consider 53 • 3,5,7,9,11,13,15,17,19,21,23,…,51 • No need to test beyond 7 or 8

  26. Algorithm • Check if divisible by 2 • If not proceed further • d = 3 • rem = n mod d • while ( rem != 0 and d <= sqrt(n) ) • { d = d +2 • rem = n mod d } • Why compute sqrt every time ??

  27. Refined algo. • if n mod 2 = 0 then sd = 2 • else sn = integer part of sqrt (n) • d = 3 • rem = n mod d • while ( rem != 0 and d <= sn ) • { d = d +2 • rem = n mod d } • how to know if sd =1 or otherwise ?

  28. Actually no need to find square root of n • Look at factors of 320 • 5 x 64 • 8 x 40 • 10 x 32 • 16 x 20 • After that it will be just a repetition of factors, so no point proceeding after the number 20 • At every step the range of divisors keeps on reducing • This will hold good for any odd number as well

  29. Final idea for the algorithm • So given a number n, check if it is even • If not start with a divisor d = 3 • Now the range of divisors to be selected is up to p ( = n / d). • If there is a remainder, increase d through • d= d+2 • Find new p the range…and continue loop

  30. How to represent algorithms • English language • Pseudo code • flowchart

  31. English language • Ambiguous • Print names of all students who are majoring in computer science and whose grade point average is below 3.00 or whose current status is Graduate. • Interpretation 1 • Print names of computer sc. students with GPA below 3.00 or • Graduate students (of any major and any GPA) • Interpretation 2 • Print names of students who are computer sc majors • with GPA below 3.00 or graduate students.

  32. Pseudo coding • A pseudo code language is a semi-informal English like language • With a limited vocabulary • Written in a shorthand form • Produce simple narrative notation Any algorithm needs 3 basic control structures: Sequence ( statements), selection (if-then-else), repetition (for , while loop)

  33. Example of problem solving • Develop a computer program to compute income tax from tax rate schedules • Tax tables can be used for taxable incomes less than $50,000. Otherwise, one has to use the tax rate schedules • Single >49,300 : 11000 + 31% of amount >49300 • Married filing jointly • 34,000< Income <82000: 5100 + 28% of amount > 34000 • Income >82,000 : 18600 + 31% of amount over 82000 • Married filing separately:……etc. • Head of household: …….etc.

  34. Problem analysis • Input • Taxable income • Filing status ( interactive inputs ) • Output • Printing the inputs and computed tax • Constraints • Income below 50,000 not acceptable • Filing status : only as desired in specifications • formulas

  35. Top-Down Stepwise Refinement • Compute income tax from tax schedules: first algorithm • Read and verify income • Read and verify filing status • Compute tax • Print income, status and computed tax Each step now needs to be expanded further

  36. Step 1 refined • Print “Enter income: should be >=50000” • read income • while income < 50000 • begin • Print “Enter income………………00” • Read income • end • end while • Note Indentation of begin, end , while etc.

  37. Refinement of step 2 • First decide how do you want this data to be entered by the user • In string form or in form of numbers? • String form may be difficult to match with upper case/lower case/spaces /spellings etc • May be wiser to supply a menu and ask them just to enter a choice of number

  38. Step 2 continued • How to verify that the choice of filing status is entered correctly • Use a while loop • while entered value is incorrect (tricky ?) • ask user to enter correct value • end – while • Suppose again he enters wrong value ?

  39. Step 3 • This is easy, just use if –else statements and the given formula • Step 4 • If filing status = 1 print “single” etc.

  40. flowcharting • Flowchart is a graph consisting of geometrical shapes that are connected by flow lines • OK for small algorithms • For large algorithms the loops may be complicated and very long • Not recommended

  41. Successful algorithms must consider all possible cases presented by acceptable data. • You will succeed more quickly at constructing algorithms if you make it a habit to • · think about the problem and its data, then • · enumerate all the special cases that the algorithm must handle.

  42. Properties of good algorithms 1. Precision ·each step must be clear and unambiguous in its meaning ·the order of execution of the steps must be clear ·the number of steps must be finite ·each step must itself be finite 2. Simplicity ·each step must be simple enough that it can be easily understood ·each step should translate into only a few (or one) computer operation or instruction 3. Levels of abstraction ·the steps in the algorithm should be grouped into related modules or blocks ·you may use one module inside another module ·you may refer to other algorithms by name instead of including all of their steps in the current algorithm

  43. Abstraction • In Computer Science: ·Abstraction refers to the LOGICAL GROUPING of concepts or objects. • ·     We focus on general characteristics instead of concrete realities, specific objects or actual instances • ·Define/implement the general idea, isolate the details

  44. Well-designed algorithms will be organized in terms of abstraction. This means that we can refer to each of the major logical steps without being distracted by the details that make up each one. The simple instructions that make up each logical step are hidden inside modules. These modules allow us to function at a higher level, to hide the details of each step inside a module, and then to refer to that module by name whenever we need it. B By hiding the details inside appropriate modules, we can understand the main ideas without being distracted. This is a key goal of using levels of abstraction: ·Each module represents an abstraction. The name of the module describe the idea that the module implements. The instructions hidden within the module specify how that abstraction is implemented. ·We can see what is being done (the idea) by reading the descriptive name of the module without having to pay attention to how it is being done. ·If we want to understand how it is being done, we can look inside the module to find out.

  45. Software Development Steps of developing software to solve a problem: 1.Problem Understanding ·Read the problem carefully and try to understand what is required for its solution. 2.Analysis ·Identify problem inputs and outputs. 3.Design (Top-down design) ·Map out the modular structure of your algorithm. ·Refine your modular design. Give a descriptive identifier for each module. Does each module do only one logical task? Subdivide any module that does not, as many times as necessary. ·Define the interface of each module before writing any code. ·Begin the bottom-up work of constructing each module. 4.Implementation ·Implement the algorithm as a (C) program. ·Convert steps of the algorithm into programming language statements. 5.Testing and Verification ·Test the completed program, and verify that it works as expected. ·Use different test cases (not one) including critical test cases.

More Related