Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.

Slides:



Advertisements
Similar presentations
How SAS implements structured programming constructs
Advertisements

Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
 Control structures  Algorithm & flowchart  If statements  While statements.
ACM Workshop Number Theory.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 2: Algorithm Discovery and Design
Discrete Mathematics Lecture 4 Harper Langston New York University.
Program Design and Development
Discrete Structures Chapter 2 Part B Mathematical Induction
Chapter 2: Input, Processing, and Output
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Fundamentals of Python: From First Programs Through Data Structures
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Fundamentals of Python: First Programs
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSI 3125, Axiomatic Semantics, page 1 Axiomatic semantics The assignment statement Statement composition The "if-then-else" statement The "while" statement.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Controlling Program Flow with Decision Structures.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 3: Decisions and Loops
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction To Flowcharting
3rd prep. – 2nd Term MOE Book Questions.
Program Design Introduction to Computer Programming By:
Copyright © Cengage Learning. All rights reserved.
Axiomatic semantics Points to discuss: The assignment statement
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter (3) - Looping Questions.
` Structured Programming & Flowchart
Faculty of Computer Science & Information System
Computer Science Core Concepts
Flowcharts and Pseudo Code
Application: Algorithms
Application: Algorithms
Chapter 2: Input, Processing, and Output
REPETITION Why Repetition?
COMPUTING.
Presentation transcript:

Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then stop. – Lewis Carroll, 1832 – 1898 Alice’s Adventures in Wonderland, 1865

Variable In higher-level computer languages, the term variable is used to refer to a specific storage location in a computer’s memory. Data Type The data type of a variable indicates the set in which the variables takes its values. Assignment Statement An assignment statement gives a value to the variable in the form x: = e where x is the variable and e is the expression. 4.8 Application: Algorithms2

Ordinarily, algorithm statements are executed one after another in the order in which they are written. Conditional statements allow this natural order to be overridden by using the current values of program variables to determine which algorithm statement will be executed next. 4.8 Application: Algorithms3

Conditional statements are denoted in one of two ways. 1.If (condition) then s 1 else s 2 2.If (condition) then s 1 Notice that we use indentation to indicate that the statements belong together. We can also bind statements by do and ending with end do. 4.8 Application: Algorithms4

1. The condition is evaluated by substituting the current values of all algorithm values appearing in it and evaluating the truth or falsity of the resulting statement. 2.If the condition is true, then s 1 is executed and execution moves to the next algorithm statement following the if-then- else statement. 3.If the condition is false, then s 2 is executed and execution moves to the next algorithm statement following the if-then- else statement. Note: Sometimes condition is called guard because it is stationed before s 1 and s 2 and restricts access to them. 4.8 Application: Algorithms5

Find the value of z when each of the algorithm segments is executed. 4.8 Application: Algorithms6 2. i := 3 if (i  3 or i > 6) then z := 2 else z := 0 1. i := 2 if (i > 3 or i  0) then z := 1 else z := 0

Iterative statements are used when a sequence of algorithm statements is to be executed over and over again. We use two types of iterative statements: while loops and for-next loops. 4.8 Application: Algorithms7

A while loop has the form while (condition) [ statements that make up the body of the loop] end while 4.8 Application: Algorithms8

1. The condition is evaluated by substituting the current values of all algorithm variables and evaluating the truth or falsity of the resulting statement. 2.If the condition is true, all statements in the body of the loop are executed in order. Then execution moves back to the beginning of the loop and the process repeats. 3.If the condition is false, execution passes to the next algorithm statement following the loop. Note: Each execution of the body of the loop is called an iteration of the loop. 4.8 Application: Algorithms9

i := 10 while (i > 0) display i i := i – 1 end while display “Blast Off” 4.8 Application: Algorithms10

A for-next loop has the form for variable := initial expression to final expression [ statements that make up the body of the loop] next (same) variable 4.8 Application: Algorithms11

1. The for-next loop variable is set equal to the value of the initial expression. 2.A check is made to determine whether the value of variables is less than or equal to the value of the final expression. 3.If the value of the variable is less than or equal to the value of the final expression, then the statements in the body of the loop are executed in order, variable is increased by 1, and execution returns back to step 2. 4.If the value of the variable is greater than the value of the final expression, then execution passes to the next algorithm statement following the loop. 4.8 Application: Algorithms12

Find the values of a after execution of the loop. a := 2 for i := 1 to 2 a := a/2 + 1/a next i 4.8 Application: Algorithms13

We generally include the following information when describing algorithms. 1.The name of the algorithm, together with a list of input and output variables. 2.A brief description of how the algorithm works. 3.The input variable names, labeled by data type (integer, real number, etc.). 4.The statements that make up the body of the algorithm, possibly with explanatory comments. 5.The output variable names, labeled by data type. 4.8 Application: Algorithms14

4.8 Application: Algorithms15

Make a trace table to trace the action of Algorithm for the given input values. 6. a = 26 d = Application: Algorithms16

Let a and b be integers that are not both zero. The greatest common divisor of a and b, denoted gcd(a, b), is that integer d with the following properties: 1. d is a common divisor of both a and b. In other words, d | a and d | b. 2.For all integers c, if c is a common divisor of both a and b, then c is less than or equal to d. In other words, for all integers c, if c | a and c | b, then c  d. 4.8 Application: Algorithms17

Lemma If r is a positive integer, then gcd(r, 0) = r. Lemma If a and b are any integers not both zero, and if q and r are any integers s.t. a = bq + r, then gcd(a, b) = gcd(b, r). 4.8 Application: Algorithms18

4.8 Application: Algorithms19

Make a trace table to trace the action of Algorithm for the given input values ,001 and Application: Algorithms20