1 / 28

General Computer Science for Engineers CISC 106 Midterm 2 Review

General Computer Science for Engineers CISC 106 Midterm 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’

alamberton
Télécharger la présentation

General Computer Science for Engineers CISC 106 Midterm 2 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. General Computer Science for EngineersCISC 106Midterm 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. 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’

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

  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 [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

  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) • Find the minimum data in the set • Swap it with the first element of the set • Repeat Steps 1-2 for the remaining elements

  14. Running Time (Selection Sort) loop progress

  15. 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]

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

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

  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 = [1 2 3 4 5 6]; • x < 3 • x(x < 3) • x(x < 3 | 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 = [1 2 3 4 5 6]; • y = x < 3 • x(y) = x(y) .* 2;

  26. MATLAB functions - find • find • locates index of all nonzero elements of array • z = [5 3 0 0 0 7]; • find(z) • [1 2 6]

  27. MATLAB functions - any/all • x = [1 2 3 4 5 6]; • any(x < 3) • any(x < 0) • all(x > 1) • all(x > 0)

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

More Related