General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and...

40
General Computer Science General Computer Science for Engineers for Engineers CISC 106 CISC 106 Lecture 13 - Midterm Lecture 13 - Midterm Review Review James Atlas Computer and Information Sciences 10/02/2009

description

Unix Commands When 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

Transcript of General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and...

Page 1: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 2: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Important Notes on ExamImportant Notes on ExamWrite codeStudy labsStudy Midterm review

Page 3: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 4: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 5: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

IF/Elseif StatementsIF/Elseif Statementsif (expression1)

statements1;elseif (expression2)

statements2;else statements3;end

Page 6: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 7: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

If statementsIf statementsprint “blue” if N <= 5 print “red” if N > 5 and N <=

10print “green” if N > 10

Page 8: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 9: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 10: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Logical Operators&, && - AND|, || - OR~ - NOT

Page 11: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 12: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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!

Page 13: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 14: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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);

Page 15: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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);

Page 16: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Tracing FunctionsNon-recursive, call to function a()

function out = a() out = b();

function out = b() out = c();

function out = c() out = 5;

Page 17: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Tracing Functions

function arg local return value function out = a() out = b();

function out = b() out = c();

function out = c() out = 5;

Page 18: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Tracing Functions

a b()function arg local return value

function out = a() out = b();

function out = b() out = c();

function out = c() out = 5;

Page 19: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 20: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 21: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 22: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 23: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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;

Page 24: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Tracing RecursionRecursive call to function sum(3)

function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);

Page 25: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

Tracing Recursionsum(n) if n < 1 out = 0; else out = n + sum(n - 1);

sum n=3 sum(2)function arg local return value

Page 26: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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)

Page 27: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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)

Page 28: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 29: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 30: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 31: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 32: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 33: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 34: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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]

Page 35: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 36: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 37: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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.

Page 38: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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);

Page 39: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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

Page 40: General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.

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