Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31 Discussion 1D Winter19: week 3

Similar presentations


Presentation on theme: "CS31 Discussion 1D Winter19: week 3"— Presentation transcript:

1 CS31 Discussion 1D Winter19: week 3
TA: Behnam Shahbazi Credit to former TAs: Chelsea Ju and Bo-Jhang Ho

2 for ( ; ; ) { } Syntax of for loop Execution flow: Initial value
Execution condition Increment value Line 1 Line 2 Line 3 Line 4 Line 5 } Execution flow: Initial Condition Block Increment Condition Block Increment Condition Block Increment Condition Block Increment Condition

3 Exercise: summation program
int main() { int i; int sum = 0; for (i = 1; i <= 5; i++) sum += i; cout << sum << endl; cout << i << endl; return 0; } Initial value Execution condition Increment value Block Result?

4 while ( ) { } Syntax of while loop Execution flow: Execution condition
Line 1 Line 2 Line 3 Line 4 Line 5 } Execution flow: Condition Block Condition Block Condition Block Condition

5 Exercise: Counting number of trailing zeros in an integer
For example: Input 27000, output 3 Input 290, output 1 Input , output 7 Input 123, output 0

6 Exercise: Counting number of trailing zeros in an integer
int main() { int num; cout << "Enter a number: "; cin >> num; int count = 0; while (num % 10 == 0) { num /= 10; count++; } cout << count << " trailing zeros"; return 0; } Execution condition Block What’s the result when input is 27000? What’s the result when input is 290?

7 Syntax of do-while loop
Line 1 Line 2 Line 3 Line 4 Line 5 } while ( ); Execution condition Important reminder: Don’t forget the semicolon in the end of while clause! Execution flow: Block Condition Block Condition Block Condition

8 Exercise: Guessing number game
int main() { int target = 15; int num; do { cout << "Enter a number: "; cin >> num; } while (num != target); cout << "You guess is correct!"; return 0; } Execution condition Block Discussion: Why do I put != instead of == in the while condition?

9 Comparison of 3 types of loop
for ( ; ; ) { Initial Condition Increment For When the task is sequential A typical usage is for counting Line 1 Line 2 } while ( ) { Condition While We don’t know how many times we have to repeat the block, but know when to repeat (or stop) Line 1 Line 2 } do { Do-while Similar to while, but if we must execute the block at least once Don’t forget the semicolon! Line 1 Line 2 } while ( ); Condition

10 Is it possible to replace one type of loop to another?
Technically we can But it doesn’t make any sense The 3 kinds of loop fit into different scenario

11 Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial
Condition Increment Line 1 Line 2 } }

12 Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial
Condition Increment Condition Line 1 Line 2 Line 1 Line 2 } }

13 Loop replacement For -> While for ( ; ; ) { while ( ) { } } Initial
Condition Increment Condition Line 1 Line 2 Line 1 Line 2 } Increment }

14 Loop replacement While -> For for ( ; ; ) { while ( ) { } }
Condition Line 1 Line 2 } }

15 Loop replacement While -> For for ( ; ; ) { while ( ) { } }
Condition Condition Line 1 Line 2 Line 1 Line 2 } }

16 Nested loops: Printing a rectangle
int main() { for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) { cout << "*"; } cout << endl; } return 0; }

17 Nested loops: Printing a triangle
int main() { int size; cout << "Enter size: "; cin >> size; for (int r = 1; r <= size; r++) { for (int c = 1; c <= r; c++) { cout << "*"; } cout << endl; } return 0; }

18 Take-home exercise: Can you print triangles in different angle?
* ** *** **** ***** * ** *** **** ***** Done! ***** **** *** ** * ***** **** *** ** *

19 String demystify: (1) Get the length
int main() { string s = "Hello"; cout << "Length: " << s.size() << endl; return 0; } Result? Length: 5

20 String demystify: (2) Dump all the characters
int main() { string s = "Hello"; for (int i = 0; i < s.size(); i++) cout << "index " << i << ": " << s[i] << endl; return 0; } Result? index 0: H index 1: e index 2: l index 3: l index 4: o Variable s is a string s[<a number>] Get the character with the corresponding index The type is a character

21 String demystify: (3) Dump all the characters
int main() { string s = "Hello"; int count = 0; for (int i = 0; i < s.size(); i++) if (s[i] == 'e') count++; cout << "Number of Es: " << count << endl; return 0; } Result? Number of Es: 1 ‘e’ is a character literal

22 More exercises Compute factorization, e.g., 7!
Compute combination number, e.g., 10 choose 6 List all the prime numbers up to 100 Great common divisor of two numbers

23 Puzzle time: What does the code print?
int main() { int n = 5; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << (i / j) * (j / i); cout << endl; } return 0; }

24 Puzzle time: What does the code print?
int main() { int n = 6; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) cout << (i / j) * (j / i); cout << endl; } return 0; }

25 What is a function Take some numbers, produce a result value int
double double string Take some numbers, produce a result value

26 What is a function f(x) = 2x + 3 What is f(5)?

27 What is a function f(x, y) = 3x + 2y - xy What is f(4, 1)?

28 What is a function What is the output?
int square(int n) { return n * n; } int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } What is the output?

29 What is a function int square(int n); int main() { int a = 3; int b = 6; a = a * b + square(a + b); cout << a << endl; return 0; } int square(int n) { return n * n; } When a function is called, it always look back and try to find the function. If you want to declare it before the callee, you have to declare it first.

30 Don’t memorize them! ASCII Characters #include <cctype>
Some functions in this library: int isalnum(int c); // alphanumeric int isdigit(int c); // decimal digit int islower(int c); // lowercase letter int isupper(int c); // uppercase letter int isspace(int c); // white-space int toupper(int c); // convert to uppercase int tolower(int c); // convert to lowercase Don’t memorize them!

31 #include <cctype> example

32 Worksheet 2 Let’s team up and work on worksheet 2


Download ppt "CS31 Discussion 1D Winter19: week 3"

Similar presentations


Ads by Google