Realworld Algorithms - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Realworld Algorithms

Description:

a lightning introduction to algorithms. IMPORTANT... Students... Design an algorithm to find and report the area and circumference of a circle ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 22
Provided by: davidda3
Category:

less

Transcript and Presenter's Notes

Title: Realworld Algorithms


1
Real-world Algorithms
...a lightning introduction to algorithms
  • David Davenport
  • Computer Eng. Dept.,
  • Bilkent UniversityAnkara - Turkey.
  • email david_at_bilkent.edu.tr

2
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.
3
From problem to program
  • In Robo
  • Reduce cognitive load hence mistakes by
  • separating design and coding
  • using top-down design

The Problem
The Design
The Program
DESIGN
IMPLEMENT
Dont forget unit and integration testing
4
From problem to program
  • In real world
  • Problem in Natural language
  • Top Down Design in pseudo-code
  • Program in computer code(code in almost any
    computer language.)

The Problem
Requirements A program that does this and
that and theother. It must take data fromthis
source and
The Design
The Program
// 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
  • Reduce cognitive load
  • separating design and coding
  • using top-down design

IMPLEMENT
5
Area-Circumference Problem
  • Requirements
  • Design an algorithm to find and report the area
    and circumference of a circle whose radius the
    user gives.

Example interaction
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
6
Area-Circumference Problem
  • To 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.

Put yourself in the place of the computer and
explain what you would do in plain English
7
Area-Circumference Problem
To 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.
8
Area-Circumference Problem
To 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.
9
Area-Circumference Problem
  • To find area circumference of circle
  • Say Welcome to circle computer
  • Ask the user for the radius of their circle and
    get the radius value the user gives in response
  • Compute the corresponding area as pi times the
    radius squared,
  • Compute the circumference as two times pi times
    the radius.
  • 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.

10
Area-Circumference Problem
  • To find area circumference of circle
  • Print welcome message
  • Ask for get radius from user
  • Compute area as pi.radius.radius
  • Compute circumference as 2.pi.radius
  • Report area, circumference radius

Identify preconditions ensure they are
satisfied.
Once we are sure that this is correct, move on to
solve any non-trivial sub-problems.
11
Area-Circumference Problem
  • Solve
  • Ask for get radius from user
  • Ask (prompt) the user to enter radius
  • Get radius value from user

12
Area-Circumference Problem
  • Solve
  • Report area, circumference radius
  • Print msg The area of a circle with radius
  • Print radius value
  • Print msg is
  • Print area value and move to next line
  • Print msg and its circumference is
  • Print circumference value
  • Print blank line

Take care with spaces, number formats line
breaks!
13
Exam average problem
  • Requirements
  • Given a set of already graded exam
    papers,compute and report the average grade.
  • Algorithm
  • Print welcome message
  • Given the set of exam papersfind the number of
    papers and the sum of all the grades on the
    papers
  • Compute average grade as sum of all the grades /
    number of papers
  • Report the average grade
  • Print all done message

14
Exam average problem
  • Revised algorithm

Algorithm 1. Print welcome message 2. 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 else 3F.1 Compute average grade
as sum of all the grades / number of
papers 3F.2 Report the average grade 4. Print
all done message
15
Exam average problem
  • Solve
  • Given the set of exam papersfind the number of
    papers and the sum of all the grades on the
    papers
  • Ask user for get the number of papers
  • For each paper read the grade from the paper and
    add it to the sum of grades so far.
  • Sum of all the grades is now sum of grades so far

16
Exam average problem
  • Solve (Alternative)
  • Given the set of exam papersfind the number of
    papers and the sum of all the grades on the
    papers
  • Set count of papers so far to zero, then for each
    paper add one to the count of papers so far.
  • 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.
  • Number of papers is now count of papers so far
  • Sum of all grades is now sum of grades so far

17
Exam average problem
  • Solve (Yet another alternative)
  • Given the set of exam papersfind the number of
    papers and the sum of all the grades on the
    papers
  • Set count of papers so far to zero
  • Set sum of grades so far to zero
  • 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
  • Number of papers is now count of papers so far
  • Sum of all grades is now sum of grades so far

18
Exam average problem
  • Solve ( yet another alternative)
  • Given the set of exam papersfind the number of
    papers and the sum of all the grades on the
    papers
  • 1. Set count of papers so far to zero
  • 2. Set sum of grades so far to zero
  • 3. For each paper
  • 3.1 read the grade from the paper,
  • 3.2 add grade to the sum of grades so far
  • 3.3 add one to the count of papers so far
  • 4. Number of papers is now count of papers so far
  • 5. Sum of all grades is now sum of grades so far

19
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

20
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

21
Types Layout (of algorithm steps)
Any step can be replaced with one of the other
types
n. for so many times do step
Write a Comment
User Comments (0)
About PowerShow.com