Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k<=b; k++) {

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Recursion.
Start. More Loops 03/14/11 Look at geometric series example.
Computer Science 1620 Loops.
Introduction to Analysis of Algorithms
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS150 Introduction to Computer Science 1
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Induction and recursion
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Do-while loop Syntax do statement while (loop repetition condition)
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CPS120: Introduction to Computer Science Decision Making in Programs.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
CS101 Computer Programming I Chapter 4 Extra Examples.
Algorithm Design.
Recursive Algorithms &
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Solving Complex Problems. Review A subroutine is a set of instructions to perform a particular computation –used to solve subproblems of more complex.
Algorithm Analysis Algorithm Analysis Lectures 3 & 4 Resources Data Structures & Algorithms Analysis in C++ (MAW): Chap. 2 Introduction to Algorithms (Cormen,
Subroutines. Harder Problems  Examples so far: sum, sum between, minimum –straightforward computation –small number of intermediate variables –single.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Review Expressions and operators Iteration – while-loop – for-loop.
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.
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Program Development and Design Using C++, Third Edition
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
CS212: Data Structures and Algorithms
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Applied Discrete Mathematics Week 2: Functions and Sequences
Topic 6 Recursion.
Chapter 15 Recursion.
Analysis of Algorithms
Chapter 15 Recursion.
Algorithm Analysis CSE 2011 Winter September 2018.
FOR LOOPS.
Conditional Construct
A function with one argument
Faculty of Computer Science & Information System
Computer Science Core Concepts
CSC 143 Recursion.
The structure of programming
REPETITION Why Repetition?
COMPUTING.
Presentation transcript:

Control Structures: Examples

for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k<=b; k++) { x=x-k; } First iteration ( k=1, x=7 ) –test: 1<=3 is true –execute: x=7-1=6 –update: k=2 Second iteration ( k=2, x=6 ) –test: 2<=3 is true –execute: x=6-2=4 –update: k=3 Third iteration ( k=3, x=4 ) –test: 3<=3 is true –execute: x=4-3=1 –update: k=4 Fourth iteration ( k=4, x=1 ) –test: 4<=3 is false –exit loop

Another for-loop example Q: If a=2, b=4, x=1, and y=9, what are the values of x and y when the loop terminates? A: x=6, y=0 for(k=a; k<b; k++) { x=x+k; y=y-x; } First iteration ( k=2, x=1, y=9 ) –test: 2<4 is true –execute: x=1+2=3 y=9-3=6 –update: k=3 Second iteration ( k=3, x=3, y=6 ) –test: 3<4 is true –execute: x=3+3=6 y=6-6=0 –update: k=4 Third iteration ( k=4, x=6, y=0 ) –test: 4<3 is false –exit loop

while-loop example Q: What is the value of p when the loop terminates? A: p=150 p=0; t=1; n=10; while(n>t) { p=p+n*t; t=t+4; } First iteration ( t=1, p=0 ) –test: 10>1 is true –execute: p=0+10*1=10 t=1+4=5 Second iteration ( t=5, p=10 ) –test: 10>5 is true –execute: p=10+10*5=60 t= 5+4=9 Third iteration ( t=9, p=60 ) –test: 10>9 is true –execute: p=60+10*9=150 t= 9+4=13 Fourth iteration ( t=13, p=150 ) –test: 10>13 is false –exit loop

Another while-loop example Q: What are the values of z, p, and n after executing the following statements? A: z=0, p=13, n=-10 n=-1; z= 0; p= 1; while(p<=10) { z=n*z*p; p=p+4; n=n-3; } First iteration ( n=-1, z=0, p=1 ) –test: 1<=10 is true –execute: z=-1*0*1=0 p= 1+4=5 n=-1-3=-4 Second iteration ( n=-4, z=0, p=5 ) –test: 5<=10 is true –execute: z=-4*0*5=0 p= 5+4=9 n=-4-3=-7 Third iteration ( n=-7, z=0, p=9 ) –test: 9<=10 is true –execute: z=-7*0*9=0 p= 9+4=13 n=-7-3=-10 Fourth iteration: exit loop

Algorithm Design: Examples

Minimum of two integers Problem: Find the minimum of two integers

Minimum of two integers Analyze the problem –Inputs x first integer y second integer –Output min minimum of x and y –How do we find the minimum?? if the first number is smaller than the second number, then the first number is the minimum else, the second number is the minimum

Minimum of two integers Design an algorithm to solve the problem 1.Get input values for x and y 2.Compute minimum value 3.Return output value min if(x < y) min = x; else min = y;

Sum of positive integers Problem: Find the sum of all positive integers less than or equal to some positive integer n

Sum of positive integers Analyze the problem –Input n a positive integer –Output sum sum of all positive integers <=n –How to find the sum?? sum = … +n initialize sum=0 let k loop over the values [ 1, n ] compute sum=sum+k at each iteration of loop

Sum of positive integers Design an algorithm to solve the problem 1.Get input value for n 2.Compute sum of integers 1 through n 3.Return output value sum sum=0; for(k=1; k<=n; k++) { sum=sum+k; }

Factorial Problem: Given some positive integer n, compute the factorial of n

Definition: factorial The factorial of a positive integer n, denoted n!, is the product of the positive integers less than or equal to n For example –1! = 1 –2! = 2*1 = 2 –3! = 3*2*1 = 6 –4! = 4*3*2*1 = 24 –5! = 5*4*3*2*1 = 120 We define the factorial of 0 to be 1 –0! = 1

Factorial Analyze the problem –Input n a positive integer –Output fn n!, the factorial of n –How to find the factorial?? fn = 1*2*3* … *n initialize fn=1 let k loop over the values [ 2, n ] compute fn=fn*k at each iteration of loop why don’t we set fn=0 ? 

Factorial Design an algorithm to solve the problem 1.Get input value for n 2.Compute product of integers 2 through n 3.Return output value fn fn=1; for(k=2; k<=n; k++) { fn=fn*k; } what happens when n=1 ? 

Practice problems 1.Design an algorithm to compute the inclusive sum between two integers –example: for the input values 2 and 6, your algorithm should output 20 (because = 20) 2.Design an algorithm that computes x n for an integer x and a non-negative integer n –x n is defined as follows: x 0 = 1 if n=0, otherwise x n = x*x*x*x*x* *x n times

Practice problems 3.Design an algorithm to compute the maximum of two integers 4.Design an algorithm to compute the inclusive product between two integers –example: for the input values 3 and 6, your algorithm should output 360 (because 3*4*5 * 6 = 360)

Subroutines

Subroutines Set of instructions to perform a particular computation –subproblems of more complex problems –repeated computation (e.g. different inputs) –may be used in solving other problems Also called subprograms, methods, functions, procedures Subroutines are named Subroutines have zero or more parameters –list (possibly empty) of input values and their types Subroutines have return types

A subroutine to add two integers int add(int x, int y) { return x+y; }

A subroutine to add two integers int add(int x, int y) { return x+y; } subroutine name parameters (input) return value type (output) return statement output value

Minimum of two integers - algorithm Problem: Find the minimum of two integers Algorithm: 1.Get input values for x and y 2.Compute minimum value 3.Return output value min if(x < y) min = x; else min = y;

Minimum of two integers - subroutine int minimum(int x, int y) { int min; if(x < y) min=x; else min=y; return min; } variables used inside the subroutine must be declared variables can not have the same name as the subroutine  

Minimum of two integers v.2 int minimum(int x, int y) { if(x < y) return x; else return y; }

Sum of positive integers - algorithm Problem: Find the sum of all positive integers less than or equal to some positive integer n Algorithm: 1.Get input value for n 2.Compute sum of integers 1 through n 3.Return output value sum sum=0; for(k=1; k<=n; k++) { sum=sum+k; }

Sum of positive integers - subroutine int sum_integers(int n) { int k; int sum=0; for(k=1; k<=n; k++) { sum=sum+k; } return sum; }

Factorial - algorithm Problem: Given some positive integer n, compute n! Algorithm: 1.Get input value for n 2.Compute product of integers 2 through n 3.Return output value fn fn=1; for(k=2; k<=n; k++) { fn=fn*k; }

Factorial - subroutine int factorial(int n) { int k; int fn=1; for(k=2; k<=n; k++) { fn=fn*k; }

Practice problems Write subroutines to perform the following computations: 1.Compute the inclusive sum between two integers 2.Compute x n for an integer x and a non-negative integer n 3.Compute the maximum of two integers 4.Compute the inclusive product between two integers