general computer science for engineers cisc 106 lecture 02 james atlas computer and information...
Post on 19-Dec-2015
216 views
TRANSCRIPT
![Page 1: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/1.jpg)
General Computer Science General Computer Science for Engineersfor Engineers
CISC 106CISC 106Lecture 02Lecture 02
James AtlasComputer and Information Sciences
6/10/2009
![Page 2: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/2.jpg)
Lecture OverviewLecture OverviewHow does our program work?
More on VariablesMore on ExpressionsMore on FunctionsM-files
- Scripts versus functions
![Page 3: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/3.jpg)
How does our program work?CPU
Disk
Memory
Putting it together
![Page 4: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/4.jpg)
VariablesVariablesvar = expression
x = 2 * 2comment = ‘This is a string’area = circleArea(5)
![Page 5: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/5.jpg)
VariablesVariablesvar = expression
x = 2 * 2comment = ‘This is a string’area = circleArea(5)
What type of data is stored in each variable?
![Page 6: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/6.jpg)
ExpressionsData + Operator
◦2 + 2◦circleArea(5)
![Page 7: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/7.jpg)
Our ringArea(rad1, rad2)
function out=ringArea(radius1, radius2)area1 = circleArea(radius1);area2 = circleArea(radius2);out = area1 - area2;
Can you think of a simpler way to write this?
![Page 8: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/8.jpg)
ExpressionsData + Operator
◦2 + 2◦circleArea(5)
Nested Expressions◦circleArea(5) + circleArea(3)
![Page 9: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/9.jpg)
FunctionsFunctionsAnalogous to mathematical
functions◦f(x) = x + 1
Each function is like a mini-program within the larger program itself
Key to breaking problems down
![Page 10: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/10.jpg)
FunctionsFunctionsIn MATLAB, the first line of a function
takes the following form:function <return value> =
<name>(<arguments>)
![Page 11: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/11.jpg)
Functions cont.Functions cont.The first line of a function:function <return value> =
<name>(<arguments>)
The return value can be a number, a string, a matrix, etc.
Arguments ◦Can be a list of zero or more variables ◦ Can also be numbers, strings, matrices
![Page 12: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/12.jpg)
FunctionsFunctionsFunctions can call other functions
Like in class on Monday:
function outputValue = ringArea(rad_1, rad_2)
outputValue = circleArea(rad_1) - circleArea(rad_2);
![Page 13: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/13.jpg)
FunctionsFunctionsBreak down problems into
subproblems◦Decomposition
How big/small should a function be?
![Page 14: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/14.jpg)
Problem SolvingProblem SolvingMP3 player
◦Store song lists, store playlists, import/export songs, shuffle play, repeat play, etc.
![Page 15: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/15.jpg)
Bank ATM SoftwareBank ATM SoftwareWhat are inputs and outputs?Inputs:Output:
![Page 16: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/16.jpg)
Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?
![Page 17: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/17.jpg)
Bank ATM SoftwareBank ATM SoftwareWhat functions might we need?
Break it down even more?
![Page 18: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/18.jpg)
M-filesM-filesA script file
◦Store commands in◦Running a script file (m-file)
A function file◦Special type of m-file◦Contains a function name,
arguments, etc., and implementation
![Page 19: General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009](https://reader035.vdocuments.us/reader035/viewer/2022062421/56649d395503460f94a12de5/html5/thumbnails/19.jpg)
Scripts vs. functionsScripts vs. functionsFunctions have input and output
parametersScripts are a sequence of commands
Functions are more flexibleFunction files have one function per
file