Presentation is loading. Please wait.

Presentation is loading. Please wait.

5. Algorithm 1: Variables, Operators, Sequences 1.

Similar presentations


Presentation on theme: "5. Algorithm 1: Variables, Operators, Sequences 1."— Presentation transcript:

1 5. Algorithm 1: Variables, Operators, Sequences 1

2 Objectives What is an algorithm? How to represent an algorithm Components of an algorithm 2

3 Algorithm An algorithm is "a set of rules that precisely defines a sequence of operations” ( https://en.wikipedia.org/wiki/Algorithm#Informal _definition ) https://en.wikipedia.org/wiki/Algorithm#Informal _definition I define an algorithm as “a description of a solution to a problem step by step” The word “algorithm” is derived from the ninth-century Arab mathematician, Al-Khwarizmi who worked on “written processes to achieve some goal.” 3

4 Algorithms exist in our lives Recipe Manual Direction Schedule Study plan Travel plan (as a sequence of operations) 4

5 An Algorithm Structure A computer takes input raw data such as numbers, text, sound, image, animations, video, and etc., process it and converts it into meaningful information, presenting the changed input as output. All numbers, text, sound, images, animations, and video used as input are called data, and all numbers, text, sound, images, animations, and video returned as output are called information. Input Output Processing DataInformation http://digitalworldcentre.blogspot.kr/2015/06/introduction-to-computer-by-digital.html 5

6 Processing is the core part Data representation: data types, variables Operators: arithmetic, relational, logical operators Control structure: sequence, conditional, iteration Input Output Data types and variables Operators Control Structure Processing ProblemSolution 6

7 Algorithm Representation Natural Language: English prose Pseudocode: prose but much closer to the high-level programming language Flowchart: a graphical representation NS (Nassi-Schneiderman)-Chart: a graphical representation for structured programming 7

8 Natural Language For example, below is an algorithm to find the largest number in a list of numbers of random order. 1.If there are no numbers in the set then there is no highest number. 2.Assume the first number in the set is the largest number in the set. 3.For each remaining number in the set: if this number is larger than the current largest number, consider this number to be the largest number in the set. 4.When there are no numbers left in the set to iterate over, consider the current largest number to be the largest number of the set. https://en.wikipedia.org/wiki/Algorithm 8

9 Pseudocode For example, below is a pseudocode to find the largest number in a list of numbers of random order https://en.wikipedia.org/wiki/Algorithm 9

10 Flowchart a flowchart to find the largest number in a list of numbers of random order http://www.rff.com/find-largest-number.htm 10

11 NS (Nassi-Schneiderman) Chart An example of flowchart to find divisors of a given number K K = Input Number N = 1 Repeat for K times (K%N)==0 YN Print N N=N+1 11

12 In this class We will use natural language or pseudocode descriptions 12

13 So far, we learned that we can write an algorithm in English prose. Now, we will learn the building blocks of algorithm creation. variable and assignment operators input and output sequence control structure: if, while 13

14 Variables and Datatypes A variable is a name associated with a value and whose associated value A value can be a number or a text string 2016“Batman” yearname For instance, a variable “year" can contain the number 2016. a variable “name” can contain the text string “Batman”. 14

