Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop.

Similar presentations


Presentation on theme: "Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop."— Presentation transcript:

1 Lecture 15: Control Flow (cont)

2 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop algorithms. t Training session on statement level control structures (statements) t Demo programs t Exercises

3 3 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. t See source text on back page of handout t Edit, compile and run the program

4 4 Function getint(nmin, nmax) #include 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 nmax) { errorflag = 1; cout <<"\n Number " << inval << "not in range"; } } while (errorflag); return inval; }

5 5 Character I/O using end file controlled loop Design:read a character; while (character is not end-of-file indicator) { display the character just read; read a new character; } Demo programs CPPCountCharEOF.cpp and CCountCharEOF.cpp from previous lecture

6 6 Pythagorean triples Input: two positive integer values m, n; m>n Output:side1 = m 2 – n 2 side2 = 2mn hypotenuse = m 2 + n 2 Task: To compute 5 Pythagorean triples Task: To compute k Pythagorean triples

7 7 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; }

8 8 Pythagorean triples The solution: void main() { int I; int m, n; int k; cout > k; for (i=1; i<=k; i++) { cin >> m>> n; side1 = … side2 = … hypotenuse = … cout << ‘\n’ << side1 << ‘ ‘ << side2 < ‘ ‘ << hypotenuse; }

9 9 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;

10 10 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) { gcd = I; } i++; } cout << gcd;

11 11 Practical Tasks Write a program that calculates the factorial function N! = 1 * 2 * … * N.

12 12 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.)

13 13 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.)

14 14 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.)

15 15 More on loop statement(s) Extract from Friedman/Koffman, chapter 5

16 Repetition and Loop Statements Chapter 5

17 17 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

18 18 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

19 19 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

20 20 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);

21 21 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

22 22 Before lecture end Lecture: Control Flow. Repetition and loop structures More to read: Friedman/Koffman, Chapter 05

23 23 Thank You For Your Attention!


Download ppt "Lecture 15: Control Flow (cont). 2 Lecture Contents: t Design and implementation of programs illustrating linear algorithms, branch algorithms and loop."

Similar presentations


Ads by Google