1 / 74

Program Abstraction

Program Abstraction. The solution to a given problem should not be based on a particular computer language. Otherwise, we spend more time in thinking about how to fit the solution to language syntax rather than thinking about the logic of solution to the problem.

lois-boone
Télécharger la présentation

Program Abstraction

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. Program Abstraction

  2. The solution to a given problem should not be based on a particular computer language. • Otherwise, we spend more time in thinking about how to fit the solution to language syntax rather than thinking about the logic of solution to the problem. • We should think about the solution ignoring the language constructs such as input and output statements, data types, syntax of iteration and selection structures etc.,

  3. These decisions can be made when we are certain that the solution correctly solves the problem. • Developing a solution to a problem by ignoring the context of programming language is called 'program abstraction'. • Such an abstract solution is called an 'algorithm'. • Once developed, the algorithmic solution must be documented for later use. • There are two notations to document algorithms, namely, pseudo-code and flow-chart.

  4. Problems, Algorithms and Programs • An Algorithm is the method one has to use to solve a Problem. • A Program is the precise expression of an algorithm in a language that could be followed by a computer.

  5. Software Development Process • Understand the Problem • Come up with an Algorithm to solve it • Flesh out the Algorithm in a program

  6. What is an Algorithm? • An algorithm is a methodical approach to solving a problem • Examples: • An algorithm to multiply X and Y is to add X to itself Y times • An algorithm to compute the average of N numbers is to add them up and then divide by N

  7. The Makeup of an Algorithm • An algorithm is made up of a set of “basic steps” that are followed sequentially. • Each algorithm must have a single “entry” point from which the algorithm is started. • Each algorithm must have one or more “exit” points that indicate when the algorithm is finished.

  8. What Constitutes a Basic Algorithmic Step? • Input a value into a “memory variable” • Output a value from a “memory variable” • Assigning and reassigning a value to a variable • Performing a math or some other operation on a number of memory variables • Deciding the next step based on the outcome of a yes/no question • Decision/choice

  9. Tracing of an Algorithm • Tracing an algorithm means checking or verifying the algorithm or flowchart whether is working properly or not. • It is one of the way to find out errors in an algorithm or flowchart. • Through trace table one can easily detect error in their flowchart. • Trace table is also known as verification table. • Through algorithm we can draw flowchart and from flowchart we can draw the Trace Table. • In Trace Table we take Steps, Input variable, Intermediate Variable and Output.

  10. Examples: 1) To perform addition of two numbers: • Algorithm:- • Start • Read Values a and b • Compute a + b and store result in sum. • Print value of Sum • Stop • Pseudo Code:- Begin input a, b sum = a+ b Print sum End

  11. Tracing of algorithm with a=10 and b=20 Start Read a & B Sum = a + b Print Sum Stop

  12. 1) To perform addition of two numbers and square of sum: • Algorithm:- • Start • Read Values a and b • Compute a + b and store result in sum. • Compute sum * sum and store in square. • Print value of Square • Stop • Pseudo Code:- Begin input a, b sum = a+ b square = sum * sum Print square End

  13. Tracing of an algorithm with a=3 and b=2 Start Read a & B Sum = a + b Square= sum * sum Print Square Stop

  14. 3) To find maximum of 2 numbers: • Algorithm:- • Start • Input two numbers a and b • If a > b then goto step 4 else goto step 5 • Output a • Output b • Stop

  15. Tracing with a=5 and b=10 Start Input a,b If a > b Yes No Output a Output b Stop

  16. Implementation Of Algorithm • Algorithms must be designed in such a fashion that they follows the pure top down approach. • This ensures the straight line execution path. • Following points should be kept in mind while writing algorithms: 1)Use of procedures: • We write program referring the algorithm. • It should emphasis on development of implementation & readability. • Modularizing programs will accomplish both of these needs. • This allows us to implement independent separate procedures to perform specific task. • The main program can have references to different procedures.

  17. 2) Choice of variable Names: • By choosing appropriate variable names & constants we can make programs more meaningful and easier to understand. • Eg: If we wish to add the two numbers and store the result; we can use variables as Num1, Num2 and Sum instead of a,b,c. • This makes the program self explanatory & self documented. • One should ensure that each variable should have only one role in a given program.

  18. 3)Documentation of Programs: • This is a useful practice to have in programs. • Brief and accurate comments should be written at the beginning of each procedure/function. • Program documentation is a good practice because the program can be used easily by other people unfamiliar with the working and input requirements of the program. • This indicates that the program must specify during execution exactly what responses require from the user.

  19. 4)Debugging the program: • It is necessary to carry out number of tests during implementation of algorithm , to ensure that the program is behaving correctly according to its specifications. • Program may have some logical errors which may not be detected during compilation. • To detect such type of errors one may include “print” statements to print information at strategic points in computations. • There are no complete methods for debugging. • However following steps can be taken to ease the burden of process: 1) Always work the program through by hand before ever attempting execute it. 2) Print out the strategic information.

  20. 5) Program Testing: • The program should be tested against all possible inputs. • Some of the things we might check are whether the program solves the smallest possible problem, whether it handles all the cases for varying input data. • Whenever possible programs should be accompanied by input and output sections.

  21. From Algorithm to a Program • Flowcharts are aimed at “humans”; they help programmers “visualize” the algorithm to be followed to solve a problem. • Flowcharts are not suitable for computers to execute because they are not “detailed” and “specific” enough. • To represent an algorithm to be followed by a computer, we have to express that algorithm as a program.

  22. Representation of an Algorithm • An algorithm can be represented as: • Flow Charts • Pseudo code • Programs

  23. Flow Charts • The flowchart is a means of visually presenting the flow of data through an information processing systems, the operations performed within the system and the sequence in which they are performed. • A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. • Flowcharts are generally drawn in the early stages of formulating computer solutions. • Flowcharts facilitate communication between programmers and business people.

  24. Flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. • Once the flowchart is drawn, it becomes easy to write the program in any high level language. • Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.

  25. Symbols Start/Stop Assignment Statements, Expressions Input/Output Decision Making Connector Flow Indicators

  26. ADVANTAGES OF USING FLOWCHARTS The benefits of flowcharts are as follows: 1. Communication: Flowcharts are better way of communicating the logic of a system to all concerned. 2. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way. 3. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes.

  27. 4. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. 5.Proper Debugging: The flowchart helps in debugging process. 6.Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

  28. LIMITATIONS OF USING FLOWCHARTS 1.Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. 2. Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely. 3.Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. 4.The essentials of what is done can easily be lost in the technical details of how it is done.

  29. Pseudo code • Pseudo-Code is simply a numbered list of instructions to perform some task. • It consists of short, English phrases used to explain specific tasks within a program’s algorithm. • It is also called as Structured English. • It requires an algorithm to be specified with the same precision as that of the programming language in which the program is eventually written.

  30. By moving from diagrams to text , pseudo code brings the design process a step closer to the coding process and makes the ultimate translation of the algorithm into a programming language somewhat easier.

  31. Why is pseudo code necessary? • Programming process is a complicated task. • Firstly it is necessary to understand the program specifications. • Then it is necessary to organize thoughts and create the program. • This is a difficult task when program is big and complex. • A main task is broken down into smaller ones, in order to be able to write fully developed code. • Writing Pseudo Code will save time later during the construction and testing phase of a program’s development.

  32. How to write pseudo code? • First make a list of the main tasks that must be accomplished on a piece of scratch paper. • Then, focus on each of those tasks. • Try to break each main task down into very small tasks that can each be explained with a short phrase. • There may eventually be a one-to-one correlation between the lines of pseudo code and the lines of the code that you write after you have finished pseudo coding.

  33. Functions and procedures can be shown within pseudo code. • Purpose of pseudo code is to help the programmer efficiently write code. Therefore, you must honestly attempt to add enough detail and analysis to the pseudo code. • In the professional programming world, workers who write pseudo code are often not the same people that write the actual code for a program. In fact, sometimes the person who writes the pseudo code does not know beforehand what programming language will be used to eventually write the program.

  34. Pseudo code Language Constructions • Input/Output: Get “variable”,”variable”,…. Display “variable”,”variable”,…. • Computation/Assignment: Set the value of “variable” to “arithmetic expression” or “variable” = “expression”

  35. Conditional: 6. If “condition” 6.1 statement 1 6.2 ………… 7. Else 7.1 statement 2 7.2 …………. • Iterative : 9. While “condition” 9.1 statement 1 9.2 …………………

  36. Algorithm, Pseudo code, Flowchart to add two numbers • Algorithm:- • Start • Read Values a and b • Compute a + b and store result in sum. • Print value of Sum • Stop • Pseudo Code:- Begin input a, b sum = a+ b Print sum End

  37. Start Read a & B Sum = a + b Print Sum Stop

  38. SEARCHING

  39. Information retrieval is one of the most important applications of computers. • Suppose you want to identify the name of student, whose roll no is 150. • Is it sufficient for you to get the information which you need.? No since there may be many classes. • We may now add another attribute to our query- “second year”. Even now it may fail, as there may be branches. So another attribute branch name. • Thus we can now formulate the query as: • Find the name of student having Roll. No. 150, studying in second year computer science.

  40. Every time we are given with a piece of information (e.g. Roll no) and we are asked to find the record that contains associated information for the given piece of information. • The initial piece of information is called “Key”. • Using Key, we are retrieving the information from the database. This is called searching. • Hence searching is a technique where we try to obtain certain information, given a key information.

  41. Sequential Search • The simplest way to do a search is to begin at one end of the information, scan down, until the desired key is found or the end is reached. • This method is called sequential or linear search. • More precisely , we can define sequential search as: A=<a1,a2,a3,….an> be the list of ‘n’ records, each record has a key element ai. Given a key value ‘v’ we want to find the information or retrieve the record from the list, such that v= ai, i.e. we are finding the target record, by its key value.

  42. Algorithm: Let A be an array of structure, having two fields Rollno and name. The algorithm for making the sequential search to search for a student having Rollno provided as a key is given as follows: Step 1: Read the information of ‘n’ students in the array A. (i.e. rollno and name). Step 2: Read the Rollno, which you want to search, say key. Step 3: Initialize Counter I to 1. Step 4: Repeat steps 5 & 6 until I < n,

  43. Step 5: If (A[i].rollno = key) then Print ‘Key Found’, Print Rollno, name and goto step 8. Step 6: i=i+1 i.e advance to next entry in the list and goto step 5. Step 7: Else Print ‘No such entry in list’. Steo 8: Stop.

  44. start Read how many nos say n Flowchart: Read the array A of n nos. Read the no. to be searched say x i=1 i=i+1 Is a[i]=x No Yes Yes Is i<= n Print “no. found” No Print “no. not found” Stop

  45. Advantages: • It is very easy to implement. It requires no additional data structure. Disadvantage: • Its performance degrades when the list grows. Suppose you are searching for a telephone number of Mr.Xavier and that you are searching it sequentially from the beginning. You may read unnecessarily many entries, which could have been avoided.

  46. Binary Search • Binary search is an improvement in searching method to reduce the amount of work that is made. • If the list is organized in order (just like in telephone directory, where the names are arranged alphabetically) one of the best approach is to , first compare the required target key with the center of the list, and then restrict ourselves to only the first or second half of the list, depending on whether the required item comes before or after the center one. • In this way at each step we reduce the length of the list to be searched by half, hence the name binary search.

  47. For example: we are searching for telephone number of Mr.Xavier. The current page which we have opened of the directory is containing names starting with letter say ‘k’. Since ‘x’ > ‘k’ (X of Xavier) we continue to search the upper (next) pages, leaving the earlier pages. This method, can locate a name from a list of million names, within only 20 comparisons. Binary Search method is applicable only when the elements are arranged in order.

  48. Algorithm: Step1: Read the ‘n’ elements in an array, say A. Step 2: Read the element to be searched say X. Step 3: Low = 1, High =n (initialize the lower and upper limit of the list to be searched) Step 4: Find the middle element, mid-position = (Low + High)/2 Y=A [Mid-position] Step 5: If (X =Y) then Print “element found and its location is ‘mid position’” Goto Step 9.

  49. Step 6: If (X<Y) (i.e. element to be searched is less than the middle element) then search to be continued in lower half; high = mid-position – 1 goto step 8. Step 7: Otherwise the element is in upper half, low = mid-position + 1. Step 8: If (High >= Low) then goto step 4 else print “Element not in the list” Step 9: Stop.

  50. Example 1: Let n=5, and array A have elements: 10, 20, 30, 40, 50 Now we will run the algorithm for searching the element 20. Following table shows run steps for searching x=20.

More Related