Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009

2 Lecture Overview Linear Search (recursive) Binary Search Midterm 1 Review ◦ Unix commands ◦ if statements ◦ arrays (or matrices) ◦ loops ◦ scripts ◦ functions ◦ recursion

3 Linear search Searching, extremely important in computer science If we have a list of unsorted numbers, how could we search them?

4 Iterative strategy Given [5 4 2 10 6 7] find which position 6 occupies Alternatively, does the array contain the number 6?

5 Recursive strategy

6 Binary search Now, what if the array is sorted, can we search it faster? Group exercise: Speed up our search process if we know the array is already sorted (smallest to greatest) Hint: Try splitting the array in half

7 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

8 Let’s do this in Matlab Recursive solution

9 Binary Search Flowchart diagram of the algorithm Note the two stopping conditions (Exits) Note the one loop (Note: flowchart is equivalent to pseudocode) How much faster is this than linear search? If linear search takes roughly n steps for n things, Then binary search takes roughly log2(n) steps for n things. This is because we divide the n things by 2 each time.

10

11 Midterm and Review Midterm review online ◦ http://www.udel.edu/CIS/106/jatlas/09Su/exams/09Su/mi dterm1review.pdf http://www.udel.edu/CIS/106/jatlas/09Su/exams/09Su/mi dterm1review.pdf Midterm 1 ◦ July 1 (Class Time : Wednesday!)

12 Important Notes on Exam Write pseudo-code from memory Study labs Study Midterm review Bloom’s Taxonomy

13

14 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

15 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

16 IF/Elseif Statements if (expression1) statements1; elseif (expression2) statements2; else statements3; end

17 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

18 If statements print “blue” if N <= 5 print “red” if N > 5 and N <= 10 print “green” if N > 10

19 If statements (cont’d) if (N <= 5) fprintf('blue\n‘); end if (N > 5 & N <= 10) fprintf('red\n‘); end if (N > 10) fprintf('green\n‘); end

20 Arrays (aka matrices) 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

21 Arrays (aka matrices) x = 1:0.5:5 Now x is an array of numbers; x = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]

22 Arrays (aka matrices) A = [1, 2; 3, 4; 5, 6] Creates a 3x2 array, 3 rows, 2 columns. semicolon creates a new row. A = 1 2 3 4 5 6

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

24 Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold 115 222 320 418 Widget(1) = 15 Widget(2) = 22 Widget(3) = 20 Widget(4) = 18 Avg = (Widget(1) + Widget(2) + Widget(3) + Widget(4)) / 4 ◦ This is easy for a small number of days. ◦ What if we had a 1000 days? ◦ We can use a for loop!

25 Example Problem total = 0; for i = 1:1:1000 loop starts at 1 total = total+widget (i);loop increments by 1 end loop ends at 1000 avg = total / 1000;

26 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

27 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

28 Scripts files Store commands in Variables are global, available after you call script file

29 Scripts files sumIt=0; for current=1:finish if (mod(current,2)==1) sumIt=sumIt+current; end

30 Functions Special type of m-file ◦ Function name same as file name Contains a function name, arguments, output, and “implementation” All variables in function are local ◦ They are not visible outside call!

31 Example Function function sumIt=sumOddInt(finish) sumIt=0; for current=1:finish if (mod(current,2)==1) sumIt=sumIt+current; end % sumIt, current, and finish are local

32 When you call a function… function foo1 function bar1 function foo2 function bar2 function main function main calls foo1 function foo1 calls bar1 function bar1 calls foo2 function foo2 calls bar2 function bar2 executing

33 Recursion Example Classic Example ◦ Function output = numbersSum(input) if (input == 1) output = 1; else output = input+numbersSum(input-1) end


Download ppt "General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009."

Similar presentations


Ads by Google