🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

T.Y.BSc Applied Component Algorithms &FlowCharts Definition: An Algorithm is a definite procedure for solving a problem using a finite number of steps. OR An algorithm is a step by step instruction which lead to the solution...

T.Y.BSc Applied Component Algorithms &FlowCharts Definition: An Algorithm is a definite procedure for solving a problem using a finite number of steps. OR An algorithm is a step by step instruction which lead to the solution of a problem. Problem: Calculate Sum of 2 nos and display it. Algorithm: 1. Start. 2. input a , b 3. c greater than  less than or equal to  greater than or equal to = equal to  not equal to 5. Logical operators: ‘OR’ , ‘AND’ and ‘NOT’ To write an algorithm we follow the following rules: 2 1.Each step will be numbered in the order in which it will be executed. Generally step numbers will be natural numbers in ascending order. The First step will be written as ‘Start’ which will mark the beginning of an algorithm. If required the name of the procedure (or algorithm) can be written in bracket. 2. The last step will be written as ‘End’ to mark the completion of an algorithm. 3. Input/Output Statements : We can use the words ‘Input’ or ‘Read’ to assign values to variables at the time of execution of the algorithm. Format: Input < list of variables separated by commas> OR Read < list of variables separated by commas> 4. Selection Statements : This is used when the execution of a particular set of statements is based on condition. I. if statement Format: if endif if the condition is ‘true’ then the statement(s) between ‘if’ and ‘endif’ is executed and then Statement(s) after ‘endif’ ( if it exists ) is executed and then execution continues with the next step. if the condition is ‘false’ then Statement(s) after ‘endif’ ( if it exists ) is executed and then execution continues with the next step. (d) Iterative statements or loops : These statements are used when a particular set of statements are to be performed (or executed) repeatedly. The statements repeated are called loops and the process is called looping. The loops can be performed in following ways : (i) while loop format : while < condition> do enddo 3 The statement(S) between ‘do’ and ‘enddo’ (ie. Loop) will be repeatedly executed as long as the condition remains true.The loop terminates when the condition becomes false. After the loop terminates the statement(s) after ‘enddo’ (if it exists) are executed and then the execution continues with the next step. The loops can be performed in following ways : (i) while loop format : while < condition> do enddo (ii) For loop format : For variable=initial-value to final-value do enddo This loop is used when we know the exact number of repetition (or iteration) to be performed. Sorting: is the process of arranging data in the array in either ascending order(smallest to largest) or descending order(largest to smallest). Sorting can be done by two methods (i) Simple sort(or Exchange sort) Method Simple sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The algorithm gets its name from the way smaller(or larger) elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. step-by-step example 4 Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. First Pass: (51428) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them. (15428) ( 1 5 4 2 8 ), No Swap since 1< 4 (1 5 4 2 8 ) ( 1 5 4 2 8), No Swap since 1 < 2 (1 5 4 2 8) ( 1 5 4 2 8), No Swap since 1 < 8 Second Pass: (1 5 4 2 8) ( 1 4 5 2 8 ) , Swap since 5 > 4 (1 4 5 2 8) ( 1 2 5 4 8 ), Swap since 4 > 2 (1 2 5 4 8 ) ( 1 2 5 4 8 ) No Swap since 2 < 8 Third Pass: (1 2 5 4 8 ) ( 1 2 4 5 8 ) , Swap since 5 > 4 (1 2 4 5 8 ) ( 1 2 4 5 8 ), No Swap since 4 < 8 Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Fourth Pass: (1 2 4 5 8 ) ( 1 2 4 5 8 ) , No Swap since 5 < 8 Finally, the array is sorted. Thus, we can generalize this result as: We require (n-1) Pass(or iterations) in which for ith Pass we require (n-i) comparisons to sort an array of n elements. Searching : is the process of finding the location of a target value(or search value) in the array. Linear search or sequential search is a method for finding a target value in an array that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found. In this method the search begins at the start of the array(i.e first element in the array) and then continues to search sequentially until you have either found the target value or the array gets exhausted. Flow Charts Definition 5 A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. Eg: Flow chart for printing square of no START INPUT X Y=X*X PRINT Y STOP Flowchart Symbols Different flow chart symbols have different meanings. The most common flow chart symbols are: Terminator: An oval shape indicating the start or end of the process. Process: A rectangular shape indicating a normal process flow step. Decision: A diamond shape indication a branch in the process flow. 6 Data: A parallelogram that indicates data input or output (I/O) for a process. Connector: In flowcharts, this symbol is typically small and is used as a Connector to show a jump from one point in the process flow to another. Connectors are usually labeled with capital letters (A, B) to show matching jump points. They are handy for avoiding flow lines that cross other shapes and flow lines. Off-Page Connector: Off-Page Connector shows continuation of a process flowchart onto another page. When using them in conjunction with Connectors, it's best to differentiate the labels, e.g. use numbers for Off-Page Connectors and capital letters for Connectors. In actual practice, most flowcharts just use the Connect shape for both on-page and off-page references. Rules for Drawing Flow Chart: To make sure that a flowchart works, you need to follow a few basic construction rules:  Each flowchart must have one and only one Start object.  The flow of control must always enter an object from the top.  The flow of control must always leave an object from the bottom (except for Decision objects, which allow the flow of control to leave from the side).  The flow of control must not split. You can use Decision objects to give the flow of control a choice of paths to follow, but it can only follow one of these paths.  The flow lines must not cross or overlap each other.  All the flow lines should converge into the stop object at the end of flow chart. 7

Use Quizgecko on...
Browser
Browser