Lecture 15: Control Flow (cont) Lecture Contents: Design and implementation of programs illustrating linear algorithms, branch algorithms and loop algorithms. Training session on statement level control structures (statements) Demo programs Exercises 2 Function getint(nmin, nmax) Build an integer valued function with two formal parameters int getint(int nmin, int nmax); to return an integer that is in the range specified by the function’s two arguments nmin and nmax. A loop would repeatedly prompt the user for a value in the desired range. See source text on back page of handout Edit, compile and run the program 3 Function getint(nmin, nmax) #include <iostream> using namespace std; int getint(int, int); void main() { for (int k=1; k<=3; k++) { cout << "\nDriver program testing function getint(p1,p2)"; cout << "\nYou entered value " << getint(10, 20) << "\n"; } } int getint(int nmin, int nmax) { int inval, errorflag; do { errorflag = 0; cout<<"\nEnter integer in range from " << nmin << " to "<< nmax; cin >> inval; if (inval < nmin || inval > nmax) { errorflag = 1; cout <<"\n Number " << inval << "not in range"; } } while (errorflag); return inval; } 4 Pythagorean triples Input: two positive integer values m, n; m>n Output: side1 = m2 – n2 side2 = 2mn hypotenuse = m2 + n2 Task: To compute 5 Pythagorean triples Task: To compute k Pythagorean triples 5 Pythagorean triples The solution: void main() { int I; int m, n; for (i=1; i<=5; i++) { cin >> m>> n; side1 = … side2 = … hypotenuse = … cout << ‘\n’ << side1 << ‘ ‘ << side2 < ‘ ‘ << hypotenuse; } } 6 Pythagorean triples The solution: void main() { int I; int m, n; int k; cout << “\n Enter value for k”; cin >> k; for (i=1; i<=k; i++) { cin >> m>> n; side1 = … side2 = … hypotenuse = … cout << ‘\n’ << side1 << ‘ ‘ << side2 < ‘ ‘ << hypotenuse; } } 7 Euclid’s algorithm – GCD of two positive integer numbers Input: two positive integer values m, n Algorithm: loop division m%n while the remainder is != 0 int m, n, rem, gcd; cin >> m >> n; while ( (rem = m%n)!= 0) { m = n; n = rem; } gcd = n; cout << “\nGCD is =“ << gcd << endl; 8 Trivial algorithm – GCD of two positive integer numbers Input: two positive integer values m, n Algorithm: loop – looking for common divisors of m and n int m, n, i=1, gcd; cin >> m >> n; while ( i<m || i<n ) { if (m%i==0 && n%i==0) { i++; } cout << gcd = I; } gcd; 9 Practical Tasks Write a program that calculates the factorial function N! = 1 * 2 * … * N. 10 Practical Tasks Write a program to display the first 10 numbers that are part of the Fibonacci sequence. (The Fibonacci sequence begins 1, 1, 2, 3, 5, 8, …, where each new number in the sequence is found by adding the previous two numbers in the sequence.) 11 Practical Tasks Write a program to display the first n (n is input value) numbers that are part of the Fibonacci sequence. (The Fibonacci sequence begins 1, 1, 2, 3, 5, 8, …, where each new number in the sequence is found by adding the previous two numbers in the sequence.) 12 Practical Tasks Write a program to display all the numbers between 1 and 100 that are part of the Fibonacci sequence. (The Fibonacci sequence begins 1, 1, 2, 3, 5, 8, …, where each new number in the sequence is found by adding the previous two numbers in the sequence.) 13 More on loop statement(s) Extract from Friedman/Koffman, chapter 5 14 Repetition and Loop Statements Chapter 5 Exercise 15.1 Build programs based on loop algorithms using the repetition statements: counter controlled and/or logically controlled loops with for, while and do-while statements; Problem: Test the function int getint(int nmin, int nmax) to return an integer that is in the range specified by its two arguments min and nmax 16 Exercise 15.2 Build programs based on loop algorithms using the repetition statements: counter controlled and/or logically controlled loops with for, while and do-while statements; Problem: Character I/O using end file controlled loop 17 Exercise 15.3 Build programs based on loop algorithms using the repetition statements: counter controlled and/or logically controlled loops with for, while and do-while statements; Problem: Pythagorean triples 18 Exercise 15.4 Build programs based on loop algorithms using the repetition statements: counter controlled and/or logically controlled loops with for, while and do-while statements; Problem: Greatest Common Divisor (Euclid’s algorithm int gcdi(int m, int n); 19 Exercise 15.4 Build programs based on loop algorithms using the repetition statements: counter controlled and/or logically controlled loops with for, while and do-while statements; Problem: Factorial function int facti(int n); n! = 1 * 2 * . . . * n 0! = 1 20 Before lecture end Lecture: Control Flow. Repetition and loop structures More to read: Friedman/Koffman, Chapter 05 21 Thank You For Your Attention! 22
© Copyright 2024 Paperzz