Quiz1 Question  Add Binary Numbers 1 0 1 1 a) 1 0 1 0 1 0 +1 0 0 0 b) 0 1 0 0 1 1 1 0 0 1 1 c) 0 1 0 0 0 1 d) 0 1 0 1 1 1 e) none 001011 001000.

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

ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS1010 Programming Methodology
 Control structures  Algorithm & flowchart  If statements  While statements.
Computer Software & Software Development H&K Chapter 1 Instructor – Gokcen Cilingir Cpt S 121 (June 20, 2011) Washington State University.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Structured programming
Program Design and Development
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Pseudocode and Algorithms
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Adapted from slides by Marie desJardins
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
CIS162AD - C# Decision Statements 04_decisions.ppt.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Information and Programs. Foundations of Computing Information –Binary numbers –Integers and Floating Point –Booleans (True, False) –Characters –Variables.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CMSC 1041 Algorithms III Problem Solving and Pseudocode.
ITEC113 Algorithms and Programming Techniques
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
1 Lecture 3 Control Structures else/if and while.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
The ‘while’ loop ‘round and ‘round we go.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
CMSC 104, L041 Algorithms, Part 1 of 3 Topics Definition of an Algorithm Example: The Euclidean Algorithm Syntax versus Semantics Reading Sections 3.1.
CMSC 104, Version 8/061L05Algorithms2.ppt Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode Control Structures Reading Section 3.1.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 3 Selection Statements
CS1010 Programming Methodology
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
while Repetition Structure
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
UMBC CMSC 104 – Section 01, Fall 2016
JavaScript: Control Statements I
Computer Science 101 While Statement.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Arithmetic operations, decisions and looping
2008/09/22: Lecture 5 CMSC 104, Section 0101 John Y. Park
Structured Program
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
3 Control Statements:.
Chapter 3 – Control Structures
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Computer Science Core Concepts
Programming Concepts and Database
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
EPSII 59:006 Spring 2004.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Presentation transcript:

Quiz1 Question  Add Binary Numbers a) b) c) d) e) none

Quiz1 question  What is the largest decimal number you can represent using 3 bits ? a) 7 b) 8 c) 9 d) 15 e) 16 f) 17 g) None  What is a bit? A bit is a single binary digit (a 1 or 0). A byte is 8 bits

Algorithms 3 Pseudocode, If, If-Else, While, For

Control Structures Any problem can be solved using only three logical control structures: Sequence Selection Repetition

While & For

Euclid’s Algorithm Problem: Find the largest positive integer that divides evenly into two given positive integers (i.e., the greatest common divisor). Algorithm:  Assign M and N the values of the larger and smaller of the two positive integers, respectively.  Divide M by N and call the remainder R.  If R is not 0, then assign M the value of N, assign N the value of R, and return to Step 2. Otherwise, the greatest common divisor is the value currently assigned to N.

Finding the GCD of 24 and 9 MN R So, 3 is the GCD of 24 and

GCD Pseudocode Display “Enter the larger number: “ Read Display “Enter the smaller number: “ Read = modulo While ( > 0) = = modulo EndWhile Display “GCD =” MN R

While Loops Line 1 b=0 While (b<3) Line2 b=b+1 End While Line 3 If boolean expression is True the lines inside the while statement are executed. If boolean expression is False lines are skipped Line1 b=0 Line2 b=b+1 (b is now = 1) Line2 b=b+1 (b is now = 2) Line2 b=b+1(b is now = 3) Line3

For loop For ( = 5 To 16) Display "You are “, Display "Go to school." EndFor Display "School's out!" This would produce the following output : You are 5. Go to school. You are 6. Go to school.... You are 15. Go to school. You are 16. Go to school. School's out!

Compute the average of ten numbers =0 For (1 to 10) Display “Enter the number: ” Read = + EndFor = / 10 Display “average of the 10 numbers is = ”,

If Statements

Bank Display “Enter exisitng balance: “ Read Display “Enter the deposit amount: “ Read = + If ( < 500) = .02 Display “monthly Interest is”, = + End If Display “New balance is: “,

If Statements Line 1 If (b<5) Line2 Line3 Line4 End If Line 5 If Boolean expression is True the lines following the if statement are executed Lines following else statement are skipped If b = 2 the lines that will be executed are: Line1 Line2 Line3 Line4 Line5

