General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and...
-
Upload
johnathan-clarke -
Category
Documents
-
view
224 -
download
0
description
Transcript of General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and...
General Computer General Computer Science Science
for Engineersfor EngineersCISC 106CISC 106
Lecture 13 - Midterm Lecture 13 - Midterm ReviewReviewJames Atlas
Computer and Information Sciences10/02/2009
Important Notes on ExamImportant Notes on ExamWrite codeStudy labsStudy Midterm review
Unix CommandsUnix CommandsWhen you log into a UNIX terminal
◦ You are in your home directory.◦ To see the files in your directory.
ls◦ To make an new folder/directory.
mkdir exampledir◦ To change directories.
cd exampledir◦ To go back one directory.
cd .. ◦ To go back to your home directory.
cd
Basic if statementsBasic if statementsIF statements allow program to
make choices whether a condition is met or not
if (expression1)
statements1;end
if (expression2) statements2;end
IF/Elseif StatementsIF/Elseif Statementsif (expression1)
statements1;elseif (expression2)
statements2;else statements3;end
Major Relational OperatorsMajor Relational Operators◦ A < B A is less than B◦ A > B A is greater than B◦ A <= B A is less than or equal to B◦ A >= B A is greater than or equal to B◦ A == B A is equal to B◦ A ~= B A not equal B
If statementsIf statementsprint “blue” if N <= 5 print “red” if N > 5 and N <=
10print “green” if N > 10
If statements (cont’d)If statements (cont’d) if (N <= 5) fprintf('blue\n‘);
elseif (N > 5 & N <= 10) fprintf('red\n‘); elseif (N > 10) fprintf('green\n‘); end
Switch constructcolor = ‘yellow’;switch (color)case ‘red’ disp(‘Stop now!’);case ‘green’ disp(‘Proceed through intersection.’);case ‘yellow’ disp(‘Prepare to stop.’);otherwise disp(‘Illegal color encountered.’);end
Logical Operators&, && - AND|, || - OR~ - NOT
Logical Operators Evaluate the following as true or
false for x=5:
1. x < 7 & 3 < x2. ˜(x / 2 > 1 & x < 0)3. x ˜= 5 & x + 2 > 6 | x > 54. x + x == 2 * x && ˜x
FunctionsFunctions.m-file
◦Function name same as file nameContains a function name, arguments,
output, and “implementation”◦Contract◦Description◦Examples
All variables in function are local◦They are not visible outside call!
Example FunctionExample Function%circleArea(number) -> number%Authors: James Atlas%CISC106 Lab Section 45 TA: Scott Ivanka %Description:% This function computes the area of a circle given the radius.%Examples:% circleArea(3) -> 28% circleArea(5) -> 78 % circleArea(45) -> 6362function output = circleArea(radius) output = pi * radius * radius;
Recursive FunctionFunction that refers to itselfExample: sum numbers 1 to n
function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Recursive FunctionFunction that refers to itselfExample: sum numbers 1 to n
function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Tracing FunctionsNon-recursive, call to function a()
function out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
function arg local return value function out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a b()function arg local return value
function out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a b()
b c()
function arg local return valuefunction out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a b()
b c()
c 5
function arg local return valuefunction out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a b()
b 5 5
c 5
function arg local return valuefunction out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a 5 5
b 5 5
c 5
function arg local return valuefunction out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing Functions
a 5 5
b 5 5
c 5
function arg local return valuefunction out = a() out = b();
function out = b() out = c();
function out = c() out = 5;
Tracing RecursionRecursive call to function sum(3)
function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
sum n=2 sum(1)
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
sum n=2 sum(1)
sum n=1 sum(0)
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
sum n=2 sum(1)
sum n=1 sum(0)
sum n=0 0
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
sum n=2 sum(1)
sum n=1 0 1
sum n=0 0
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 sum(2)function arg local return value
sum n=2 1 3
sum n=1 0 1
sum n=0 0
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 3 6function arg local return value
sum n=2 1 3
sum n=1 0 1
sum n=0 0
Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);
sum n=3 3 6function arg local return value
sum n=2 1 3
sum n=1 0 1
sum n=0 0
ArraysArraysAll variables in matlab are arraysAn array of one element is called a
scalarA one dimension array is called a
vector
x=3.14; scalar
a = [1,2,3,4,5]; vector
ArraysArraysInterval notation
x = 1:0.5:5
Now x is a vector of numbers:
x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]
Arrays (aka matrices)Arrays (aka matrices) A = [1, 2; 3, 4; 5, 6]
Creates a 3x2 matrix, 3 rows, 2 columns.
semicolon creates a new row.
A = 1 2 3 4 5 6
a = [1 2 3 4 5]b = [1; 2; 3; 4]c = [1 2; 3] (error)d = [1 2; 3 4]f = d(1,2)g(4,5) = 7a(3:end)a(1:2:end)d’
Array commandsArray commands
For LoopsFor LoopsUsed when you know how many times code is
to be executed.Syntax
for <variable> = <start>:<increment>:<end>
Variable is initially the start valueAt end of iteration variable changes by
incrementIf value is not greater than end the loop runs
again.
Example Problem Example Problem total = 0;
for i = 1:1:1000 loop starts at 1 total = total + i; loop increments by 1
end loop ends at 1000disp(total);
A Loop Analogy (for)A Loop Analogy (for)The runner executes a loop. If they know the distance they want to
runFor loop
for lapCount = start : 1 : end runLap()end
A Loop Analogy (while)A Loop Analogy (while)The runner executes a loop. If they don’t know the distance they
want to run (run until tired)While loop
tired = false;while(~tired)
tired = runLap()end