15 Variable and Assignment Unlike variable in mathematics, a variable in programming is often given long name to help human programmers comprehend its meaning A variable is very useful when we need to use it over and over again (Learn to Program, Chris Pine. https://pine.fm/LearnToProgram/chap_03.html ) Example student_name = ‘Eunjoo’ student_age = 20 x = x + 1 Let’s read this ‘Give x the value of x plus 1’ 15

16 Assignment Store a value in a variable using the symbol = IllustrationDescriptionPseudo code Assign 50 to aa = 50 Assign 20 to bb = 20 Assign the value of a+b to cc = a+b 16

17 Assignment is different from equal E.g., age = age + 1 In mathematics, age = age + 1 is wrong. But in computer science, the computer performs the following steps 20 age STEP 1. Get the value that is associated with the variable ‘age’ (which was 20) STEP 2. Perform the operation 20 + 1 20 + 1 STEP 3. Store 21 to age age 21 17

18 Operators An operator is a symbol that tells the computer language to perform specific mathematical or logical manipulations OperatorFunctionExampleDescription +additiona = b + cAssign the value of (b+c) to a -subtractiona = b - cAssign the value of (b-c) to a *multiplicationa = b * cAssign the value of (b*c) to a /divisiona = b / cAssign the value of (b/c) to a %remaindera = b % cAssign the value of (b%c) to a 18

19 Control Structure Sequences: decide the order of tasks or instructions Decisions: performs certain instructions depending on a condition Iterations: Repeat instructions until a condition is met 19

20 Sequences Decides the order of tasks or instructions The computer preforms one instruction at a time Sequencing is one of the ways that humans do their jobs E.g., Ramen recipe 1. put water 2. boil the water 3. put ramen 4. boil it 5. eat 20

21 Sequence Algorithm Example #1: to print the user’s name 1. Ask the user to input a name 2. Get the input and assign the value to a variable 3. Print the input 21

22 Sequence Algorithm Example #2: to print the double of a given value 1. Ask the user to input a value and assign it to a variable x 2. x = x * 2 3. Print x – Programmer: a person who solves problems by writing programs on a computer – User: any person who runs a program 22

23 Sequence Algorithm Example #3: to print the addition of two values input by the user 1. Ask the user to input a value and assign it to a variable x 2. Ask the user to input a value and assign it to a variable y 3. z = x + y 4. Print z 23

24 How it works with Python: From an algorithm to a program 24

25 The Python Language The Python Programming Language was created by Guido van Rossum. It was first released in the early 1990s. Its name comes from a 1970s British comedy sketch show called Monty Python’s Flying Circus (The Argument Clinic). Companies and organizations that use Python include YouTube, Google, Yahoo and NASA. 25

26 Python Features Simple Syntax Python programs are clear and easy to read Interpreted Language Python instructions can be executed interactively Powerful Programming Features Can accomplish significant computation with few instructions Numerous Python Modules Provide Additional Capabilities Capabilities that can be incorporated into a Python program (Dierbach, Introduction to Computer Science Using Python, John Wiley and Sons, 2013) 26

27 The IDLE Python Development Environment IDLE is an open source integrated development environment that contains a set of tools for program development (https://www.python.org/)https://www.python.org/ an editor: for writing a program a translator for executing programs a program debugger: for finding program errors 27

28 The Python Shell A tool that executes a Python code interactively >>> is a prompt 28

29 Sequence Algorithm Example #1: to print the user’s name 1. Ask the user to input a name and assign the value to a variable 2. Print the input name = input("Type your name") print(name) AlgorithmPython Program 29

30 The Input Function in Python Prompts the user to enter data User types response, presses ENTER key Entry assigned to variable on left © 2016 Pearson Education, Ltd. All rights reserved. 30

31 The Print function in Python Print function display values on the monitor The print function can display the result of evaluated expressions A single print function can display several values 31

32 Summary An algorithm is a sequence of instructions An algorithm can be described in prose or graphical representations Variables store values Operators are used to manipulate on values An algorithm can be implemented as a program using a computer language, e.g., Python 32

33 Exercise Assignment 33

34 Sequence Algorithm Example #2: to print the double of a given value 1. Ask the user to input a value and assign it to a variable x 2. x = x * 2 3. Print x Python Code 34

35 Sequence Algorithm Example #3: to print the addition of two values input by the user 1. Ask the user to input a value and assign it to a variable x 2. Ask the user to input a value and assign it to a variable y 3. z = x + y 4. Print z Python Code 35

36 Sequence Algorithm Example #4: print the distance traveled when the speed and time elapsed are given by the user 1. Ask the user to input the speed and assign it to a variable s 2. Ask the user to input the time elapsed and assign it to a variable t 3. distance = s * t 4. Print distance Python Code 36


Download ppt "5. Algorithm 1: Variables, Operators, Sequences 1."

Similar presentations


Ads by Google