If Statements Line 1 If (b<5) Line2 Line3 Line4 End If Line 5 If Boolean expression is True the lines following the if statement are executed Lines following else statement are skipped If b = 7 the lines that will be executed are: Line1 Line5

The Flow of the if Statement if statements true false next statement... test expression

Cookie Jar Problem Problem: Mom had just filled the cookie jar when the 3 children went to bed. That night one child woke up, ate half of the cookies and went back to bed. Later, the second child woke up, ate half of the remaining cookies, and went back to bed. Still later, the third child woke up, ate half of the remaining cookies, leaving 3 cookies in the jar. How many cookies were in the jar to begin with?

Original Pseudocode Display “Enter the number of children: “ Read Display “Enter the number of cookies remaining: “ Read = While ( > 0) = x 2 = - 1 End_While Display “Original number of cookies = “,

What will user see Enter the number of children: Enter the number of cookies remaining: Original number of cookies =

Cookie Jar Problem  What if the Cookie Jar was not touched Number of kids is 0  What if we wanted our Pseudocode to emphasize that the cookie jar was not touched.  We need to add an extra Display statement. But it should be executed only if number of kids is 0.

Pseudocode: Number of Children was 0 Display “Enter the number of children: ” Read Display “Enter the number of cookies remaining: “ Read = If ( ==0) Display “Cookie Jar was untouched” EndIf While ( > 0) = X 2 = - 1 End_While Display “Original number of cookies = “, 0

What will user see if He/She enters 0 Enter the number of children: Enter the number of cookies remaining: Cookie Jar was untouched Original number of cookies =

What will user see if He/She enters 1 Enter the number of children: Enter the number of cookies remaining: Original number of cookies =

Pseudocode: Number of Children was 0 Display “Enter the number of children: ” Read Display “Enter the number of cookies remaining: “ Read = If ( ==0) Display “Cookie Jar was untouched” EndIf While ( > 0) = x 2 = - 1 End_While Display “Original number of cookies = “, 1

If-Else

Compute a Min Display “Enter x: “ Read Display “Enter y: “ Read If ( ) Display “ Y is grater or equal to X” Else Display “ X is grater then Y” EndIfElse

If Else Statements Line 1 If (b<5) Line2 Line3 Line4 Else Line 5 Line 6 End if else Line 7 If Boolean expression is True the lines following the if statement are executed Lines following else statement are skipped If b = 2 the lines that will be executed are: Line1 Line2 Line3 Line4 Line7

If Else Statements Line 1 If (b<5) Line2 Line3 Line4 Else Line 5 Line 6 End if else Line 7 If Boolean expression is True the lines following the if statement are executed Lines following else statement are skipped If b = 7 the lines that will be executed are: Line1 Line5 Line6 Line7

The Flow of the if/else Statement if test expression if statements true false next statement else statements else...

Cookie Jar Problem- unsolvable Problem: Mom had just filled the cookie jar when the 3 children went to bed. That night one child woke up, ate half of the cookies and went back to bed. Later, the second child woke up, ate half of the remaining cookies, and went back to bed. The third child woke up, ate the remaining cookies the jar. How many cookies were in the jar to begin with?

Original Pseudocode Display “Enter the number of children: “ Read Display “Enter the number of cookies remaining: “ Read = While ( > 0) = x 2 = - 1 End_While Display “Original number of cookies = “,

if else Statement in Cookie Jar Problem Display “Enter the number of children: “ Read Display “Enter the number of cookies remaining: “ Read If ( ==0) Display “The mystery can not be solved” Else = While ( > 0) = x 2 = - 1 End_While Display “Original number of cookies = “, EndIfElse Display “Good Bye”

What will user see if He/She enters 1 Enter the number of children: Enter the number of cookies remaining: Original number of cookies = 24 Good Bye 3 3

What will user see if He/She enters 1 Enter the number of children: Enter the number of cookies remaining: The mystery can not be solved Good Bye 3 0

HW2  Logic must be correct  Style Do not write C Code ( no }, no ; ) Use Key words to Display and Read Need to have variables in <> Need to have indentation for if, if-else, for, while