Lecture-1-Algorithm-Pseudo-code-and-Flowchart.pdf
Document Details
Uploaded by UnaffectedIdiom
Tags
Related
- Problem Solving Strategies and Computational Approaches PDF
- GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING - Question Bank PDF
- CPRO1 Introduction to Computer Programming PDF
- Lecture 1: Subject Introduction and Overview on Structured Programming Approach PDF
- Fundamentals of Programming - Algorithms and Flowcharts PDF
- Algorithms and Flowcharts Lecture Notes PDF
Full Transcript
ALGORITHMS, FLOWCHARTS, DATA TYPES AND PSEUDOCODE ALGORITHMS The term ALGORITHM originally referred to any computation performed via a set of rules applied to numbers written in decimal form. The word is derived from the of the last name of Abu Ja'far Mohammed ibn Musa al-Khowari...
ALGORITHMS, FLOWCHARTS, DATA TYPES AND PSEUDOCODE ALGORITHMS The term ALGORITHM originally referred to any computation performed via a set of rules applied to numbers written in decimal form. The word is derived from the of the last name of Abu Ja'far Mohammed ibn Musa al-Khowarizmi. An algorithm is a representation of a solution to a problem. An algorithm is a step-by-step process. ALGORITHMS THROUGHOUT HISTORY, MAN HAS THOUGHT OF EVER MORE ELEGANT WAYS OF REDUCING THE AMOUNT OF LABOR NEEDED TO DO THINGS. A computer has immense potential for saving time/energy, as most (computational) tasks that are repetitive or can be generalized can be done by a computer. Types Of Algorightms 1. Sorting Algorithms: These algorithms arrange a list of items in a specific order, such as numerical or lexicographical order. 2. Searching Algorithms: These algorithms look for a specific element within a collection of data. 3. Graph Algorithms: Graph algorithms deal with problems related to networks or interconnected structures. 4. Dynamic Programming Algorithms: These algorithms solve complex problems by breaking them down into smaller subproblems and reusing solutions to those subproblems. 5. Backtracking Algorithms: Backtracking algorithms explore different paths to find a solution and backtrack when a path leads to a dead end. An example of an algorithm people use.. Recipe to make a cake. Procedure to make a cake. 1. 1&1/2 C. stock 1. Mix all the ingredients, except the oil, in a deep bowl. 2. 1/2 teaspoon salt 2. Put 1" water in wide pot, then place deep bowl of batter inside. 3. 1 scallion, minced 3. Cover pot tightly and steam 15 min. 4. 1 C. small shrimp or lobster flakes 4. Heat oil very hot and pour over custard. 5. 1 t. soy sauce 5. Steam 5 more min. 6. 1 Tablespoon oil 6. Serves 4 people. This breaks down 'Making Chinese egg custard' into smaller steps. To make the product one still needs to know how to execute each of the steps in the procedure and understand all of the terms. PROCEDURE Is a finite sequence of well-defined instructions, each of which can be mechanically carried out in a finite amount of time. The procedure must break up the problem solution into parts that the recipient party can understand and execute. The procedure must consist of smaller steps each of which the computers understand. There may be no ambiguities in the translation of the procedure into the necessary action to be taken. ALGORITHMS An ALGORITHM is procedure consisting of a finite set of unambiguous rules (instructions) which specify a finite sequence of operations that provides the solution to a problem, or to a specific class of problems for any allowable set of input quantities (if there are inputs). In other word, an algorithm is a step-by- step procedure to solve a given problem. FLOWCHARTS Is a tool developed in the computer industry, for showing the steps involved in a process. A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows. Flowcharting combines symbols and flowlines, to show figuratively the operation of an algorithm. Process improvement starts with an understanding of the process, and flowcharting is the first step towards process understanding. Flowcharting Symbols 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. Flowcharting Symbols 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. General Rules for flowcharting 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. Flowchart Flowcharting uses symbols that have been in use for a number of years to represent the type of operations and/or processes being performed. The standardized format provides a common method for people to visualize problems together in the same manner. The use of standardized symbols makes the flow charts easier to interpret, however, standardizing symbols is not as important as the sequence of activities that make up the process. Flowcharting Tips 1. Keep it Simple: Flowcharts should convey information clearly and concisely. Avoid overly complex designs that can confuse the reader. 2. Use Standard Symbols: Stick to standard flowchart symbols for processes, decisions, start/end points, connectors, and more. This ensures your flowchart is easily understood by others. 3. Start with the Main Process: Begin your flowchart with the main process or task you're illustrating. Clearly label it to provide context. 4. Logical Flow: Ensure the flowchart follows a logical sequence. Use arrows to show the direction of flow from one step to another. 5. Use Consistent Sizing and Spacing: Keep the size and spacing of symbols consistent throughout the flowchart. This enhances readability. Flowcharting Tips 6. Label Everything: Clearly label each symbol and connector with a brief description of its purpose. This helps prevent confusion and ambiguity. 7. Keep Lines Neat: Use straight lines for connectors and arrows. Avoid unnecessary bends or curves that can make the flowchart messy. 8. Use White Space: Leave enough white space around symbols and connectors to prevent overcrowding and improve visual clarity. 9. Limit Decision Points: If possible, avoid too many decision points in a single flowchart. Complex decision trees can be difficult to follow. 10. Test for Clarity: After creating the flowchart, show it to someone unfamiliar with the process. If they can understand it without explanation, you're on the right track. Flowcharting Tips 11. Group Related Steps: If certain steps are related, consider grouping them together in a separate box or container to visually indicate their connection. 12. Use Color Sparingly: Color can be used to highlight certain elements, but avoid excessive use that could distract from the main content. 13. Review and Edit: Flowcharts can evolve as you refine your process. Review and edit your flowchart to make sure it accurately reflects the process you're describing. 14. Consider Software Tools: There are many software tools available for creating digital flowcharts, which can make the process easier to manage and update. 15. Include a Legend: If your flowchart uses symbols that might not be immediately clear to everyone, include a legend that explains what each symbol represents. Examples of Algorithms and Flowcharts Example 1: Design an algorithm and the corresponding flowchart for adding the test scores as given below: 26, 49, 98, 87, 62, 75 Example 1 Examples of Algorithms and Flowcharts Example 2: The problem with this algorithm is that, some of the steps appear more than once, i.e. step 5 get second number, step 7, get third number, etc. One could shorten the algorithm or flowchart as follows: Example 2 DATA TYPES In computer science and computer programming, a data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support common data types of real, integer and Boolean. Although some contemporary languages allow programmers to invent his own data types, and define their related operations. DATA TYPES Integer Real Numbers Fixed Point Floating Point Character Boolean Integer Integers are numeric data items, which are either POSITIVE or NEGATIVE including ZERO, i.e. 1, 488, -22, 0, 456. Real Numbers : Fixed Point Fixed point data items are numbers which have embedded decimal point i.e. 1.5, 458.4589, -0.569. Real Numbers : Floating Point Floating point data items are numbers, which are, held as binary fractions by a computer. The numbers are expressed in a form where you have a mantissa and an exponent, for example Floating point representation of data is used to overcome the restrictions placed on the magnitude of numbers by the size of computer’s memory locations. Character Character data, sometimes referred to as “STRING” data, may consist of any digits, letters of the alphabet or symbols which, the internal coding system of the computer is capable of representing. Many programming languages require character data to be enclosed by quotation marks when used in program instructions, for example PRINT “HAPPY NEW YEAR”. Boolean Boolean data items are used as status indicators and may contain only one of two possible values: True or False. DATA ITEM There are two basic methods of using data items in a program: a) Constants b) Variables and Variable names Constants Data items are sometimes required to keep their values throughout the program, hence the term constant. A constant is a specific value or character string used explicitly in an operation. Consider the constant values 47.5, 1, and 13 in the example below. Variables and Variable names A variable is a symbolic name assigned to a data item by the programmer. At any particular time, a variable will stand for one particular data, called the value of a variable, which may change from time to time during a computing process. The value of a variable may change many times during the execution of a program. A variable is usually given a name by the programmer. Assignment The assignment operation is used to assign a name to a value. Thus it is used whenever you need to keep track of a value that is needed later. Some typical uses include: The assignment operator is not commute i.e. x = e is not the same as e = x. The variable must be declared. Variables used in the expression must be defined (have values). The type of the expression must be compatible with the type of the variable. PSEUDOCODE Pseudocode is one of the tools that can be used to write a preliminary plan that can be developed into a computer program. Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax. It cannot be executed on a real computer, but it models and resembles real programming code, and is written at roughly the same level of detail. PSEUDOCODE Computer science textbooks often use pseudocode in their examples so that all programmers can understand them, even if they do not all know the same programming languages. Since pseudocode style varies from author to author, there is usually an accompanying introduction explaining the syntax used. PSEUDOCODE In the algorithm design, the steps of the algorithm are written in free English text and, although brevity is desired, they may be as long as needed to describe the particular operation. The steps of an algorithm are said to be written in pseudocode.