Download presentation

Presentation is loading. Please wait.

Published byHector Vaillant Modified over 2 years 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 = [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

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

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
10843125769 1843 25769 1243 85769 1234 85769 1234 85769 123458 769 123456 789 1234567 89 12345678 9 123456789 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
10843125769 184362579 154362789 124365789 124356789 123456789 123456789 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. 1 2 3 4 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 27Chris12/15/2011 18Sola05/17/2011 55Roger06/10/2009 15Tom05/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 = [1 2 3 4 5 6]; ◦ 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 = [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)

Similar presentations

Presentation is loading. Please wait....

OK

3 – SIMPLE SORTING ALGORITHMS

3 – SIMPLE SORTING ALGORITHMS

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on student leadership Ppt on training and placement cell project Ppt on commerce education in india Slide show view ppt on ipad Ppt on email and search engines Ppt on applied operational research course Ppt on type 2 diabetes mellitus Download ppt on nutrition in plants for class 7 Ppt on sources of energy Ppt on terrorism in india free download