Algorithm & Problem Solving PDF

Summary

This document is a lecture from Salahaddin University - Erbil on Algorithm & Problem Solving. It covers concepts such as course description, organization, objectives, grading system, and syllabus.

Full Transcript

Algorithm & Problem Solving Algorithms & Problem Solving Chapter One INTRODUCTION Mohammed Y...

Algorithm & Problem Solving Algorithms & Problem Solving Chapter One INTRODUCTION Mohammed Y. Taha, MSc. Dept. of Electrical Engineering College of Engineering Salahaddin University-Erbil [email protected] ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 1 Algorithm & Problem Solving COURSE DESCRIPTION An Introduction to algorithmic problem solving by computers will be covered in this course. This course is intended for students with no background in computer programming or algorithms, so no knowledge of how computers are programmed is assumed. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 2 Algorithm & Problem Solving COURSE ORGANIZATION This module is a lecture theory course in which topics are presented by the instructor theoretically, assignments linked to the lectures are explained, then they are completed by students at home. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 3 Algorithm & Problem Solving COURSE OBJECTIVES To understand the types of problems and problem solving cycle. To understand problem solving methods by computer.  To describe proposed solutions by steps and algorithms. To be able to build flowcharts and pseudo codes to develop the instructions for each module in the solution of problems. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 4 Algorithm & Problem Solving GRADING SYSTEM Assignments and 20% two assignments and two quizzes quizzes during the semester. Continuous Exam 20 % Two Exams Final Exam 60 % ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 5 Algorithm & Problem Solving RESOURCES AND REFERENCES 1. Lecture notes are preprints of Microsoft PowerPoint slides used by the instructor during lectures. They are fairly detailed and comprehensive. 2. The text book: M. Sprankle and J. Hubbard, “Problem Solving and Programming Concepts”, Pearson, 2012. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 6 COURSE SYLLUBUS 1. General Problem Solving Concepts Problem solving in everyday life, types of problems, problem solving with computers. 2. Beginning Problem Solving Concepts for the Computer Constants and variables, data types, how the computer stores data, functions, operators, expressions and equation. 3. Solution Planning Communicating with the computer, organizing the solution, Unified Modeling Language, testing the solution. 4. Programming Structure Pointers for structuring a solution, the modules and their functions, cohesion and coupling, local and global variables, parameters, return values, variable names and data dictionary. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 7 COURSE SYLLUBUS cont. 5. Problem solving with the sequential logic structure Algorithm instructions, flowcharts, pseudo codes, the sequential logic structure, solution development. 6. Problem solving with decisions The decision logic structure, multiple if/then/else instructions, using straight- through logic, positive and negative logic. 7. Problem solving with loops The loop logic structure, incrementing, accumulating, while/while end, nested loops, indicators. 8. Data Structure Arrays, table look-up technique, table-relation structure. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 8 COURSE SYLLUBUS cont. 9. Problem Solving documentation and putting all together Comments, notes , error handling, breaks and packaging for the coding phase. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 9 Algorithms & Problem Solving Chapter Two General Problem Solving Concepts Mohammed Y. Taha, MSc. Dept. of Electrical Engineering College of Engineering Salahaddin University-Erbil [email protected] ©2023 , IBTEHAL H, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 10 What is a problem? A problem is a situation or puzzle that requires logical thought or mathematics to solve it. Examples: How to win the first place award in the college football league? Where to go for this weekend? How to sort this list of names? How to determine the square root of 4? How to find current in an electrical circuit? ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 11 What is problem solving?  The process of working through details of a problem to reach a solution.  Problem solving may include mathematical or systematic operations.  Problem solving consists of using generic or ad hoc methods in an orderly manner to find solutions to problems. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 12 Problem solving in every day life Six problem solving steps Identify Identify the Understand the alternative problem problem solutions List the Evaluate the Select the instruction to solution solution solve the problem ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 13 The six problem solving steps 1. Identify the problem If you do not know what the problem is, you can not solve it. Example: Pass an exam, finding a job, solving a chess game, solving a bus line stack, going to school in the morning, sorting a list of names, searching for a name in a list. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 14 The six problem solving steps 2. Understand the problem This step includes understanding the knowledge base of the machine and person for whom you will solve the problem. What are the limitations and information level available. You need to use a lot of instructions to tell someone how to find a restaurant if he is not from the city. What are the ability of the computer if you use it in the solution. Example: To solve a calculus problem you must know calculus, to play a chess you must know how to play the chess. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 15 The six problem solving steps 3. Identify alternative ways to solve the problem Identify all available solutions. Example: You could go from Erbil to Istanbul by air way, but this probably not be an acceptable solution to your travel need. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 16 The six problem solving steps 4. Select the best ways to solve the problem You need to evaluate the pros and cons of each possible solution before selecting the best one. In order to do this, you need to select criteria for the evaluation, for example cost, time, safety, … etc. Example: Is the final needed outcome guaranteed? Does it meet the deadline? ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 17 The six problem solving steps 5. List instructions that enable you to solve the problem using the selected solution Step by step instruction must fall within the knowledge base of the individual or machine. No instruction can be used unless the individual or the machine can understand it. This can be very limiting when working with computers. Example: Use MS-Excel to sort a list of names, how? Open Excel, enter the list, use the function sort, organize the result, But I do not now how to use Excel!!! ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 18 The six problem solving steps 6. Evaluate the Solution To evaluate or test a solution means to check its results to see if it is correct, and to see if it satisfies the needs of the person with the problem. Example: If a person needs a piece of furniture to sleep on, buying him a cot may be a correct answer but it may not be satisfactory. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 19 Homework Problem: What to do this weekend? Step 1: Identify the problem, how do the individuals wish to spend the weekend? Step 2: Understand the problem, the knowledge base of the participants must be considered. Every one involved must know how to do. Step 3: Alternatives: Watch TV, Invite friends over, play video games, go to Sami Park, … etc. Note: The list is complete only when you can think of no more alternatives. Step 4: Select the best solution, think about cost, interests. Step 5: Prepare a list of instructions that will result in a fun weekend. Step 6: Are we having fun, if no, then review the plan, change, if it is not working, the process must start again. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 20 Types of Problems  Problems do not always have straightforward solutions. Some problems (example: backing a cake) can be solved with a series of actions. These solutions are called Algorithmic Solutions. Note: Once the best solution has been chosen among alternatives, the solution can be reached by completing the actions in steps. These steps are called the Algorithm. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 21 Types of Problems  The solution of some problems (example how to buy a car, how to speak English, how to throw a ball) are not so straightforward. These solutions require reasoning built on knowledge and experience, and a process of trail and error. Solutions that can not be reached through a direct set of steps are called Heuristic Solutions. Note: The problem solving can use the six steps for both types, however in step 6, the evaluation and the correctness of heuristic solutions are far less certain. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 22 Problem Solving with Computers  Computers are built to deal with Algorithmic solutions, which are difficult and/or very time consuming for humans.  People are better than computers at developing heuristic solutions.  The field of computers that deal with heuristic types of problems is called Artificial Intelligence (AI).AI is an expanding computer field, especially with the increased use of robotics. Example: alphabetizing 10,000 names is an easy task for computer, but the problem of how to throw a ball is not. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 23 Notes:  In computer problem solving, the term solution means the instruction listed during step 5 of problem solving.  The instructions must be followed to produce the best solution.  Result Means the outcome of the completed computer- assisted answer.  Program means the set of instructions that make up the solution after they have been coded into a particular computer language. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 24 Notes:  When solving problems on the computer, one of the most difficult tasks for the problem solver is writing the instructions. Example: Take the task of deciding which number is the largest from a group of three numbers. Almost anyone can immediately tell which is the largest, but many cannot explain the steps they followed to arrive at it.  The computer is a tool that will perform only tasks that the user can explain. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 25 Questions? ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 26 Algorithms & Problem Solving Chapter Three Problem Solving Concepts for the Computer Mohammed Y. Taha, MSc. Dept. of Electrical Engineering College of Engineering Salahaddin University-Erbil [email protected] ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 27 Algorithmic Problems: (Problems that can be solved on computers) 1. Computational problems involving mathematical processing (y=2, x=4+2y) 2. Logical problems: involving relational or logical processing used in decision making on the computer (if x>5 then y=10) 3. Repetitive problems: problems involving repeating a set of mathematical and/or logical instructions (i=0, i=i+1 repeat for 100 times) ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 28 Constants and Variables A programmer takes the data (un-organized facts) and information (organized facts) relevant to a problem and defines them as constants or variables. Constant is a value (alphabet or numeric, or any data type) that never changes during the processes of a solution. Variable or (Identifier) is a value (alphabet or numeric, or any data type) that may change during processing. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 29 Constants and Variables Example: Sneakers’ price It is a variable since its value may change. It should be given a name. NB: the Variable name will not change but it’s value may change Sneakers Price 50.00 35.00 10.00 ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 30 Constants and Variables Example: In a solution that calculates the pay roll (Salary) for a company, the name of the company would be a constant since it does not change. The employee name, the hours, the rate of pay would be variable because the value of these items change for each employee. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 31 Examples: ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 32 Rules to name variables Each company you work for will have naming conventions for variables. The convention for naming variables may differ with companies as well as languages. It is important to have all programmers within an environment follow the specified convention (method), Because If several programmers work on the same project, they will not have conflict problems for variable names. It allow programs to be easily read because there is only one consistent name for a variable. Allow the code to be easily maintained. Rules: 1. Name a variable according to what it represents, that is, Hours for hours worked, Payrate for rate of pay, and so on. 2. Create as short a name as possible but one that clearly represents the variable. 3. Do not use spaces in a variable name; for example, use Hours Worked. 4. Start a variable name with a letter. 5. Do not use a dash (or any other symbol that is used as a mathematical operator) in a variable name. The computer will recognize these symbols as mathematical operators ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 33 Rules to name variables 6. After you have introduced a variable name that represents a specific data item, this exact variable name must be used in all places where the data item is used. 7. Be consistent when using upper- and lower-case characters. In some languages HOURS is a different variable name than Hours. 8. Use the naming convention specified by the company where you work. Variable Names Constant Names PayRate PI Rate KCONSTANT HoursWorked MULTIPLIER Amount Temperature ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 34 Incorrect Variable Names ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 35 Data Types To process solutions, the computer must have data. Data are unorganized facts. They go into the computer as input and are processed by the program. What is returned to the user is output, or information. This information is printed in the form of reports. when a computer calculates the balance of a checkbook, data are the checks, the deposits, and the bank charges. The information from the processing is shown on the balance sheet. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 36 Data Types The data that computer uses are of many different types. Computers must be told the data type of each variable or constant. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 37 Data Types Examples of Data Types ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 38 Functions:  Are small sets of instructions that perform specific tasks and return values (output or result of function)  Because they are basic tasks that are used repeatedly in the problem solving process, by using them a programmer or user can shorten the problem solving time and improve the readability of the solution.  Each language has a set of functions within it. Most languages allow programmers to write their own functions.  Libraries of functions can be added to many languages. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 39 Functions Syntax The name of the function followed by an open parenthesis, followed by the data (parameters) needed to perform the function and concluded by a closed parenthesis. FunctionName(data) Example: Sqrt(n) Sqrt(4) Returns 2 ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 40 Functions Function houseprice (area,beds) price=area*200+beds*5000 return price -------------------------------------------------------- House1=houseprice(200, 3) Result:55000 ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 41 Types of Functions 1. Mathematical Functions Often used in science and business, mathematical functions calculate such things as square root, absolute value ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 42 Types of Functions 2. String Functions These are used to manipulate string variables. For example find the length or the number of characters in the string. *Definitions of symbols: N is a numeric value—a constant, a variable, or an expression S is a string value—a constant, a variable, or an expression n, n1, n2 are integer values—a constant, a variable, or an expression ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 43 Types of Functions 3. Conversion Functions These functions are used to convert data from one data type to another. 4. Statistical Functions These functions are used to calculate things such as maximum values, minimum values, and so forth. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 44 Types of Functions 5. Utility Functions Examples of these include date and time functions. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 45 Operators  The computer has to be told how to process data. This task Is accomplished through the use of operators.  Operators are the data connectors, within expressions and equations.  Operands are the data that the operator connects and processes.  The resultant is the answer that results when the operation is completed. Example 5+7=12 + is the operator 5 and 7 are the operands 12 is the resultant ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 46 Mathematical Operators Include addition, subtraction, multiplication, division, integer division, modulo division, powers, and functions. The computer has a symbol for each of them as shown below ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 47 Relational Operators Include the following: equal to, less than, greater than, less than or equal to or etc.… The operands of a relational operator can be either numeric or character (a string) A programmer uses relational operators to program decisions. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 48 Logical Operators They are used to connect relational expressions (decision-making expressions) and to perform operations on logical data. Definitions of the Logical Operators ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 49 Definitions of the Logical Operators ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 50 Hierarchy of Operations Order of Operations (The order in which operations take place). When a problem involves multiple operations, do the steps in the following order: 1. Parentheses ( ) – Perform the operations. 2. Exponents – Simplify expressions or perform any operations involving exponents. 3. Multiplication and Division – Do multiplication and division from left to right. 4. Add and Subtract – Do addition and subtraction from left to right. Please Excuse My Dear Aunt Sally ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 51 Hierarchy of Operations ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 52 Example: ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 53 Quiz A/ B/ D/ C/ ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 54 Expressions and Equations An expression processes data, the operands, through the use of operators. An equation stores the resultant of an expression in a memory location in the computer through the equal (=) sign. length *width (expression) Area=length*width (equation) ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 55 Expressions and Equations Equations are often called assignment statements because the variable on the left hand side of the equal sign is assigned the value of the expression on the right hand side ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 56 Setting Up a Numeric Expression and Equation for computer Example 1/ N= Example 2/ ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 57 Evaluating a mathematical expression Evaluate for X=2, Y=3, Z=6 ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 58 Evaluating an Equation That Uses Both Relational and Logical Operators Example Evaluate F=NOT(A R c. NOT C d. B AND F ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 60 Questions? ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 61 Algorithms & Problem Solving Chapter Four Planning Solutions Mohammed Y. Taha, MSc. Dept. of Electrical Engineering College of Engineering Salahaddin University-Erbil [email protected] ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 62 Communication with the computer Computers are only as good as their: 1. hardware 2. their software 3. the people using them Note:  The computer must be told what to do.  It needs a set of instructions in order to process data in a correct sequence and reach the desired results. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 63  An instruction has essentially the same meaning in any computer language or application.  The difference is in the way they are set up.  Syntax refers to the rules governing the computer operating system, the language, and the application.  An error is called a bug.  Debugging is the process of finding and correcting a bug. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 64 Computer based Problem solving tools  Problem analysis chart : shows a beginning analysis of the problem  Structure chart (interactivity chart) : shows the overall layout or structure of the solution.  IPO chart : which shows the input, the processing, and the output.  The Algorithms : which show the sequence of instructions comprising the solution.  The flowcharts :which are graphic representations of the algorithms.  Pseudocode: which represents a language like solution. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 65 Organizing the Solution To analyze a problem and set up an efficient solution: a programmer organizes the solution by using all or some of these tools. When these tools are not used during the problem-solving process: The solution takes longer to program. The final program is less efficient, lacks readability, and increases programmer frustration. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 66 Analyzing the problem A good way to analyze a problem is to separate it into four parts as mentioned in Problem Analysis Chart (PAC): 1. The given data 2. The required results 3. The processing that is required in the problem 4. A list of solution alternatives ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 67 Problem Analysis Chart Given Data Required Results Data given in the problem Requirements for the or provided by the user. output reports. Processing Required Solution Alternatives List of processes required. List of ideas for the This includes equations or solution of the problem other type of processing. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 68 Example: Calculate the gross pay of an employee given the hours worked and the rate of p[ay. The gross pay is calculated by multiplying the hours worked by the rate of pay. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 69 Solution using PAC  The hours worked and pay rate are the data given and are put into the Given Data box.  The gross pay is what needs to be calculated and given to the user, therefore it is put into the Required Results box.  The formula used is GrossPay=Hours*PayRate Therefore it is put into the Processing Required box.  The solution alternatives are as follows: 1. Define the hours worked and pay rate as constants. 2. Define the hours worked and pay rate as input values. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 70 Problem Analysis Chart (PAC) Given Data Required Results Hours Gross Pay Pay Rate Processing Required Solution Alternatives GrossPay=Hours*PayRate 1. Define the hours worked and pay rate as constants 2. Define the hours worked and pay rate as input values.* ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 71 Writing The Algorithm After analyzing the problem, the next step in organizing a solution is for the programmer to develop sets of instructions for the computer , called Algorithm. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 72 Flowchart  A flowchart is a type of diagram that represents an algorithm, workflow or process.  The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows.  This diagrammatic representation illustrates a solution model to a given problem. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 73 Flowchart symbols Flowchart Symbol Explanation Indicate the start and the end of a module (Start/Stop) Processing block, for such things as calculations, opening and closing files. (Process) Indicates input to and output from the computer memory. (I/O) Indicates a decision. It has one entrance and two and only two exits from the block. One exit For True, the other for False. On-Page Connectors*, connects sections on the same page Off-Page Connectors*, connects flowcharts from page to page. Flowlines are indicated by straight lines with optional arrows to show the direction of data flow. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 74 Example The flowchart to calculate the average of three numbers. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 75 Example Finding ODD and EVEN numbers Begin Input A If A/2 gives a reminder Then print “A is ODD” Else print “A is EVEN” END If End ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 76 Pointers for Structuring a Solution 1. Use modules: break the whole into parts, with each part having a particular function. 2. Use the three logic structures to ensure that the solution flows smoothly from one instruction to the next, rather than jumping from one point in the solution to another. a. The sequential structure executes instructions one after another in a sequence. b. The decision structure branches to execute one of two possible sets of instructions. c. The loop structure executes a set of instructions many times. See 3. Eliminate the rewriting of identical processes by using modules. 4. Use techniques to improve readability, including the four logic structures, proper naming of variables, internal documentation, and proper indentation. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 77 Sequential logic structure ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 78 Decision logic structure ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 79 Loop logic structure ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 80 Local and Global Variables Local variables may be used only by the module itself. Other modules have no knowledge of these variables. All modules have knowledge of global variables. They are global to the program because they can be seen by all modules. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 81 Algorithm Instructions, Flowchart Symbols, and Pseudocode An algorithm is a set of instructions telling the computer how to process a module in a solution. A flowchart is a visual illustration of an algorithm. You will draw a flowchart to accompany each algorithm. ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 82 Example for Sequential logic Write an algorithm to find area of a rectangle FLOWCHART: ALGORITHM: Step 1: Start Step 2: get l,w values Step 3: Calculate A=l*w Step 4: Display A Step 5: Stop PSEUDOCODE: BEGIN READ l,b CALCULATE A=l*b DISPLAY A END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 83 Sequential and Decision logic Example Write an algorithm To check greatest of two numbers Step 1: Start Step 2: get a,b value Step 3: check if(a>b) print “a is greater” Step 4: else print “b is greater” Step 5: Stop BEGIN READ a,b IF (a>b) THEN DISPLAY a is greater ELSE DISPLAY b is greater END IF END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 84 To check leap year or not Step 1: Start Start Step 2: get y Step 3: if(y%4==0) print “leap year” Step 4: else print “not leap year” Get y Step 5: Stop BEGIN READ y Yes Is No IF (y%4==0) THEN Y%4==0 DISPLAY leap year ELSE Print Print DISPLAY not leap “leap year” “not leap year” year END IF END Stop ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 85 To check positive or negative number Step 1: Start Step 2: get n Step 3: check if(n>0) print “positive” Step 4: else “negative” Step 5: Stop BEGIN READ num IF (num>0) THEN DISPLAY num is positive ELSE DISPLAY num is negative END IF END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 86 To check odd or even number Step 1: Start Step 2: get num Step 3: check if(num%2==0) print num is even Step 4: else num is odd Step 5: Stop BEGIN READ num IF (num%2==0) THEN DISPLAY num is even ELSE DISPLAY num is odd END IF END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 87 To check greatest of three numbers BEGIN Step1: Start READ a, b, c Step2: Get A, B, C IF (a>b) THEN Step3: if(A>B) goto Step4 else goto step5 IF(a>c) THEN Step4: If(A>C) print A else print C DISPLAY a is greater Step5: If(B>C) print B else print C ELSE Step6: Stop DISPLAY c is greater END IF ELSE IF(b>c) THEN DISPLAY b is greater ELSE DISPLAY c is greater END IF END IF END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 88 Write an algorithm to check whether given number is +ve, -ve or zero. Step 1: Start Step 2: Get n value. Step 3: if (n ==0) print “Given number is Zero” Else goto step4 Step 4: if (n > 0) then Print “Given number is +ve” Step 5: else Print “Given number is -ve” Step 6: Stop BEGIN GET n IF(n==0) THEN DISPLAY “ n is zero” ELSE IF(n>0) THEN DISPLAY “n is positive” ELSE DISPLAY “n is positive” END IF END IF END ©2021 , MOHAMMED Y. TAHA, SALAHADDIN UNIVERSITY-ERBIL, COLLEGE OF ENGINEERING, DEPARTMENT OF ELECTRICAL ENG., WWW.SU.EDU.KRD 89 Write an algorithm to print n odd numbers Step 1: start step 2: get n value step 3: set initial value i=1 step 4: check if(i

Use Quizgecko on...
Browser
Browser