ALGORITHMS 1 INTRODUCTION Algorithmic Representation of Computer Functions
- Slides: 43
ALGORITHMS 1. INTRODUCTION
Algorithmic Representation of Computer Functions v Input – Get information Get (input command) v Storage – Store information Given/Result Intermediates/Set v Process – Arithmetic – Repeat instructions – Branch conditionals Let (assignment command) Loop If v Output – Give information Give (output command)
Algorithm Description v Understand the problem before solving it – – Identify & name each Input/Givens Identify & name each Output/Results Assign a name to our algorithm (Name) Combine the previous 3 pieces of information into a formal statement (Definition) • Results = Name (Givens)
Method v We develop a series of instructions, when executed, will compute the desired Results from the Givens (Method) (Results) ﻧﺤﺴﺐ ﺍﻟﻨﺘﺎﺋﺞ ، ﻭﻋﻨﺪﻣﺎ ﻳﺘﻢ ﺗﻨﻔﻴﺬﻫﺎ ، ﻧﺒﻨﻲ ﺳﻠﺴﻠﺔ ﻣﻦ ﺍﻟﺘﻌﻠﻴﻤﺎﺕ v (Method) Givens ﺍﻟﻤﺮﺟﻮﺓ ﻭﻓﻖ ﺍﻟﻤﺪﺧﻼﺕ
Assignment command ( )ﺗﺨﺼﻴﺺ Syntax X = 5 Y + 16 On the left side of =, we put the name of a variable and on the right side we put a value or an expression. Each variable refers to a unique location in computer memory that contains a value. Interpretation An assignement is executed in two steps : 1 - evaluation of the expression found on the right side. 2 - setting the returned value in the memory cell corresponding to variable. Example Let Side. Size=15 Let Area=Side. Size
Assignment in computre science and equality in mathematics a) The following instructions are the same in mathematics. A=B B=A not in computer science. Let A=B is different from Let B=A b) In mathematics we work with relations. A relation B=A+1 means that it is true all the time In computer science, we work with assignments. We can have: A=5 B=A+1 A=2 The relation B=A+1 is true only after the second instruction and before third one.
Assignment in computer science and equality in mathematics c) The instruction A=A+3 is false in mathematics. In computer science Let A=A+3 means: the new value of A is equal to the old one plus three. d) The instruction A+5=3 is allowed in mathematics (it is an equation). Let A+5=3 has no meaning in computer science (the left side must be a variable).
Input command Syntax Get variable The variable must be from Givens Interpretation Here the user must give a value. The given value is assigned to the variable. Example Get Size_Side
Output command Syntax Give variable The variable must be from Results Interpretation The value of the variable is displayed. Example Give Area
Algorithm 1. 1 v Write an algorithm to find the sum of three given numbers – – – NAME: SUM 3 GIVENS: N 1, N 2, N 3 RESULTS: Total DEFINITION: Total : = SUM 3(N 1, N 2, N 3) ------------METHOD: Get N 1 Get N 2 Get N 3 Let Total = N 1 + N 2 + N 3 Give Total
Algorithm 1. 2 v Write an algorithm to find the result of a division operation for the given two numbers X and Y – – – NAME: Division GIVENS: X, Y RESULTS: Quotient DEFINITION: Quotient : = Division(X, Y) ------------METHOD: Get X Get Y Let Quotient = X/Y Give Quotient
Algorithm 1. 3 v Write an algorithm to find the sum and product of the two given numbers – – – NAME: Sum. Times GIVENS: Num 1, Num 2 RESULTS: Total, Product DEFINITION: Total & Product : = Sum. Times(Num 1, Num 2) ------------METHOD: Get Num 1 Get Num 2 Let Total = Num 1 + Num 2 Let Product = Num 1 * Num 2 Give Total Give Product
Algorithm 1. 4 v Find the sum and average of three given numbers – – – NAME: AVG 3 GIVENS: Num 1, Num 2, Num 3 RESULTS: Sum , Average DEFINITION: Sum & Average = AVG 3(Num 1, Num 2, Num 3) ------------METHOD: Get Num 1 Get Num 2 Get Num 3 Let Sum = Num 1 + Num 2 + Num 3 Let Average = Sum /3 Give Sum Give Average
Variables v Observe that we have used names for the data items in our Givens and Results – Num 1, Num 2, Num 3, Sum, Average in Algorithm 1. 4 v Each name refers to a unique location in computer memory (one or more adjacent bytes) that contains a value v Since that value can change as the instructions in our algorithm are executed, we call each data item a variable
Variables v In our algorithm, when we use a variable name, we are referring to the value stored in memory for that data item
Intermediates v Occasionally, in an algorithm, we need to have a variable (in addition to those representing Givens or Results) to store a value temporarily v These are intermediate variables and we identify them in the Algorithm Description as Intermediates
Algorithm 1. 5 v Given 3 assignment marks (out of 50, 20, 70), find the average (calculated as a mark out of 100) ( ﻭﻧﺮﻳﺪ ﺣﺴﺎﺏ ﺍﻟﻤﺘﻮﺳﻂ ﺍﻟﻤﺌﻮﻱ ﻟﻬﺎ 50, 20, 70 )ﺍﻟﺪﺭﺟﺎﺕ ﻣﻦ v General Concept – How does one figure out the percentage of several marks? • Add them all up • Divide by the maximum possible mark : (50+20+70) • Multiply by 100
v Note: v percentage average= (Sum of marks / Sum of maximum possible marks) * 100 v Example sub Algorithms programming english mark 50 45 70 mark out of 80 53 94 v percentage average= (50 + 45 + 70)/(80 + 53 + 94) × 100 = 72. 6 v The 1% of marks= 0. 01× (80+53+94)=2. 27
Algorithm 1. 5 v Given 3 assignment marks (out of 50, 20, 70), find the percentage average, calculated as a mark out of 100 – – – – NAME: Calc. Mark GIVENS: A 1, A 2, A 3 RESULTS: Mark INTERMEDIATES: Total, Max. Mark (Constant) DEFINITION: Mark : = Calc. Mark(A 1, A 2, A 3) ------------METHOD: Set Max. Mark = 140 (Constant) Get A 1 Get A 2 Get A 3 Let Total = A 1 + A 2 + A 3 Let Mark = Total/Max. Mark * 100 Give Mark
Algorithm 1. 6 v Given a two digit number, find the sum of its digits v General Concept 41 10 = 4 – How can we break apart a number? • 41 = 4 Tens and 1 Ones • so for the number 41, we want 4 + 1 = 5 – Use integer division • DIV returns the integer part of a division • MOD returns the remainder of a division 41 MOD 10 = 1
Algorithm 1. 6 v Given a two digit number, find the sum of its digits – – – – NAME: Sum. Dig GIVENS: N RESULTS: Sum INTERMEDIATES: Tens, Ones DEFINITION: Sum : = Sum. Dig(N) ------------METHOD: Get N Let Tens = N div 10 Let Ones = N mod 10 Let Sum = Tens + Ones Give Sum
Algorithm 1. 7 v Write an algorithm which adds the given two numbers (X and Y) and returns the sum in the given variable X – – – – NAME: Add. XY GIVENS: X, Y RESULTS: X INTERMEDIATES: None DEFINITION: Add. XY (X, Y) ------------METHOD: Get X Get Y Let X = X+ Y Give X
Recapitulation Name The formal name of the algorithm Givens Names of the given values for a problem Results Names of the resulting values of the problem Intermediates Names of the intermediate variables used in the algorithm Definition The formal definition of the algorithm, using Name, Givens, and Results Method The step by step sequence of statements to solve the problem
Tracing an Algorithm( )ﺗﺘﺒﻊ ﺧﻮﺍﺭﺯﻣﻴﺔ v The purpose of tracing an algorithm is to ensure that it works v This is a paper test. It should be completed before writing the computer code v Tracing involves – Executing the sequence of instructions with a sample set of values – Computing the value of each variable after each instruction is executed – Checking for the correct result
Tracing an Algorithm v Step 1 - Number every instruction in the algorithm v Step 2 – Make a table – The first column of the table indicates which instruction has been executed – Subsequent columns list all the variables of the algorithm (Givens, Results, Intermediates)
Tracing an Algorithm v Step 3 – Complete the table – Each column of the table represents a variable – Start at the first line of your algorithm. Identify what will happen to each variable as that instruction is executed • Change any values which the instruction changes and leave all other columns blank
Trace 1. 1 v Trace Algorithm 1. 4 using the numbers 24, 31, and 35 METHOD: (1) Get Num 1 (2) Get Num 2 (3) Get Num 3 Line Num 1 1 24 2 Num 2 Sum Avg 31 3 (4) Let Sum = Num 1 + Num 2 + Num 3 4 (5) Let Average = Sum /3 5 (6) Give Sum (7) Give Average Num 3 6 output 90 7 output 30 35 90 30
Trace 1. 2 v Trace Algorithm 1. 5 with the numbers 40, 18, 26 METHOD: (1) Set Max. Mark =140 (2) Get A 1 (3) Get A 2 (4) Get A 3 Ln A 1 A 2 A 3 1 2 40 3 18 4 26 (5) Let Total = A 1 + A 2 + A 3 5 (6) Let Mark = Total/Max. Mark * 100 6 7 output 60 (7) Give Mark MM Ttl 140 Mark 84 60 NB THE ANSWER IS NOT 69 (40/50 + 18/20 + 26/70)/3
Flow Charts
Flow Charts v Can be created in MS Visio v Begin and End with an Oval v Get/Give use a parallelogram v Lets use a rectangle v Flow is shown with arrows
Algorithm 1. 1 Write an algorithm to find the sum of three given numbers NAME: SUM 3 GIVENS: N 1, N 2, N 3 RESULTS: Total DEFINITION: Total : = SUM 3(N 1, N 2, N 3)
Algorithm 1. 2 Write an algorithm to find the result of a division operation for the given two numbers X and Y NAME: Division GIVENS: X, Y RESULTS: Quotient DEFINITION: Quotient : = Division(X, Y)
Algorithm 1. 3 Write an algorithm to find the sum and product of the two given numbers NAME: Sum. Times GIVENS: Num 1, Num 2 RESULTS: Total, Product DEFINITION: Total & Product : = Sum. Times(Num 1, Num 2)
Algorithm 1. 4 Find the sum and average of three given numbers NAME: AVG 3 GIVENS: Num 1, Num 2, Num 3 RESULTS: Sum , Average DEFINITION: Sum & Average : = AVG 3(Num 1, Num 2, Num 3)
Algorithm 1. 5 Given 3 assignment marks (out of 50, 20, 70), find the percentage average, calculated as a mark out of 100 NAME: Calc. Mark GIVENS: A 1, A 2, A 3 RESULTS: Mark INTERMEDIATES: Total, Max. Mark (Constant) DEFINITION: Mark : = Calc. Mark(A 1, A 2, A 3)
Algorithm 1. 6 Given a two digit number, find the sum of its digits NAME: Sum. Dig GIVENS: N RESULTS: Sum INTERMEDIATES: Tens, Ones DEFINITION: Sum : = Sum. Dig(N)
Algorithm 1. 7 NAME: Swap GIVENS: X, Y Change: X, Y RESULTS: None INTERMEDIATES: Temp DEFINITION: Swap (X, Y)
Algorithm 1. 8 Write an algorithm which adds the given two numbers (X and Y) and returns the sum in the given variable X NAME: Add. XY GIVENS: X, Y Change: X RESULTS: None INTERMEDIATES: None DEFINITION: Add. XY (X, Y)
Homework Due date for the homework: sunday / /2014
1. Write an algorithm to sum 5 numbers. v Trace the algorithm using the numbers 1, 3, 5, 7 and 9. 2. Write an algorithm to compute the average of 5 numbers. v Trace the algorithm using the numbers 1, 3, 5, 7 and 9. 3. Write an algorithm to multiply 3 numbers. v Trace the algorithm using the numbers 2, 9 and 6. 4. Write an algorithm to multiply 2 numbers and divide the product by a third number. v Trace the algorithm using the numbers 2, 9 and 6.
1. Write an algorithm to calculate the average mark out of 100, given three assignment marks, each of which is marked out of 20. v Trace the algorithm using 15, 20 and 10 as the three assignment marks. 2. Write an algorithm to calculate the sum of the digits of a three digit number. v Trace the algorithm using the number 659. 3. Write an algorithm to swap two numbers. v Trace the algorithm using x as 8 and y as 9. 4. Write an algorithm to sum four numbers and return the sum in the variable x. v Trace the algorithm using w as 2, x as 3, y as 4 and z as 5.
- Introduction to algorithmic trading strategies
- Introduction to algorithmic trading strategies
- Introduction to algorithmic trading strategies
- Kalman filter trading strategy
- Algorithmic trading singapore
- Mealy asm chart
- Computational thinking gcse
- Algorithmic nuggets in content delivery
- Algorithmic cost modelling
- Algorithmic graph theory and perfect graphs
- Notasi algoritma
- Input in algorithm
- Ireallytrade.com
- Algorithmic adc
- Event log correlation
- Asm chart examples
- Gdpr algorithmic bias
- Computer arithmetic: algorithms and hardware designs
- Computer networks routing algorithms
- Dda line drawing algorithm derivation
- Algorithm analysis examples
- An introduction to bioinformatics algorithms
- Introduction of design and analysis of algorithms
- Introduction to algorithms
- Introduction to algorithms 강의
- Introduction to algorithms 2nd edition
- 蔡欣穆
- Introduction to algorithms lecture notes
- Introduction to the design and analysis of algorithms
- Introduction to sorting algorithms
- Introduction to algorithms 2nd edition
- Introduction to algorithms 2nd edition
- Introduction to bioinformatics algorithms
- Bioalgorithms
- Standard representation of logic functions
- Data representation computer science
- Information representation
- Utah teapot vertex data
- Assembly language
- Information representation in computer
- Solid modeling in computer graphics
- Data representation and computer arithmetic
- Different types of data representation
- Data representation in computer organization