Chapter-3-Algortihm-Pseudocode-Flowchart (2).pdf
Document Details
![RewardingJungle](https://quizgecko.com/images/avatars/avatar-5.webp)
Uploaded by RewardingJungle
Negros Occidental High School
Full Transcript
Chapter 3 Algorithm, Pseudocodes & Flowcharts Presented By: MARK JARUS T. TALANQUINES Assistant Professor II TABLE OF CONTENTS NO: LIST OF TOPICS Topic 1: Algorithm Topic 2: What is the need for algorithms? Topic 3: Types of Algorithms Topic 4:...
Chapter 3 Algorithm, Pseudocodes & Flowcharts Presented By: MARK JARUS T. TALANQUINES Assistant Professor II TABLE OF CONTENTS NO: LIST OF TOPICS Topic 1: Algorithm Topic 2: What is the need for algorithms? Topic 3: Types of Algorithms Topic 4: Pseudocode Topic 5: Flowcharting Topic 6: General Rules for Flowcharting Topic 7: Logical Control Structures Topic 1 What is Algorithms What is Algorithm | Introduction to Algorithms The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or other problem-solving operations ” Or ” A procedure for solving a mathematical problem in a finite number of steps that frequently involves recursive operations”. Therefore Algorithm refers to a sequence of finite steps to solve a particular problem. Use of the Algorithms: Algorithms play a crucial role in various fields and have many applications. Some of the key areas where algorithms are used include: 1. Computer Science: Algorithms form the basis of computer programming and are used to solve problems ranging from simple sorting and searching to complex tasks such as artificial intelligence and machine learning. 2. Mathematics: Algorithms are used to solve mathematical problems, such as finding the optimal solution to a system of linear equations or finding the shortest path in a graph. 3. Operations Research: Algorithms are used to optimize and make decisions in fields such as transportation, logistics, and resource allocation. Use of the Algorithms: 4. Artificial Intelligence: Algorithms are the foundation of artificial intelligence and machine learning, and are used to develop intelligent systems that can perform tasks such as image recognition, natural language processing, and decision-making. 5. Data Science: Algorithms are used to analyze, process, and extract insights from large amounts of data in fields such as marketing, finance, and healthcare. These are just a few examples of the many applications of algorithms. The use of algorithms is continually expanding as new technologies and fields emerge, making it a vital component of modern society. Use of the Algorithms: Algorithms can be simple and complex depending on what you want to achieve. It can be understood by taking the example of cooking a new recipe. To cook a new recipe, one reads the instructions and steps and executes them one by one, in the given sequence. The result thus obtained is the new dish is cooked perfectly. Every time you use your phone, computer, laptop, or calculator you are using Algorithms. Similarly, algorithms help to do a task in programming to get the expected output. The Algorithm designed are language-independent, i.e. they are just plain instructions that can be implemented in any language, and yet the output will be the same, as expected. What is the need for algorithms? 1. Algorithms are necessary for solving complex problems efficiently and effectively. 2. They help to automate processes and make them more reliable, faster, and easier to perform. 3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to do manually. 4. They are used in various fields such as mathematics, computer science, engineering, finance, and many others to optimize processes, analyze data, make predictions, and provide solutions to problems. What are the Characteristics of an Algorithm? What are the Characteristics of an Algorithm? As one would not follow any written instructions to cook the recipe, but only the standard one. Similarly, not all written instructions for programming are an algorithm. For some instructions to be an algorithm, it must have the following characteristics: Clear and Unambiguous: The algorithm should be unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning. Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined inputs. It may or may not take input. For some instructions to be an algorithm, it must have the following characteristics: Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it should be well-defined as well. It should produce at least 1 output. Finite-ness: The algorithm must be finite, i.e. it should terminate after a finite time. Feasible: The algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology or anything. Language Independent: The Algorithm designed must be language-independent, i.e. it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected. For some instructions to be an algorithm, it must have the following characteristics: Input: An algorithm has zero or more inputs. Each that contains a fundamental operator must accept zero or more inputs. Output: An algorithm produces at least one output. Every instruction that contains a fundamental operator must accept zero or more inputs. Definiteness: All instructions in an algorithm must be unambiguous, precise, and easy to interpret. By referring to any of the instructions in an algorithm one can clearly understand what is to be done. Every fundamental operator in instruction must be defined without any ambiguity. Properties of Algorithm: It should terminate after a finite time. It should produce at least one output. It should take zero or more input. It should be deterministic means giving the same output for the same input case. Every step in the algorithm must be effective i.e. every step should do some work. Types of Algorithms: There are several types of algorithms available. Some important algorithms are: 1. Searching algorithms are essential tools in computer science used to locate specific items within a collection of data. These algorithms are designed to efficiently navigate through data structures to find the desired information, making them fundamental in various applications such as databases, web search engines, and more. What is Searching? o Searching is the fundamental process of locating a specific element or item within a collection of data. Applications of Searching: Searching algorithms have numerous applications across various fields. Here are some common applications: o Information Retrieval: Search engines like Google, Bing, and Yahoo use sophisticated searching algorithms to retrieve relevant information from vast amounts of data on the web. o Database Systems: Searching is fundamental in database systems for retrieving specific data records based on user queries, improving efficiency in data retrieval. o E-commerce: Searching is crucial in e-commerce platforms for users to find products quickly based on their preferences, specifications, or keywords. o Networking: In networking, searching algorithms are used for routing packets efficiently through networks, finding optimal paths, and managing network resources. o Artificial Intelligence: Searching algorithms play a vital role in AI applications, such as problem-solving, game playing (e.g., chess), and decision-making processes o Pattern Recognition: Searching algorithms are used in pattern matching tasks, such as image recognition, speech recognition, and handwriting recognition. Types of Algorithms: There are several types of algorithms available. Some important algorithms are: 2. Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. For Example: The below list of characters is sorted in increasing order of their ASCII values. That is, the character with a lesser ASCII value will be placed first than the character with a higher ASCII value. Applications of Sorting: Data management: Sorting data makes it easier to search, retrieve, and analyze. For example the order by operation in SQL queries requires sorting. Database optimization: Sorting data in databases improves query performance. We preprocess the data by sorting so that efficient searching can be applied. Machine learning: Sorting is used to prepare data for training machine learning models. Data Analysis: Sorting helps in identifying patterns, trends, and outliers in datasets. It plays a vital role in statistical analysis, financial modeling, and other data-driven fields. Operating Systems: Sorting algorithms are used in operating systems for tasks like task scheduling, memory management, and file system organization. Advantages of Algorithms: It is easy to understand. An algorithm is a step-wise representation of a solution to a given problem. In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program. Disadvantages of Algorithms: Writing an algorithm takes a long time so it is time-consuming. Understanding complex logic through algorithms can be very difficult. Branching and Looping statements are difficult to show in Algorithms. How to Design an Algorithm? To write an algorithm, the following things are needed as a pre- requisite: 1. The problem that is to be solved by this algorithm i.e. clear problem definition. 2. The constraints of the problem must be considered while solving the problem. 3. The input to be taken to solve the problem. 4. The output is to be expected when the problem is solved. 5. The solution to this problem is within the given constraints. Then the algorithm is written with the help of the above parameters such that it solves the problem. Example: Consider the example to add three numbers and print the sum. Step 1: Fulfilling the pre-requisites As discussed above, to write an algorithm, its prerequisites must be fulfilled. 1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum. 2. The constraints of the problem that must be considered while solving the problem: The numbers must contain only digits and no other characters. 3. The input to be taken to solve the problem: The three numbers to be added. 4. The output to be expected when the problem is solved: The sum of the three numbers taken as the input i.e. a single integer value. 5. The solution to this problem, in the given constraints: The solution consists of adding the 3 numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other method. Example: Consider the example to add three numbers and print the sum. Step 2: Designing the algorithm Now let’s design the algorithm with the help of the above pre-requisites: Algorithm to add 3 numbers and print their sum: 1. START 2. Declare 3 integer variables num1, num2, and num3. 3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3 respectively. 4. Declare an integer variable sum to store the resultant sum of the 3 numbers. 5. Add the 3 numbers and store the result in the variable sum. 6. Print the value of the variable sum 7. END Example: Consider the example to add three numbers and print the sum. Step 3: Testing the algorithm by implementing it. To test the algorithm, let’s implement it in C language. How to analyze an Algorithm? 1. Priori Analysis: “Priori” means “before”. Hence Priori analysis means checking the algorithm before its implementation. In this, the algorithm is checked when it is written in the form of theoretical steps. This Efficiency of an algorithm is measured by assuming that all other factors, for example, processor speed, are constant and have no effect on the implementation. This is done usually by the algorithm designer. This analysis is independent of the type of hardware and language of the compiler. It gives the approximate answers for the complexity of the program. How to analyze an Algorithm? 2. Posterior Analysis: “Posterior” means “after”. Hence Posterior analysis means checking the algorithm after its implementation. In this, the algorithm is checked by implementing it in any programming language and executing it. This analysis helps to get the actual and real analysis report about correctness(for every possible input/s if it shows/returns correct output or not), space required, time consumed, etc. That is, it is dependent on the language of the compiler and the type of hardware used. How to express an Algorithm? 1. Natural Language: Here we express the Algorithm in the natural English language. It is too hard to understand the algorithm from it. 2. Flowchart: Here we express the Algorithm by making a graphical/pictorial representation of it. It is easier to understand than Natural Language. 3. Pseudo Code: Here we express the Algorithm in the form of annotations and informative text written in plain English which is very much similar to the real code but as it has no syntax like any of the programming languages, it can’t be compiled or interpreted by the computer. It is the best way to express an algorithm because it can be understood by even a layman with some school- level knowledge. Topic 2 Pseudocodes Pseudocode? Pseudocode is a term which is often used in programming and algorithm based fields. It is a methodology that allows the programmer to represent the implementation of an algorithm. Pseudo code, as the name suggests, is a false code or a representation of code which can be understood by even a layman with some school level programming knowledge. Algorithms are expressed using natural verbal but somewhat technical annotations. Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and informative text written in plain English. It has no syntax like any of the programming language and thus can’t be compiled or interpreted by the computer. Advantages of Pseudocode Improves the readability of any approach. It’s one of the best approaches to start implementation of an algorithm. Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough documentation, so the program of one developer can be understood easily when a pseudo code is written out. In industries, the approach of documentation is essential. And that’s where a pseudo-code proves vital. The main goal of a pseudo code is to explain what exactly each line of a program should do, hence making the code construction phase easier for the programmer. How to write a Pseudocode? 1. Arrange the sequence of tasks and write the pseudocode accordingly. 2. Start with the statement of a pseudo code which establishes the main goal or the aim. Example: How to write a Pseudocode The way the if-else, for, while loops are indented in a program, indent the statements likewise, as it helps to comprehend the decision control and execution mechanism. They also improve the readability to a great extent. How to write a Pseudocode 1. Use appropriate naming conventions. The human tendency follows the approach to follow what we see. If a programmer goes through a pseudo code, his approach will be the same as per it, so the naming must be simple and distinct. 2. Use appropriate sentence casings, such as CamelCase for methods, upper case for constants and lower case for variables. 3. Elaborate everything which is going to happen in the actual code. Don’t make the pseudo code abstract. 4. Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way we use it in programming. 5. Check whether all the sections of a pseudo code is complete, finite and clear to understand and comprehend. 6. Don’t write the pseudo code in a complete programmatic manner. It is necessary to be simple to understand even for a layman or client, hence don’t incorporate too many technical terms. Topic 3 Flowcharting What is a Flowchart and its Types? Flowcharts are nothing but the graphical representation of the data or the algorithm for a better understanding of the code visually. It displays step-by-step solutions to a problem, algorithm, or process. It is a pictorial way of representing steps that are preferred by most beginner-level programmers to understand algorithms of computer science, thus it contributes to troubleshooting the issues in the algorithm. To draw a flowchart, certain rules need to be followed which are followed by all professionals to draw a flowchart and are widely accepted all over the countries. Flowchart symbols Different types of boxes are used to make flowcharts flowchart Symbols. All the different kinds of boxes are connected by arrow lines. Arrow lines are used to display the flow of control. Let’s learn about each box in detail. There are 6 basic symbols commonly used in flowcharting of assembly language programs: Terminal, Process, input/output, Decision, Connector and Predefined Process. This is not a complete list of all the possible flowcharting symbols, it is the ones used most often in the structure of Assembly language programming. Uses of Flowcharts in Computer Programming/Algorithms The following are the uses of a flowchart: 1. It is a pictorial representation of an algorithm that increases the readability of the program. 2. Complex programs can be drawn in a simple way using a flowchart. 3. It helps team members get an insight into the process and use this knowledge to collect data, detect problems, develop software, etc. 4. A flowchart is a basic step for designing a new process or adding extra features. 5. Communication with other people becomes easy by drawing flowcharts and sharing them. Advantages & Disadvantages Advantages of Flowchart Disadvantages of Flowchart 1. It is the most efficient way of 1. Flowcharts are challenging communicating the logic of the system. to draw for large and 2. It acts as a guide for a complex programs. blueprint during the program 2. It does not contain the design. proper amount of details. 3. It also helps in the debugging 3. Flowcharts are very difficult process. to reproduce. 4. Using flowcharts we can easily analyze the programs. 4. Flowcharts are very difficult 5. flowcharts are good for to modify. documentation. General Rules for Flowcharting 1. All boxes of the flowchart are connected with Arrows. (Not lines) 2. Flowchart symbols have an entry point on the top of the symbol with no other entry points. The exit point for all flowchart symbols is on the bottom except for the Decision symbol. 3. The Decision symbol has two exit points; these can be on the sides or the bottom and one side. 4. Generally a flowchart will flow from top to bottom. However, an upward flow can be shown as long as it does not exceed 3 symbols. 5. Connectors are used to connect breaks in the flowchart. Examples are: From one page to another page. From the bottom of the page to the top of the same page. An upward flow of more then 3 symbols 6. Subroutines and Interrupt programs have their own and independent flowcharts. 7. All flow charts start with a Terminal or Predefined Process (for interrupt programs or subroutines) symbol. 8. All flowcharts end with a terminal or a contentious loop. The Logical Control Structures Control Structures - are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It basically analyzes and chooses in which direction a program flows based on certain parameters or conditions. There are three basic types of logic, or flow of control, known as: 1. Sequence logic, or sequential flow 2. Selection logic, or conditional flow 3. Iteration logic, or repetitive flow The Logical Control Structures (Con..) Sequential Logic (Sequential Flow) - as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. The sequences may be given, by means of numbered steps explicitly. Also, implicitly follows the order in which modules are written. The Logical Control Structures (Con..) Selection Logic (Conditional Flow) - simply involves a number of conditions or parameters which decides one out of several written modules. The structures which use these type of logic are known as Conditional Structures. In this way, the flow of the program depends on the set of conditions that are written. This can be more understood by the following flow charts: The Logical Control Structures (Con..) Iteration Logic (Repetitive Flow) - The Iteration logic employs a loop which involves a repeat statement followed by a module known as the body of a loop. In this, there requires a statement that initializes the condition controlling the loop, and there must also be a statement inside the module that will change this condition leading to the end of the loop. REFERENCES 1. Ritchi, D. M., & Kernighan, B. W. (2021). C programming language (2nd ed.). 2. Gookin, D. (2020). C programming for dummies. John Wiley & Sons. 3. McGrath, M. (2018). C programming in easy steps: Updated for the GNU compiler version 6.3.0 and Windows 10 (5th ed.). In Easy Steps. REFERENCES 4. C programming language tutorial. (2024, June 10). GeeksforGeeks. https://www.geeksforgeeks.org/c- programming-language/?ref=shm 5. W3Schools.com. (n.d.). W3Schools Online Web Tutorials. https://www.w3schools.com/c/index.php 6. C tutorial. (n.d.). Quality Tutorials, Video Courses, and eBooks.https://www.tutorialspoint.com/cprogramming/index. htm The End of Chapter! Email: [email protected] Website: Pending..