Algorithm(lesson).pdf
Document Details
Uploaded by BrightChalcedony6470
Davao Oriental State University
Tags
Related
- GE8151- PROBLEM SOLVING AND PYTHON PROGRAMMING - Question Bank PDF
- Computer Science Problem Solving Concepts (2022) PDF
- Computer Science Algorithms and Problem Solving 2015 PDF
- Introduction to Programming and Problem Solving (CS101) Lecture 1 PDF
- CIT 108 Problem Solving Strategies (PDF)
- Problem Solving Final Notes
Full Transcript
Algorithm, Flowcharts & Pseudocode (and NS) Dony Dongiapon Learning Objectives: Develop problem-solving skills. Create flowcharts and pseudocode to represent algorithms. Interpret flowcharts and pseudocode (translate to code). Problem Solving Process/Strategies 1. Ask questions....
Algorithm, Flowcharts & Pseudocode (and NS) Dony Dongiapon Learning Objectives: Develop problem-solving skills. Create flowcharts and pseudocode to represent algorithms. Interpret flowcharts and pseudocode (translate to code). Problem Solving Process/Strategies 1. Ask questions. You ask when, why, where until the task is completely specified. 2. Look for things that are familiar (Pattern Recognition). Never reinvent the wheel. If a solution exists, use it. 3. Divide and conquer. Break up large problems into smaller units that we can handle. 4. Solve by analogy. Some problems may remind you of similar problems you met before. They may be of different areas but can be solved in the same way. 5. The building-block approach. Attacking large problems by looking for existing solutions for smaller pieces of the problem. A combination of familiar – things and the divide-and-conquer approach. 6. Means-ends analysis. Large problems can be subdivided into a smaller series of beginning state and desired ending state. The overall strategy of this approach is to pin down what the ends are and then analyze what means you have of getting between them. “In the world of programming, problem-solving is the cornerstone of success. Whether you're a seasoned coder or just starting your journey, the ability to tackle challenges effectively is a skill that will set you apart; finding solutions to issues that arise in the development process. In this blog, we'll dive into the importance of problem-solving in programming and provide some simple tips to help you hone this invaluable skill.” - bolton.ac.uk WHY PROBLEM-SOLVING MATTERS IN PROGRAMMING? In the realm of computer Finding Bugs and Errors: Programming isn't all smooth sailing. programming, problem- You'll encounter bugs and errors, but with strong problem- solving isn't an option – solving skills, you can track them down and fix them; like a detective game where you follow the clues to the culprit – the it's the heart and soul of bug what we do. Without it, Creating Efficient Code: Efficient code is the golden ticket in we'd be lost in a programming. Problem-solving helps you optimise your code, making it faster and less resource hungry. It's all about finding labyrinth of errors and better ways to achieve the same results bugs. Understanding Complex Problems: Complex problems are part and parcel of programming. Problem-solving allows you to break down these big problems into smaller, manageable chunks. By tackling them one step at a time, you can conquer even the most intricate challenges TIPS FOR DEVELOPING PROBLEM- SOLVING SKILLS IN PROGRAMMING 1. Understand the Problem Before you start typing lines of code, make sure you understand the problem inside out. Read and re-read the problem statement. What's the input and what's the expected output? 2. Pseudocode: Your Best Friend Pseudocode is like a rough draft of your code. It's a way to outline your solution in plain, human language before you even start coding. This step will help you structure your thoughts and prevent you from getting lost in a maze of code. TIPS FOR DEVELOPING PROBLEM- SOLVING SKILLS IN PROGRAMMING 3. Break It Down Remember, big problems are just a collection of smaller problems. Break the problem into smaller, solvable pieces. Tackle each part individually and then assemble them to solve the larger issue. 4. Debugging: The Art of Eliminating Bugs When you're deep into coding, it's common to encounter bugs. Debugging is the process of finding and fixing these issues. Patience is the key, as you methodically examine your code, testing and eliminating problems. TIPS FOR DEVELOPING PROBLEM- SOLVING SKILLS IN PROGRAMMING 5. Practice, Practice, Practice The more you practice problem-solving in programming, the better you become. Challenge yourself with coding puzzles and exercises. There are many websites and platforms dedicated to providing coding challenges. Embrace them and you'll see a remarkable improvement in your skills. 6. Collaborate and Learn Don't be afraid to seek help from fellow programmers; collaboration and learning from others can boost your problem- solving abilities. Sometimes, a fresh perspective is all you need to crack a tough nut. TIPS FOR DEVELOPING PROBLEM- SOLVING SKILLS IN PROGRAMMING 7. Learn from Your Mistakes In the world of problem-solving, mistakes are your best teachers. When you encounter a challenge, try to solve it independently first. If you stumble, seek solutions and understand where you went wrong. Learning from your mistakes is a shortcut to improvement. 8. Stay Curious Keep your mind open to new techniques, languages and tools. The more you explore, the broader your problem-solving toolkit becomes. Algorithms: Flowcharts and Pseudocode Algorithms: Flowcharts and Pseudocode Definition: 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 a specific class of problems for any allowable set of input quantities (if there are inputs). In other words, an algorithm is a step-by-step procedure to solve a given problem Algorithms: Flowcharts and Pseudocode 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 phonetic pronunciation of the last name of Abu Ja'far Mohammed ibn Musa al-Khowarizmi, who was an Arabic mathematician who invented a set of rules for performing the four basic arithmetic operations (addition, subtraction, multiplication and division) on decimal numbers. Algorithms: Flowcharts and Pseudocode Characteristics: – An algorithm must have start instruction – Each instruction must be precise. – Each instruction must be unambiguous. : the operations described are understood by a computing agent without further simplification – Each instruction must be executed in finite time. – An algorithm must have stop instruction. - Effectively computable: the computing agent can actually carry out the operation Algorithms: Flowcharts and Pseudocode Characteristics: – An algorithm must have start instruction – Each instruction must be precise. – Each instruction must be unambiguous. : the operations described are understood by a computing agent without further simplification – Each instruction must be executed in finite time. – An algorithm must have stop instruction. - Effectively computable: the computing agent can actually carry out the operation Method for Developing an Algorithm Input-Process-Output Model Method for Developing an Algorithm 1. Define the problem: State the problem you are trying to solve in clear and concise terms. 2. List the inputs (information needed to solve the problem) and the outputs (what the algorithm will produce as a result) 3. Describe the steps needed to convert or manipulate the inputs to produce the outputs. Start at a high level first, and keep refining the steps until they are effectively computable operations. 4. Test the algorithm: choose data sets and verify that your algorithm works! The Böhm–Jacopini theorem, states the three specific ways (control structures). These are 1. Sequence. One statement is executed after another 2. Selection. statements can executed or skipped depending on whether a condition evaluates to TRUE or FALSE 3. Iteration. statements can be executed repeatedly until a condition evaluates to TRUE or FALSE *The theorem is typically credited to a 1966 paper by Corrado Böhm and Giuseppe Jacopini. Selection Structure There are three selection structures in Java PL and C: 1. if 2. if - else 3. switch Repetition Structure There are three repetition structures in Java PL and C: 1. while 2. do…while 3. for Pseudocode (or Program Design Language) Consists of natural language-like statements that precisely describe the steps of an algorithm or program Statements describe actions Focuses on the logic of the algorithm or program Avoids language-specific elements Written at a level so that the desired programming code can be generated almost automatically from each statement Steps are numbered. Subordinate numbers and/or indentation are used for dependent statements in selection and repetition structures. 2 Pseudocode Keywords 1. //: This keyword is used to represent a comment. 2. BEGIN,END: Begin is the first statement and end is the last statement. 3. INPUT, GET, READ: The keyword is used to input data. 4. COMPUTE, CALCULATE: used for calculation of the result of the given expression. Pseudocode Keywords 5. ADD, SUBTRACT, INITIALIZE used for addition, subtraction, and initialization. 6. OUTPUT, PRINT, DISPLAY: It is used to display the output of the program. 7. IF, ELSE, ENDIF: used to make a decision. 8. WHILE, ENDWHILE: used for iterative statements. 9. FOR, ENDFOR: Another iterative incremented/decremented tested automatically. Flowcharts A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program. Flowcharts General rules for flowcharts: - All symbols of the flowchart are connected by flow lines (note arrows, not lines) - Flowlines enter the top of the symbol and exit out the bottom, except for the Decision symbol, which can have flow lines exiting from the bottom or the sides - Flowcharts are drawn so flow generally goes from top to bottom - The beginning and the end of the flowchart are indicated using the Terminal symbol Algorithm Constructs: Computation/Assignment Pseudocode Flowcharts 1. Compute var1 as the sum of x and y 2. Assign 10 to var2 3. Increment counter1 COMPUTE var1 as the sum of x and y ASSIGN 10 to var2 INCREMENT counter1 Algorithm Constructs: Input/Output Pseudocode Flowcharts Input: Get divident, divisor Output: Display divident, divisor Algorithms Constructs: Selection Flowcharts Pseudocode Single-Selection IF 1. IF condition THEN 1.1 Statement 1 1.2 Statement 2 Algorithms Constructs: Selection Pseudocode Flowcharts Double-Selection IF 1. IF condition THEN 1.1 Statement 1 1.2 Statement 2 2. ELSE 2.1 statement 2 2.2 statement 3 ENDIF Algorithms Constructs: Selection Pseudocode Flowcharts Double-Selection IF 1. SWITCH expression TO 1.1 case 1: action1 1.2 case 2: action2 1.3 etc. 1.4 default: actionx Algorithms Constructs: Repetition Pseudocode Flowcharts WHILE 1.WHILE condition 1.1 Statement 1 1.2 Statement 2 Algorithms Constructs: Repetition Pseudocode Flowcharts DO-WHILE 6. DO 6.1 statement 1 6.2 statement 2 7. WHILE condition Tests condition at the end of the loop. Thus, statements in the structure will always be executed at least once. Algorithms Constructs: Repetition Pseudocode Flowcharts FOR structure 6. FOR bounds on repetition 6.1 statement 1 6.2 etc. A specialized version of WHILE for repeating execution of statements a specific number of times (Pseudocode) Examples Express an algorithm to get two numbers from the user (dividend and divisor), testing to make sure that the divisor number is not zero, and displaying their quotient using pseudocode BEGIN 1. Declare variables: dividend, divisor, quotient 2. Prompt user to enter dividend and divisor 3. Get dividend and divisor 4. IF divisor is equal to zero, THEN 4.1. DO 4.1.1. Display error message, “divisor must be non-zero” 4.1.2. Prompt user to enter divisor 4.1.3. Get divisor 4.2. WHILE divisor is equal to zero 5. ENDIF 6. Display dividend and divisor 7. Calculate quotient as dividend/divisor 8. Display quotient END (Flowchart) Examples Express an algorithm to get two numbers from the user (dividend and divisor), testing to make sure that the divisor number is not zero, and displaying their quotient using flowchart. Some more examples (Q & A) The End… References https://www.bolton.ac.uk/blogs/the-art-of-problem-solving-in-computer-programming