computational algorithms david davenport computer eng. dept., bilkent university ankara - turkey....

24
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: [email protected] ...a lightning introduction to real-world algorithms

Upload: lindsey-mason

Post on 03-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Computational Algorithms

David Davenport

Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.

email: [email protected]

...a lightning introduction to real-world algorithms

Page 2: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

IMPORTANT… Students…

This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn!

Instructors…You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.

thank you, David.

Page 3: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

From problem to program In “real world”…

Do thisrepeat if something then do this else do the otherPrint “all done”

• Problem in Natural language• Top Down Design in pseudo-code• Program in computer code

(code in almost any computer language.)

Requirements:

A program that does this and that and theother. It must take data fromthis source and

// Program example// David, Oct 2002

import java.io.*;Import cs1.JRobo;

public class Example {

public static void mai System.out.println( JRobo robby = new J robby.f(100); robby.rect( 150, 50); }

}

DESIGN

IMPLEMENTReduce cognitive load • separating design and coding• using top-down design

The Problem

The Design

The Program

Page 4: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

THE CIRCLE COMPUTERExample…

Page 5: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference Problem

Requirements:Compute and report the area and circumference of a circle whose radius the user gives.

Welcome to circle computer…

Please enter the radius: 5

The area of a circle of radius 5 is 78.55

and its circumference is 31.42

Example interaction

Page 6: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Put yourself in the place of the computer and explain what you

would do in plain English

Area-Circumference ProblemTo find area & circumference of circle…

First say “Welcome to circle computer…” and then ask the user for the radius of their circle and get the radius value the user gives in response. Next, compute the corresponding area as pi times the radius squared, and then compute the circumference as two times pi times the radius. Finally, tell the user that the area of a circle of the radius they gave is the area value you computed, and the circumference is the circumference value you computed.

Page 7: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference ProblemTo find area & circumference of circle…

First say “Welcome to circle computer…” and then ask the user for the radius of their circle and get the radius value the user gives in response. Next, compute the corresponding area as pi times the radius squared, and then compute the circumference as two times pi times the radius. Finally, tell the user that the area of a circle of the radius they gave is the area value you computed, and the circumference is the circumference value you computed.

Page 8: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference ProblemTo find area & circumference of circle…First say “Welcome to circle computer…”and then ask the user for the radius of their circle

and get the radius value the user gives in response

Next, compute the corresponding area as pi times the radius squared,

and then compute the circumference as two times pi times the radius.

Finally, tell the user that the area of a circle of the radius they gave is the area value you computed, and the circumference is the circumference value you computed.

Page 9: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference ProblemTo find area & circumference of circle…1. Say “Welcome to circle computer…”2. Ask the user for the radius of their circle and

get the radius value the user gives in response 3. Compute the corresponding area as pi times

the radius squared, 4. Compute the circumference as two times pi

times the radius. 5. Tell the user that the area of a circle of the

radius they gave is the area value you computed, and the circumference is the circumference value you computed.

Page 10: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference Problem

To find area & circumference of circle…

1. Print welcome message2. Ask for & get radius from user3. Compute area as pi.radius.radius4. Compute circumference as 2.pi.radius5. Report area, circumference & radius

Once we are sure that this is correct, move on to solve any non-

trivial sub-problems.

Identify preconditions ( for each step )

& ensure they are satisfied.

Page 11: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference Problem

Solve…2. Ask for & get radius from user

1. Ask (prompt) the user to enter radius2. Get radius value from user

Welcome to circle computer…

Please enter the radius: _

Welcome to circle computer…

_

Page 12: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference Problem

To find area & circumference of circle…

1. Print welcome message2. Ask for & get radius from user3. Compute area as pi.radius.radius4. Compute circumference as 2.pi.radius5. Report area, circumference & radius

The area of a circle of radius 5 is 78.55

and its circumference is 31.42

The area of a circle of radius 10 is 312.4

and its circumference is 62.84

Page 13: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Area-Circumference Problem

Take care with spaces, number formats & line breaks!

Solve…5. Report area, circumference & radius

1. Print msg “The area of a circle with radius ”2. Print radius value3. Print msg “ is ”4. Print area value and move to next line5. Print msg “ and its circumference is ”6. Print circumference value7. Print blank line

Page 14: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

EXAM AVERAGE PROBLEMExample…

Page 15: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemRequirements:Given a set of already graded exam papers,compute and report the average grade.

Algorithm1. Print welcome message2. Given the set of exam papers

find the number of papers and the sum of all the grades on the papers

3. Compute average grade as sum of all the grades / number of papers

4. Report the average grade5. Print “all done” message

Page 16: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemRevised algorithm

Algorithm1. Print welcome message2. Given the set of exam papers

find the number of papers and the sum of all the grades on the papers

3. if number of papers is zero then3T Print msg “no grades entered”

else3F.1 Compute average grade

as sum of all the grades / number of papers3F.2 Report the average grade4. Print “all done” message

Page 17: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemSolve…

2. Given the set of exam papersfind the number of papers and the sum of all the grades on the papers

1. Ask user for & get the number of papers2. For each paper read the grade from the paper and

add it to the sum of grades so far.3. Sum of all the grades is now sum of grades so far

Page 18: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemSolve… (Alternative)2. Given the set of exam papers

find the number of papers and the sum of all the grades on the papers

1. Set count of papers so far to zero, then for each paper add one to the count of papers so far.

2. Set sum of grades so far to zero, and then for each paper read the grade from the paper and add it to the sum of grades so far.

3. Number of papers is now count of papers so far4. Sum of all grades is now sum of grades so far

Page 19: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemSolve… (Yet another alternative)2. Given the set of exam papers

find the number of papers and the sum of all the grades on the papers

1. Set count of papers so far to zero2. Set sum of grades so far to zero3. For each paper, read the grade from the paper, add it

to the sum of grades so far and add one to the count of papers so far

4. Number of papers is now count of papers so far5. Sum of all grades is now sum of grades so far

Page 20: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Exam average problemSolve… (& yet another alternative)2. Given the set of exam papers

find the number of papers and the sum of all the grades on the papers 1. Set count of papers so far to zero2. Set sum of grades so far to zero3. For each paper3.1 read the grade from the paper, 3.2 add grade to the sum of grades so far3.3 add one to the count of papers so far4. Number of papers is now count of papers so far5. Sum of all grades is now sum of grades so far

Page 21: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

OF NOTE…

Page 22: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Sentence forms - control Sequence

“Do this and then do that and then do the other and …”

Decision/alternation “if this condition is true then do this else do that” “if this condition is true then do this”

Repetition “for each/every/all do this” “repeat this until condition is true” “while this condition is true do this” “do this while condition holds”

Page 23: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Sentence forms - data Input

“get value from user”

Computation “compute this as some function of that”

Output “print this message” “print/report this value” ERRORS

•Syntax•Logical•Run-time

Page 24: Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr...a lightning introduction

Types & Layout (of algorithm steps)

n. dostep

while condition

indent

1. step 2. step3. step4. step

Sequence

n. if condition then

nT stepelse

nF stepindent

Decision

n. while condition dostep

indent

Repetition

Any step can be replaced

with one of the other types or input, output, assignment.

n. for so many times dostep