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

Slides:



Advertisements
Similar presentations
General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.
Advertisements

Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
General Computer Science for Engineers CISC 106 Lecture 08
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
General Computer Science for Engineers CISC 106 Lecture 25 Dr. John Cavazos Computer and Information Sciences 04/20/2009.
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/2009.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Extending MATLAB Write your own scripts and/or functions Scripts and functions are plain text files with extension.m (m-files) To execute commands contained.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 16: Searching, Sorting, and the vector Type.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Linux Operations and Administration
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
CSC 211 Data Structures Lecture 13
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Digital Image Processing Introduction to M-function Programming.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 2/17/2010.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Searching Topics Sequential Search Binary Search.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009.
General Computer Science for Engineers CISC 106 Lecture 05 James Atlas Computer and Information Sciences 6/22/2009.
Merge Sort Presentation By: Justin Corpron. In the Beginning… John von Neumann ( ) Stored program Developed merge sort for EDVAC in 1945.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 9/9/2009.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Chapter 16: Searching, Sorting, and the vector Type
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Matlab Training Session 4: Control, Flow and Functions
CS1371 Introduction to Computing for Engineers
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

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

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

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

Iterative strategy Given [ ] find which position 6 occupies Alternatively, does the array contain the number 6?

Recursive strategy

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

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

Let’s do this in Matlab Recursive solution

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.

Midterm and Review Midterm review online ◦ dterm1review.pdf dterm1review.pdf Midterm 1 ◦ July 1 (Class Time : Wednesday!)

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

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

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

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

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

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

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

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

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]

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

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.

Example Problem I want to find the average # of widgets sold in 4 days Day# of widgets sold 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!

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;

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

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

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

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

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!

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

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

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