cse 102 introduction to computer engineering what is an algorithm?

31
CSE 102 Introduction to Computer Engineering What is an Algorithm?

Upload: luke-wilcox

Post on 02-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSE 102 Introduction to Computer Engineering What is an Algorithm?

CSE 102Introduction to Computer Engineering

What is an Algorithm?

Page 2: CSE 102 Introduction to Computer Engineering What is an Algorithm?

What is an algorithm?

An algorithm is a well-defined procedure that allows the computer to solve a problem.

Page 3: CSE 102 Introduction to Computer Engineering What is an Algorithm?

What is an algorithm?

Example algorithms:• Cooking a dish • Making a peanut-butter jelly sandwich• Shampooing hair• Programming a VCR • Making a pie

Page 4: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Is this an algorithm?

• Step 1: Wet hair• Step 2: Lather• Step 3: Rinse• Step 4: Repeat

Would you manage to wash your hair with this algorithm? How about a robot? Why (not)?

Page 5: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Algorithms

An algorithm must:• be well-ordered and unambiguous,• each operation must be effectively computable,• terminate

Page 6: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Problem: Find and print the 100th prime number– A prime number is a whole number not evenly divisible by any

other number other than 1 and itself

Algorithm (?):1. Generate a list of all prime numbers n1, n2, n3, …2. Sort the list in ascending order3. Print out the 100th element in this list

Is this an algorithm?

Page 7: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Algorithms

An algorithm consists of:• the actions to be executed, and• the order in which the actions are to be executed

Page 8: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Problem: Find the sum of the first 5 numbers in a list.

Actions: • Set the initial value of SUM to 0• Take the first number from the list• Add the number to SUM• Take the next number from the list

Algorithm:1. Set the initial value of SUM to 02. Take the first number from the list3. Add the number to SUM4. Take the next number from the list5. Repeat steps 3-4 five times

Page 9: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Pseudocode

Pseudocode is an informal language that helps programmers develop algorithms.

Page 10: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Problem: Find the sum of the first 5 numbers in a list.

Pseudocode:1. SUM ← 02. N ← first number in the list3. SUM ← SUM + N4. N ← next number in the list5. Repeat steps 3-4 five times

Page 11: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Flowchart

Flowchart is a graphical representation of an algorithm.

Page 12: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Problem: Find the sum of the first 5 numbers in a list.

SUM ← 0

N ← first number in the list

SUM ← SUM + N

N ← next number in the list

five times

exitTrue

False

start

Page 13: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Flowchart

Flowchart symbols: • Sequence structure• Selection structure• Repetition structure

Page 14: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Flowchart

Sequence structure

SUM ← 0

Page 15: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Flowchart

Selection structure

N>50TrueFalse

Page 16: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Flowchart

Repetition structure

repeated action

conditionTrue

False

Page 17: CSE 102 Introduction to Computer Engineering What is an Algorithm?

ExampleProblem: Read the grades of 20 students and print “passed” if the grade is

greater than or equal to 50, otherwise print “failed”.

COUNT ← 0

Read grade

COUNT=20

exitTrue

False

grade >= 50TrueFalse Print “passed”Print “failed”

COUNT ← COUNT+1

Page 18: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Variables

Variable– A named memory location that can store a value– Think of it as a box into which you can store a value, and from

which you can retrieve a value

Examples:

Example of operations• Set the value of i to 3• Set the value of M to i*3 + 12• Set the value of i to i+10

i M

Page 19: CSE 102 Introduction to Computer Engineering What is an Algorithm?

A model for visualizing an algorithm

Algorithm

Variables

Operations

An algorithm consists of operations that involve variables

Page 20: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Primitive operations

• Get input from user– Get the value of x from user

• Assign values to variables using basic arithmetic operations

– Set the value of x to 3– Set the value of y to x/10– Set the value of z to x +25

• Print output to user– Print the value of y, z to the user

Page 21: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example

Problem: For any three numbers input by the user, compute their sum and average, and output them

Example of algorithm in pseudocode:

Variables: a, b, c, sum, avg1. Get the values of a, b, c from user2. Set avg to (a+b+c)/3 3. Set sum to (a+b+c)4. Print sum and avg

Page 22: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Example 2

Problem: Given any value of radius from the user, compute and print the circumference of a circle with that radius

Algorithm in pseudocode:

variables: r, c1. Get the value of r from user2. Set c to 2 * pi * r3. Print “The circumference of your circle is “ c

Page 23: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Basic operations

– Read the input from user• Get x • Get a, b, c

– Print the output to the user• Print x• Print “Your mileage is ” x

– Cary out basic arithmetical computations• Set x to 10• Set y to x*x/3

Page 24: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Conditional statementsSpecify a statement that may or may not be done:

if <condition> then<statement to be done>

else <statement to be done otherwise>

Exampleif the value of A is greater than 5 then set the value of B to 1else set the value of B to 0

Page 25: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Loop statements

specify a group of statements that may be done several times (repeated):

repeat until <condition>< statements to be repeated >

• How does this work? – Condition is evaluated– If it is true than the loop terminates and the next instruction to be

executed will be the instruction immediately following the loop– If it is false, then the algorithm executes the <statements to be

repeated> in order, one by one

Page 26: CSE 102 Introduction to Computer Engineering What is an Algorithm?

ExampleVariables: countStep 1: set count to 1Step 2: repeat step 3 to step 5 until count is > 10

Step 3: set square to count *count Step 4: print value of square and value of countStep 5: add 1 to count

Step 6: end

• What does this algorithm do? • Note: indentation

– Not necessary, but makes reading/understanding algorithms easier

Page 27: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Pseudocode examplesEquivalent:

– Set the value of a to 1– Set a to 1– a=1– a←1

Equivalent:– Add 1 to count– Set count to count + 1– Increment the value of count by 1– count = count + 1– count ← count + 1

Writing in pseudocode gives you the freedom to choose any of these!

Page 28: CSE 102 Introduction to Computer Engineering What is an Algorithm?

• Incorrect– Set 1 to a– Add a + b (what do you do with the result?)– Set a+b +3

• Note: not the same– set a to b – set b to a

• Example: what is the output of the following algorithms?set a to 2 set a to 2set b to 4 set b to 4set a to b set b to aprint a, b print a, b

Page 29: CSE 102 Introduction to Computer Engineering What is an Algorithm?

A model for visualizing an algorithm’s behavior

Algorithm

Computer

Input (keyboard)

Output (screen)

Variables

Page 30: CSE 102 Introduction to Computer Engineering What is an Algorithm?

Designing Algorithms: A Methodology

1. Read the problem, identifying the input and the output.

2. What variables are needed?3. What computations are required to achieve the

output?4. Usually, the first steps in your algorithm bring

input values to the variables.5. Usually, the last steps display the output6. So, the middle steps will do the computation.7. If the process is to be repeated, add loops.

Page 31: CSE 102 Introduction to Computer Engineering What is an Algorithm?

More algorithms• Write algorithms to find

– the largest number in a list of numbers (and the position where it occurs)

– the smallest number in a list of numbers (and the position where it occurs)

– the range of a list of numbers • Range= largest - smallest

– the average of a list of numbers

– the sum of a list of numbers