Download presentation

Presentation is loading. Please wait.

Published byHector Vaillant Modified about 1 year ago

1
General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009

2
Important Notes on Exam Write code Study labs + project Study Midterm review

3
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

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

5
a = [ ] 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

6
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.

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

8
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

9
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

10
Linear Search Given [ ] 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

11
Binary Search Now, what if the array is sorted, can we search it faster?

12
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

13
Algorithm for sorting (Selection Sort) 1. Find the minimum data in the set 2. Swap it with the first element of the set 3. Repeat Steps 1-2 for the remaining elements

14
Running Time (Selection Sort) loop progress

15
Algorithm for sorting (Quick Sort) 1. Choose a random pivot point 2. Split your data into two sets: 1.Low - the data < pivot 2.High - the data > pivot 3. Repeat Steps 1-2 for both data sets 4. The sorted data is [low pivot high]

16
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

17
Structures in MATLAB

18
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

19
Given: We can implement it with arrays like this: Name Credits Grad 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/ Chris12/15/ Sola05/17/ Roger06/10/ Tom05/22/2012 A Database Application

20
Given: OR we can do it like this an array with structs:.d 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 Students (1). Name: Chris Students (1).Credits: 27 Students (1). Graduation: 12/15/2011 Students (2).Name: Sola Students (2).Credits: 18 Students (2).Graduation: 05/17/2011 Students (3). Name: Roger Students (3). Credits: 55 Students (3). Graduation: 06/10/2009 Students (4). Name: Tom Students (4). Credits: 15 Students (4). Graduation: 05/22/2012 A Database Application

21
MATLAB Array Initialization y = []; for i = 1:10 y(i) = i; end; This is an example of “growing” an array

22
MATLAB Array Initialization y = zeros(1,10); for i = 1:10 y(i) = i; end; Initializes the array first

23
Vectorization Additional examples ◦ x = [ ]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) x < 3 produces a mask

24
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

25
Masking x = [ ]; y = x < 3 x(y) = x(y).* 2;

26
MATLAB functions - find find ◦ locates index of all nonzero elements of array z = [ ]; find(z) ◦ [1 2 6]

27
MATLAB functions - any/all x = [ ]; any(x < 3) any(x < 0) all(x > 1) all(x > 0)

28
MATLAB functions - randi rand() randi(100)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google