140 likes | 156 Views
Computational Algorithms. ...a lightning introduction to real-world algorithms. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…
E N D
Computational Algorithms ...a lightning introduction to real-world algorithms David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr
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.
Do this For each x if something then do this else do the other Print “all done” 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
Example… The Circle computer
Area-Circumference Problem Requirements: Compute 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
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
Area-Circumference Problem To find area & circumference of circle… Firstsay “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 thencompute 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.
Area-Circumference Problem To find area & circumference of circle… Firstsay “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 thencompute 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.
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.
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 ( for each step )& ensure they are satisfied. Once we are sure that this is correct, move on to solve any non-trivial sub-problems.
Area-Circumference Problem Note: you might be tempted to say simply… • get radius from user Why is this a BAD IDEA? From user’s perspective runs program, it says “Welcome…” and then stops ! Welcome to circle computer… _ Has it crashed? Is it waiting for something? What should the user do? • - say “thanks”? their name? - the date? the diameter? … not veryuser-friendly
Area-Circumference Problem Solve… much better to tell the user what they should do! • Ask for & get radius from user • Ask (prompt) the user to enter the radius • Get radius value from user Welcome to circle computer… Please enter the radius: _
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 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
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!