CS112 Scientific Computation Department of Computer Science Wellesley College Divide, conquer, glue Program design.

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Introduction to Flowcharting
CS0004: Introduction to Programming Repetition – Do Loops.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Program Design and Development
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
1 Lecture-2 CS-120 Fall 2000 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 1 Program Design
Chapter 3 Planning Your Solution
CS112 Scientific Computation Department of Computer Science Wellesley College Interactive Programs Graphical User Interfaces.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
6 Steps of the Programming Process
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Introduction to Python
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
MATLAB® While Loops.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CS112 Scientific Computation Department of Computer Science Wellesley College Divide, conquer, glue Program design.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Visual Basic Programming
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 5 JavaScript.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CS112 Scientific Computation Department of Computer Science Wellesley College Blowing a gasket Classic recursive examples.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
1 Introduction to Flowcharting Computer Science Principles ASFA.
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Algorithms and Problem Solving
Variables, Expressions, and IO
Compiler Construction
Introduction to Computers
Program Design Introduction to Computer Programming By:
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithms and Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Experiment No. (1) - an introduction to MATLAB
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 4: Writing and Designing a Complete Program
Chapter 5 JavaScript Numbers and Expressions
Presentation transcript:

CS112 Scientific Computation Department of Computer Science Wellesley College Divide, conquer, glue Program design

2 Play it again, Sam… for i = 1:100% bit of a kludge… disp('Play it once, Sam, for old times'' sake ’ ); again = input('Play it again? (yes:1, no:0) '); if ~again break end again = 1; while again% much cleaner disp('Play it once, Sam, for old times'' sake ’ ); again = input('Play it again? yes(1) or no(0): '); end

3 The while statement conditional expression Yes No statements to repeat if true end while conditional expression statements to repeat if conditional expression is true end code following end

4 Fibonacci numbers, or multiplying rabbits? Fibonacci numbers first appear around 500 B.C. in writings of a Sanscrit grammarian named Pingala who studied rhythm in language Leonardo Fibonacci studied these numbers around 1200 in the context of multiplying rabbits In the first month, there ’ s one newborn pair Newborn pairs become fertile in their second month Each month, every fertile pair begets a new pair Rabbits never die …

5 Finding first Fibonacci number > 100 fibo = [1 1]; while (fibo(end) < 100) fibo(end+1) = fibo(end) + fibo(end-1); end disp([‘first Fibonacci number > 100: ' num2str(fibo(end))]);

6 Structures A structure can store multiple values of different types gold.name = 'gold'; gold.type = 'metal'; gold.symbol = 'Au'; gold.atomNum = 79; gold.mbPoints = [ ]; gold.bohrmodel = goldPict; name'gold' type'metal' symbol 'Au' atomNum 79 mbPoints bohrModel structure name field name field value gold

7 Structures make sharing easy function describeElement (element) % shows the properties stored in the input element structure disp(['name of element: ' element.name]); disp(['type of element: ' element.type]); disp(['atomic symbol: ' element.symbol]); disp(['atomic number: ' num2str(element.atomNum)]); disp(['melting point: ' num2str(element.mbPoints(1))... ' degrees Celcius' ]); disp(['boiling point: ' num2str(element.mbPoints(2))... ' degrees Celcius']); imshow(element.bohrModel);

8 Sharing structures >> describeElement(gold) name of element: gold type of element: metal atomic symbol: Au atomic number: 79 melting point: 1064 degrees Celcius boiling point: 2856 degrees Celcius name 'gold' type'metal' symbol 'Au' atomNum 79 mbPoints bohrModel gold

9 Program complexity Designing large scale programs is fraught with peril P P1P1 P2P2 P3P3 P4P4 S4S4 S3S3 S2S2 S1S1 S Divide, conquer and glue is a simple but powerful design strategy that helps us avoid danger

10 Tools of the trade We have used functions and scripts to help divide problems into manageable chunks: lineFit, poleVault rotate, spin displayGrid, virus What kinds of subtasks are performed by these individual functions in these programs, and why did we divide the programming task in this way?

11 Our goal is to design programs that: are free of errors run efficiently require no more memory than necessary are easy to understand and use can be used in a variety of situations are easy to maintain and modify if necessary *... and to do all of this within time and budget

12 Functions may… Perform a general function that is useful in many contexts e.g. lineFit function can be used for any linear regression Apply or test other functions e.g. poleVault tests the lineFit function Hide details of tasks like plotting or displaying data e.g. displayGrid displays the current state of the virus

13 Functions help to avoid repetitious code Consider a function with the following structure function outputs = myFunction (inputs) statements a statements b statements c statements b statements d statements b Encapsulate repetitious statements in a separate function similar statements

14 Test, test, test! “If there is no way to check the output of your program, in using that program, you have left the realm of scientific computation and entered that of mysticism, numerology, and the occult.” Daniel Kaplan Introduction to Scientific Computation and Programming

15 General tips on testing Test and debug each function on its own Create test data for simple cases where expected intermediate results and final answer can be easily verified Be thorough! Construct examples to test all cases considered by your program

16 Functions versus scripts Functions usually have one or more inputs that provide data or control aspects, and one or more outputs Scripts perform a specific set of actions and do not have inputs or outputs Execution of a function creates a private, temporary environment of variables Scripts have access to variables defined in the environment within which the script is called* * Danger Will Robinson!!!