Introduction to Programming Algorithm Development with Flowchart Steps in Problem Solving Process (From our previous class) Understanding and analysis of the problem Development of algorithm i.e. devising a plan Coding and implementation Deployment and Maintenance Note: At each stage there should be testing and proper documentation. ALGORITHM DEVELOPMENT PHASE Simply put, an algorithm is a set of instructions that describe a method or a plan for solving a problem. An algorithm is a set of instructions for solving a problem or sub problem in a finite amount of time using a finite amount of data. These set of instructions must be unambiguous. This means we aim to make what is ambiguous i.e. implicit in human solution unambiguous i.e. explicit. Example of an ambiguous statement is: “stay away from the bank”, the bank have different meanings e.g. bank of river, or bank where money is kept, thus the statement “stay away from bank” is ambiguous and such instruction should be made explicit enough so that the computer can execute it. Algorithm Algorithm: a precisely specified procedure for solving a problem or a step-by-step method to solve a problem or complete a task Important Properties of Algorithms A good algorithm must be: Correct always returns the desired output for all legal instances of the problem. Unambiguous Precise Efficient Can be measured in terms of Time Space Time tends to be more important i.e. tradeoff Algorithm contd A good algorithm should have the following attributes: Definiteness; it should be well defined, that is every steps should be definite Finiteness; it should terminate after finite number of times Input; it may or may not have inputs Output; it must produce some result Efficiency ; an efficient algorithm should execute faster, and take less memory space CORRECTNESS OF ALGORITHM (start 02/06) Goal of solving a mathematical problem is to produce an answer to a problem, thus if the answer is correct then the process or steps are correct. On the contrary the goal of a computer problem solving is process correctness. This means if a process produces a correct result on a certain data set, other data set will be used on the same process in order to validate and test that the process is correct. Algorithm In writing algorithm we make use of symbols and action keywords. Algorithmic Symbols Symbol used in algorithm = Equal to # Not equal to > Greater than < Less than >= greater than or equal to <= Less than or equal to ↑ or ^ exponentiation Algorithmic Action words Common Action Keywords Several keywords are often used to indicate common input, output, and processing operations in algorithm Input: READ,OBTAIN, GET, INPUT Output: PRINT, DISPLAY, SHOW, PUT Compute: COMPUTE, CALCULATE, DETERMINE Initialise: SET, INIT Examples of Algorithms Algorithm 1: Add two numbers entered by the user Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop Examples of Algorithms Algorithm 2: Area of a rectangle Step 1: Start Step 2: Declare variables length, breadth and area. Step 3: Read values length and breadth. Step 4: Multiply length and breadth and assign the result to area. area← length*breadth Step 5: Display area Step 6: Stop Examples of Algorithms Algorithm 3: Find the largest number among two numbers Step 1: Start Step 2: Declare variables a and b. Step 3: Read variables a and b. Step 4: If a > b Display a is the largest number. Else Display b is the largest number. Step 5: Stop Lab I Write an algorithm to calculate the average of 3 integer numbers Write an algorithm to calculate simple interest using the expression SI= (SRT/100) Discussion Why do we study or write algorithm? Algorithm Contd. Aim of programming methodology is: To develop a good algorithm programming methodology must be followed. Programming Methodology Programming methodology is an organized, documented set of procedures and guidelines for one or more phases of the software life cycle. such as analysis or design. Programming methodology is about good software engineering principles. This are the principles to understand in order to develop a good software system. Thus we have programming methodology which deals with how an algorithm is developed. In this course we want to learn programming language in line with program methodologies that make you a good programmer. Types of Programming Methodology We have two types of programming methodologies, they are: 1. Top down design 2. Object oriented design (OOD) Top-down approach uses divide and conquer technique where the problem is broken down into sub problems, such that each sub problems or modules can be solved (i.e. conquered) independently of the other. This methodology is used by procedural languages like C. Each sub-problem forms a procedure. An object oriented design is used majorly by object oriented programming languages like Java, and C++. This breaks problems down into objects. PROGRAMMING METHODOLOGY Each of these methodologies can be expressed in any of these forms: Diagramming notation for documenting the results of the procedure (flowchart, UML design etc); step-by-step “cookbook” approach for carrying out the procedure (pseudo code); and an objective set of criteria for determining whether the results of the procedure are of acceptable quality e.g. computational cost, time and space complexity etc. Developing an algorithm As said earlier an algorithm can be represented as either a flowchart or a pseudo code. Flowchart Flowchart A flow chart is a pictorial representation of an algorithm in which symbols are used to show the various operations and decisions to be followed in solving a problem. In summary steps that must be followed. Flowchart Symbols Flowchart symbol Explanation Terminal symbol; indicate start/stop/End Parallelogram indicate input to and output From computer memory. An input/output block has one entrance and only one exit Rectangle indicate Processing and data manipulation: define the calculation require (e.g. computation of tax, gross pay, etc in above example) The diamond indicates a decision. It has one entrance and two and only two exits. One exit is the action when the resultants is True and the other exit is the action when the resultant is False Connector : this is used for the purpose of junction. Some rimes the flow chart can’t be completed on a single page and it require 2 or more pages, these connector symbols is used. flow lines ; used to connect the various symbols. It define the logic of the algorithm Rectangle with lines down each side indicates the process of modules. They have one entrance and only one exit. This is called predefined process.. Flowchart Start This flowchart shows the steps that may be taken in solving the lamp problem. End Flowchart Steps Start from the first stage of problem solving process Draw the start symbol Draw the input symbol and indicates if the inputs (this step is optional)--- input is based on the identified input in problem solving process Based on processing requirement you may need the following symbols: Processing symbol for computation Decision symbol for making decisions Remember to use the arrows to show direction of flow of the steps The output will be the expected result Indicate the end symbol and use the necessary arrows to show how each symbol moves to the end. Group Work Design a flowchart for the followings: admission process hall registration process student registration process daily activities of Babcock student Lab II Draw the flowchart for examples 1 and 2. The volume of a cylinder is given by V = π r2h Write the algorithm and draw the flowchart The area of a circle is given by the formula Area = πr2 write the algorithm and draw the flowchart. Lab II Assignment Following the problem solving process find the sum, product, difference, and quotient of any two numbers. Design the flowchart Self Assessment Why do we study programming methodology? What is the relationship between programming methodology and algorithm development? Assignment Draw a flowchart to authenticate student before having access to Babcock webpage. Draw a flowchart to add any two numbers Draw a flowchart to add any two numbers while each number must not be greater than 10. Draw a flowchart to detect whether a number is even or odd. QUIZ 18/9 What is an algorithm? Specify the type of data to use the followings algorithm keywords for INPUT INIT SET PSUEDOCODE pseudocode is the way of expressing a program or code so that it could be easily understood by programmers of every programming languages out there. Pseudocode is an informal high-level description of the operating principle of a computer program or an algorithm Following are the basic rules before writing pseudocode : Write only one statement per line. Write what you mean, not how to program it Give proper indentation to show hierarchy and make code understandable. Make the program as simple as possible. Conditions and loops must be specified well ie. begun and ended explicity WRITE A PSEUDOCODE TO FIND THE SUM OF TWO NUMBERS. begin numeric nNum1,nNum2,nSum display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 compute nSum=nNum1+nNum2 display "SUM OF THESE NUMBER : " nSum end BEGIN NUMBER s1, s2, sum OUTPUT("Input number1:") INPUT s1 OUTPUT("Input number2:") INPUT s2 sum=s1+s2 OUTPUT sum END WRITE A PSEUDOCODE TO FIND THE AREA OF RECTANGLE. begin numeric nLen,nBrd,nAre display "ENTER THE LENGTH OF RECTANGLE : " accept nLen display "ENTER THE BREADTH OF RECTANGLE : " accept nBrd nAre=nLen*nBrd display "AREA OF RECTANGLE : " nAre end BEGIN NUMBER len,brt,area,perimeter INPUT len INPUT brt area=len*brt perimeter=2*(len+brt) OUTPUT area OUTPUT perimeter END WRITE A PSEUDOCODE TO FIND THE GREATEST OF TWO NUMBERS. begin numeric nNum1, nNum2 display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 if(nNum1>nNum2) begin display "GREATEST ONE : " nNum1 end else begin display "GREATEST ONE : " nNum2 end end WRITE A PSEUDOCODE TO FIND THE GREATEST OF THREE NUMBERS. BEGIN NUMBER num1,num2,num3 INPUT num1 INPUT num2 INPUT num3 IF num1>num2 AND num1>num3 THEN OUTPUT num1+ "is higher" ELSE IF num2 > num3 THEN OUTPUT num2 + "is higher" ELSE OUTPUT num3+ "is higher" ENDIF END WRITE A PSEUDOCODE TO CHECK WHETHER THE ENTERED NUMBER IS EVEN OR ODD. begin numeric nNum display "ENTER A NUMBER : " accept nNum if(nNum%2==0) begin display "EVEN" end else begin display "ODD" end end WRITE A PSEUDOCODE TO CHECK EQUIVALENCE OF TWO NUMBERS. USE IF STATEMENT. begin numeric nNum1, nNum2 display "ENTER THE FIRST NUMBER : " accept nNum1 display "ENTER THE SECOND NUMBER : " accept nNum2 if(nNum1==nNum2) begin display "THESE ARE EQUAL" end else begin display "THESE ARE NOT EQUAL" end end