Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009

2 Lecture Overview What if I want to do more Computer Science? ◦ Other courses ◦ Minor in CIS Final Review - Comprehensive

3 Lecture Overview Final Review - Comprehensive ◦ Midterm 1  Unix commands  if statements  arrays (or matrices)  loops  scripts  functions ◦ Midterm 2  recursion  search  sorting  vectorization  MATLAB functions - find/any/all/rand  structures ◦ Classes ◦ Cell Arrays ◦ Graphics ◦ Function callbacks (passing functions to functions)

4 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

5 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

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

7 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

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

9 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

10 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

11 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]

12 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

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

14 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!

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

16 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

17 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

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

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

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

21 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

22 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

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

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

25 Linear Search - Recursive Split array in half Search each half of array

26 Binary Search - Recursive 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

27 Selection Sort 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list Starting at the second position and advancing each time

28 Merge Sort 1. If the list is of length 0 or 1, then it is already sorted. 2. Otherwise: 1.Divide the unsorted list into two sublists of about half the size. 2.Sort each sublist recursively by re-applying merge sort. 3.Merge the two sublists back into one sorted list.

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

30 Vectorization What is vectorization? ◦ Functions that can be performed on the entire array instead of just one element in an array. Advantages of vectorization ◦ Fast and compact. Disadvantages of vectorization ◦ Hard to look at what is going on ‘inside’

31 Vectorization Examples ◦ x = [1 2 3 4 5 6]; ◦ x < 3 ◦ x(x < 3) ◦ x(x 3) ◦ vector_less = (x < 3); count = sum(vector_less); x < 3 produces a mask

32 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

33 Masking x = [1 2 3 4 5 6]; y = x < 3 x(y) = x(y).* 2;

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

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

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

37 Structures in MATLAB

38 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

39 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

40 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

41 record1.name = 'Me'; record2.name = 'Not Me'; record1.credits = 27; record2.credits = 30; record1.age = 10; record2.age = 14; function [] = displayRecordName(record) disp(record.name); displayRecordName(record1); displayRecordName(record2); Initializing a structure

42 Classes Object-oriented Programming Classes represent types of Objects Similar to structs

43 Class definition classdef dog properties name age end

44 Class definition (cont’) classdef dog properties name age end methods function obj=dog(name, age) obj.name = name; obj.age = age; end

45 The isa function isa(object, ‘classname’) isa(dog1, ‘dog’) isa(dog1, ‘person’)

46 Arrays of classes? [dog1 dog2] [dog1 dog2 person1] ?

47 Cell Arrays Normal array: zeros(1,5) = 5 doubles [1 2 3 4] = 4 doubles Cell array: {1 2 3 4} = ???

48 Cell Arrays (cont) c = {1 2 3 4} c{1} = ‘hello’ Cell arrays store an object reference ◦ Can be another array ◦ Or a string ◦... any struct/object/cell array

49 MATLAB Graphics Event-driven user interface A figure window contains: ◦ axes, line, plot, image, and figure itself ◦ controls such as buttons, menus, listboxes, textboxes  use uicontrol, and uimenu objects

50 MATLAB Simple GUI function simpleGUI x = 1; mainFig = figure('Visible','on',... 'name','SimpleGui',... 'Position',[210,170,660,440]); PlotButton = uicontrol('Style','PushButton',... 'Position',[200,43,80,40],... 'String','Click',... 'Callback',@func_Callback); function func_Callback(source,eventdata) disp('button clicked'); disp(x); x = x + 1; end

51 Function callbacks Passing a function to another function function []=functionPassTest(func, data) func(data) function []=printName(obj) disp(obj.name) cat.name = ‘Fifi’ functionPassTest(@printName, cat);


Download ppt "General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009."

Similar presentations


Ads by Google