280 likes | 299 Views
Important notes on the CISC 106 Midterm exam, covering topics such as logical operators, array commands, loop analogies, search algorithms, sorting algorithms, MATLAB structures, array initialization, vectorization, masking, and MATLAB functions. Prepare for success with this comprehensive review!
E N D
General Computer Science for EngineersCISC 106Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009
Important Notes on Exam Write code Study labs + project Study Midterm review
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
Logical Operators • &, && - AND • |, || - OR • ~ - NOT
Array commands • 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’
For Loops • Used when you know how many times code is to be executed. • Syntax for <variable> = <start>:<increment>:<end> • 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.
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);
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
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
Linear Search • Given [5 4 2 10 6 7] find which position 6 occupies • Alternatively, does the array contain the number 6? foundIndex = -1; for index = 1:length(values) if (values(index) == 6) foundIndex = index; end end
Binary Search • Now, what if the array is sorted, can we search it faster?
Binary Search • Find N in list • Pick a number X halfway between the start and end of the list • If X == N we are done else if X < N search top half of list else search bottom half of list
Algorithm for sorting (Selection Sort) • Find the minimum data in the set • Swap it with the first element of the set • Repeat Steps 1-2 for the remaining elements
Running Time (Selection Sort) loop progress
Algorithm for sorting (Quick Sort) • Choose a random pivot point • Split your data into two sets: • Low - the data < pivot • High - the data > pivot • Repeat Steps 1-2 for both data sets • The sorted data is [low pivot high]
Running Time (Quick Sort) recursion progress If we divide the size of the data, n, in half at each step, how many steps does this take? log n area of table = width * height = n * log n
A Database Application • Given: Name: Chris Credits: 27 Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012
A Database Application Name: Chris Credits: 27 Graduation: 12/15/2011 Chris 27 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Sola 18 05/17/2011 Roger 55 06/10/2009 Name: Roger Credits: 55 Graduation: 06/10/2009 Tom 15 05/22/2012 Name: Tom Credits: 15 Graduation: 05/22/2012 Given: We can implement it with arrays like this: Name Credits Grad. 1 2 3 4
A Database Application Name: Chris Credits: 27 Graduation: 12/15/2011 Students (1). Name: Chris Students (1).Credits: 27 Students (1). Graduation: 12/15/2011 Name: Sola Credits: 18 Graduation: 05/17/2011 Students (2).Name: Sola Students (2).Credits: 18 Students (2).Graduation: 05/17/2011 Name: Roger Credits: 55 Graduation: 06/10/2009 Students (3). Name: Roger Students (3). Credits: 55 Students (3). Graduation: 06/10/2009 Name: Tom Credits: 15 Graduation: 05/22/2012 Students (4). Name: Tom Students (4). Credits: 15 Students (4). Graduation: 05/22/2012 Given: OR we can do it like this an array with structs: .d
MATLAB Array Initialization y = []; for i = 1:10 y(i) = i; end; This is an example of “growing” an array
MATLAB Array Initialization y = zeros(1,10); for i = 1:10 y(i) = i; end; Initializes the array first
Vectorization • Additional examples • x = [1 2 3 4 5 6]; • x < 3 • x(x < 3) • x(x < 3 | x > 3) • x < 3 produces a mask
Masking • Masking selects only certain elements of an array to perform an operation • Masking uses an array of only 0’s and 1’s that is the same size as the argument • y = x < 3 • whos y • y is a mask of x that selects only the elements that are less than 3
Masking • x = [1 2 3 4 5 6]; • y = x < 3 • x(y) = x(y) .* 2;
MATLAB functions - find • find • locates index of all nonzero elements of array • z = [5 3 0 0 0 7]; • find(z) • [1 2 6]
MATLAB functions - any/all • x = [1 2 3 4 5 6]; • any(x < 3) • any(x < 0) • all(x > 1) • all(x > 0)
MATLAB functions - randi • rand() • randi(100)