Вы находитесь на странице: 1из 17

CS 170: Computing for the Sciences and Mathematics

Computing: Languages and Algorithms

Administrivia
Last time Exam #1
Scores are posted Exams will be returned/discussed on Thursday

Today What is computation? How do we talk to computers? HW #6 Assigned

Flow of Computation (abstract)


Data

Program

Results
(computer)

How do we tell a computer what to do?


Write a Program A program is a machine-understandable

implementation of an Algorithm

An algorithm is a sequence of instructions used to accomplish a goal

Algorithm ~= Solution Write an algorithm for walking from this classroom

to the Davies Center

How do we tell a computer what to do?


Write an Algorithm to move the red

dot to the green check


You cant move through the yellow

walls!
Only allowed the following commands: Move X

(move forward X spaces)

Turn Right (90 degrees) Turn Left (90 degrees)

How do we tell a computer what to do?


Program: machine-understandable pattern of

instructions for a computer to perform


Need a language to describe (specify) this instruction

pattern

An intermediary between humans and the computer

Language should be: Convenient for humans to express algorithms Convenient for computers to understand
Why not Natural Languages ? (like English)

Machine Language
0001110010000110 1101110110000010 0101001100110111
Sequence of instructions in binary code Code corresponds directly to instruction set of computer

Direct control of machine


Difficult to program, read, debug Not portable to other platforms

Assembly Language
LW r5, 0(r10) LW r6, 4(r10) ADD r7, r5, r6 SW r7, sum
These instructions typically correspond 1-to-1 with machine

language instructions Mnemonic codes Symbolic names (like sum) Direct control over the hardware Still hard to use and not portable

High-Level Languages
sum = 5 + 6;
Examples: C++, Fortran, Java, Python, Maple, Matlab,

Mathematica, Closer to natural language Easier to read, write, debug & modify Employ powerful data and control primitives, which allow relatively concise specification of complex computation patterns Requires no knowledge of computer hardware

Portability!

Introduction to Algorithms
Do algorithms relate to algebra? Both are derived from mathematician Mohammed ibn-Musa al-Khwarizmi Both deal with abstraction and manipulation Recall your basic algebra Variables a symbol that represents a number Expression a mathematical statement that may use numbers, variables, or both Equation statement that two expressions are equal Typical Application: Story Problems

Introduction to Algorithms
Algebra uses proven equations to transform one set of true

statements into another set of true statements x=3 y = 2x + 4 Therefore, y = 10


Algorithms use proven mutations to transform one set of

state into another set of state


State collection of variables with a value The value of a variable can change over time!

x := 3 x := x + 1

Introduction to Algorithms
Algebra has many truth-preserving manipulations Algorithms have only a handful of state-mutating

manipulations

Core Manipulations
Assignment to variables Sequence Selection Iteration

Assignment to Variables
Always need a variable on the left-hand side Three variants for right-hand side: A constant value

Example: Example: Example:

Balance 500 Balance SomeOtherBalance Balance Balance + Interest

Another variable

An expression using variables and constant values

Sequence
Most primitive form of controlling action one after the other Example: Baking a Take-Home Pizza

Preheat conventional oven to 475 degrees. Remove the plastic wrap and cardboard circle Save the cardboard circle to cut and serve your pizza on. Place the pizza on a pizza pan and place on the center rack in your oven. Bake for 20 30 minutes. Remove pizza from oven. Place pizza onto saved cardboard circle Cut into desired slices Serve and enjoy

Does order matter?

Selection
Select between options based on some condition Example: Baking a Take-Home Pizza (frozen or thawed)

Preheat conventional oven to 475 degrees. Remove the plastic wrap and cardboard circle Save the cardboard circle to cut and serve your pizza on. Place the pizza on a pizza pan and place on the center rack in your oven. if the pizza is frozen then Bake for 20 30 minutes else bake for 15 20 minutes Remove pizza from oven. Place pizza onto saved cardboard circle Cut into desired slices Serve and enjoy

Iteration
Repeat steps based on some condition Example: Baking multiple Take-Home Pizzas (frozen or thawed)
Preheat conventional oven to 475 degrees. while there are more pizzas to bake do Select pizza to bake Remove the plastic wrap and cardboard circle Save the cardboard circle to cut and serve your pizza on. Place the pizza on a pizza pan Place pan on the center rack in your oven. If the pizza is frozen then bake for 20 30 minutes else bake for 15 25 minutes Remove pizza from oven. Place pizza onto saved cardboard circle Cut into desired number of slices Serve and enjoy

HOMEWORK!

Homework 6 Maple Tutorial 1 Complete the worksheets questions and turn it in Due 10/25/2010

Thursdays Class in P115

Вам также может понравиться