Download presentation
Presentation is loading. Please wait.
Published byJohnathan Clarke Modified over 9 years ago
1
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009
2
Important Notes on Exam Write code Study labs Study Midterm review
3
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
4
Basic if statements IF statements allow program to make choices whether a condition is met or not if (expression1) statements1; end if (expression2) statements2; end
5
IF/Elseif Statements if (expression1) statements1; elseif (expression2) statements2; else statements3; end
6
Major 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
7
If statements print “blue” if N <= 5 print “red” if N > 5 and N <= 10 print “green” if N > 10
8
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
9
Switch construct color = ‘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
10
Logical Operators &, && - AND |, || - OR ~ - NOT
11
Logical Operators Evaluate the following as true or false for x=5: x < 7 & 3 < x 2. ˜ (x / 2 > 1 & x < 0) x ˜ = 5 & x + 2 > 6 | x > 5 x + x == 2 * x && ˜ x
12
Functions.m-file ◦ Function name same as file name Contains a function name, arguments, output, and “implementation” ◦ Contract ◦ Description ◦ Examples All variables in function are local ◦ They are not visible outside call!
13
Example 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) -> 6362 function output = circleArea(radius) output = pi * radius * radius;
14
Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
15
Recursive Function Function that refers to itself Example: sum numbers 1 to n function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
16
Tracing Functions Non-recursive, call to function a() function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
17
Tracing Functions functionarglocal return value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
18
Tracing Functions ab() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
19
Tracing Functions ab() bc() functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
20
Tracing Functions ab() bc() c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
21
Tracing Functions ab() b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
22
Tracing Functions a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
23
Tracing Functions a5 5 b5 5 c 5 functionarglocalreturn value function out = a() out = b(); function out = b() out = c(); function out = c() out = 5;
24
Tracing Recursion Recursive call to function sum(3) function out=sum(n) if n < 1 out = 0; else out = n + sum(n - 1);
25
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value
26
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1)
27
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0)
28
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=1sum(0) sumn=0 0
29
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=2sum(1) sumn=10 1 sumn=0 0
30
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=3sum(2) functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
31
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
32
Tracing Recursion sum(n) if n < 1 out = 0; else out = n + sum(n - 1); sumn=33 6 functionarglocalreturn value sumn=21 3 sumn=10 1 sumn=0 0
33
Arrays All variables in matlab are arrays An array of one element is called a scalar A one dimension array is called a vector x=3.14; scalar a = [1,2,3,4,5]; vector
34
Arrays Interval 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]
35
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
36
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) = 7 a(3:end) a(1:2:end) d’ Array commands
37
For Loops Used when you know how many times code is to be executed. Syntax for = : : Variable is initially the start value At end of iteration variable changes by increment If value is not greater than end the loop runs again.
38
Example Problem total = 0; for i = 1:1:1000 loop starts at 1 total = total + i;loop increments by 1 end loop ends at 1000 disp(total);
39
A Loop Analogy (for) The runner executes a loop. If they know the distance they want to run For loop for lapCount = start : 1 : end runLap() end
40
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.