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 can’t 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 .

0(r10) LW r6. 4(r10) ADD 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 . r6 SW r7. r5.Assembly Language LW r5.

Matlab. Fortran. … Closer to natural language Easier to read.     Mathematica.  Examples: C++. Maple. debug & modify Employ powerful data and control primitives. Java.High-Level Languages sum = 5 + 6. write. which allow relatively concise specification of complex computation patterns Requires no knowledge of computer hardware  Portability! . Python.

or both  Equation – statement that two expressions are equal  Typical Application: Story Problems . variables.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.

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 uses proven equations to transform one set of true statements into another set of true statements x=3 y = 2x + 4 Therefore.

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  .

Place the pizza on a pizza pan and place on the center rack in your oven.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. Remove pizza from oven. Bake for 20 – 30 minutes. Place pizza onto saved cardboard circle Cut into desired slices Serve and enjoy  Does order matter? .

if the pizza is frozen then Bake for 20 – 30 minutes else bake for 15 – 20 minutes Remove pizza from oven. Remove the plastic wrap and cardboard circle Save the cardboard circle to cut and serve your pizza on. Place pizza onto saved cardboard circle Cut into desired slices Serve and enjoy .Selection  Select between options based on some condition  Example: Baking a Take-Home Pizza (frozen or thawed) Preheat conventional oven to 475 degrees. Place the pizza on a pizza pan and place on the center rack in your oven.

Place pizza onto saved cardboard circle Cut into desired number of slices Serve and enjoy . Place the pizza on a pizza pan Place pan on the center rack in your oven.Iteration  Repeat steps based on some condition  Example: Baking multiple Take-Home Pizzas (frozen or thawed) Preheat conventional oven to 475 degrees. If the pizza is frozen then bake for 20 – 30 minutes else bake for 15 – 25 minutes Remove pizza from oven. 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.

HOMEWORK!  Homework 6  Maple Tutorial 1  Complete the worksheet’s questions and turn it in  Due 10/25/2010  Thursday’s Class in P115 .