General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

CS101: Introduction to Computer programming
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.
Lesson 5 - Decision Structure By: Dan Lunney
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Introduction to Computers and Programming Lecture 5 New York University.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Program Design and Development
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
General Computer Science for Engineers CISC 106 Lecture 08
General Computer Science for Engineers CISC 106 Lecture 13 Roger Craig Computer and Information Sciences 3/13/2009.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
ALGORITHMS AND FLOWCHARTS
Introduction CSE 1310 – Introduction to Computers and Programming
Chapter 4 The If…Then Statement
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
ITEC 2620A Introduction to Data Structures
Computer Science Selection Structures.
Introduction. 2COMPSCI Computer Science Fundamentals.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Basic Control Structures
Algorithm Design.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
Algorithms and Pseudocode
General Computer Science for Engineers CISC 106 Lecture 05 James Atlas Computer and Information Sciences 6/22/2009.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
General Computer Science for Engineers CISC 106 Lecture 03 James Atlas Computer and Information Sciences 6/15/2009.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
CE221 – Data Structures & Algorithms Lab Introduction
Chapter 4 The If…Then Statement
COP 3503 FALL 2012 Shayan Javed Lecture 15
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Algorithms and Flowcharts
Lesson 5-15 AP Computer Science Principles
3.1 Algorithms (and real programming examples).
Introduction To Flowcharting
ALGORITHMS AND FLOWCHARTS
Lecture 2: Logical Problems with Choices
ALGORITHMS AND FLOWCHARTS
Logical Operations In Matlab.
ITEC 2620M Introduction to Data Structures
Conditional Statements
Class 4: Repetition Pretest Posttest Counting Flowchart these!
Revision of C++.
Program Flow.
WRITING AN ALGORITHM, PSEUDOCODE, AND FLOWCHART LESSON 2.
ME 123 Computer Applications I Lecture 11: More on For loop 3/27/03
Presentation transcript:

General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009

Lecture Overview Practice problems Switch construct English -> Pseudocode -> Matlab Searching Linear Search Binary Search

Switch Construct like the IF construct, it is a way to branch or make a choice switch switch_expr %what we are testing the value of case case_expr % one possible value statement,..., statement %if it’s equal to the above value, execute this code case {case_expr1, case_expr2, case_expr3,...} %many possible values statement,..., statement %if it’s equal to one of above values, execute this otherwise statement,..., statement %otherwise (or else) execute this. end (Note: statements could be blank too, especially in the otherwise case.)

Switch Construct Example x = 4; If x == 1 disp(‘x is one’); elseif x == 3 disp(‘x is three’); elseif x == 4 | x == 5 disp(‘x is four or five’); else disp(‘x is not 1, 3, 4, or 5’); end MATLAB documentation: Nice quick video demonstrating it: elseif/ elseif/ (pp of Chapman) x = 4; switch x case 1 disp(‘x is one’); case 3 disp(‘x is three’); case {4 5} %note the braces disp(‘x is four or five’); otherwise disp(‘x is not 1,3, 4, or 5’); end

Pseudocode Problem solving is about planning You should have an English description and/or pseudocode before coding In the text: Page 93 of Chapman Optional links: Analogy: has a similar problem (possibly in a different field) been solved before? Analogy

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

Binary search Now, what if the list is sorted, can we search it faster?

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.

Practice problems… Are… A. Mandatory B. Optional C. A good idea D. Waste of time E. B and C

To make decisions or branch… A. Use a for loop B. Use an assignment statement C. Use the matlab branch() function D. Use an if or switch statement E. None of the above

The case part of a switch statement can have multiple conditions? A. True B. False

Pseudocode … A. Can be converted into any programming language B. Will help me score more points on exams if I can’t remember the exact Matlab code C. Is an semi-formal way of describing an algorithm and is important for planning D. Is intended for humans instead of computers E. All of the above

Linear search can be coded using one for or while loop A. True B. False

Binary search can be coded using one for or while loop. A. True B. False