Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31 Discussion 1D Winter19: week 4

Similar presentations


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

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

2 Good coding style guideline
(5) Don’t declare two variables whose lifecycles are overlapped

3 Variable lifecycle Does it compile?
int main() { if (100 < 150) { int a = 0; if (6 * 7 < 8) { a = 1; if ( < -11) a = 2; } a = 3; } a = 4; return 0; } Does it compile?

4 Variable lifecycle Does it compile then?
int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } Does it compile then?

5 Variable lifecycle Does it compile then? What’s the output?
int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } Does it compile then? What’s the output?

6 Variable lifecycle Does it compile then? What’s the output?
int main() { int a = 0; if (100 < 150) { int a = 1; if (6 * 7 < 888) { a = 2; } } cout << a << endl; return 0; } Does it compile then? What’s the output?

7 Why function? Save repeating codes Make the main function more compact

8 Function Can we write a function which exchange the values of two variables? void swap(int x, int y) { //TODO: how to do this? } int main() { int a = 3; int b = 6; swap(a, b); // Hope a = 6, b = 3 now return 0; }

9 Reference We can use reference (& after the data type) to achieve the task void swap(int &x, int &y) { int t = x; x = y; y = t; } int main() { int a = 3; int b = 6; swap(a, b); cout << "a=" << a << ", b=" << b << endl; // Hope a = 6, b = 3 now return 0; } x, y are going to control a, b

10 Have you done the exercise?
* ** *** **** ***** * ** *** **** ***** Done! ***** **** *** ** * ***** **** *** ** *

11 New challenges! * *** ***** *** ***** ******* ***** ******* ********* *********** ********* *********** ************* *** *** * *** ***** ******* ********* *********** *************

12 Global variable Global variables are those variables declared beyond any functions #include <iostream> int a; void increase() { a++; } int main() { a = 5; increase(); cout << a << endl; return 0; } What’s the output?

13 Global variable Using global variables is discouraged
That is, if there’s a way to avoid global variables, remove them It is because every function can change global variables. It’s hard to trace why and how the values are changed.

14 Array Array is for storing a sequence of elements
Array declaration: string apple[100]; Type name[size] Access an element by index: apple[3]; Note the range can only be 0 to N-1 if there are N elements

15 Array

16 Array What is the output?
int main() { int scores[5]; scores[0] = 10000; scores[1] = 2000; scores[2] = 300; scores[3] = 40; scores[4] = 5; cout << "Sum=" << (scores[0] + scores[1] + scores[2] scores[3] + scores[4]) << endl; return 0; } What is the output?

17 Array Getting interactive
int main() { int scores[5]; cout << "Enter student 1's score: "; cin >> scores[0]; cout << "Enter student 2's score: "; cin >> scores[1]; cout << "Enter student 3's score: "; cin >> scores[2]; cout << "Enter student 4's score: "; cin >> scores[3]; cout << "Enter student 5's score: "; cin >> scores[4]; cout << "Sum=" << (scores[0] + scores[1] + scores[2] scores[3] + scores[4]) << endl; return 0; } Getting interactive

18 Array Save the repetitions in input section by a loop
int main() { int scores[5]; for (int i = 0; i < 5; i++) { cout << "Enter student " << (i+1) << "'s score: "; cin >> scores[i]; } cout << "Sum=" << (scores[0] + scores[1] + scores[2] scores[3] + scores[4]) << endl; return 0; } Save the repetitions in input section by a loop

19 Array Save the repetitions in input section by a loop
int main() { int scores[5]; for (int i = 0; i < 5; i++) { cout << "Enter student " << (i+1) << "'s score: "; cin >> scores[i]; } int sum = 0; for (int i = 0; i < 5; i++) sum += scores[i]; cout << "Sum=" << sum << endl; return 0; } Save the repetitions in input section by a loop Further use a loop to simplify the processing section

20 Array If the index in an array is not within the valid range, the behavior is undefined For example, a[-1] = 999; However, sometimes the problem is not that simple: #include <iostream> int main() { int arr[10]; int a = 2; int b = 3; int x = a * b - 7; arr[x] = 1000; return 0; }

21 Array If the index in an array is not within the valid range, the behavior is undefined For example, a[-1] = 999; However, sometimes the problem is not that simple: C++ doesn’t check the boundary for you. It will run silently. And it will screw the memory (how?) That’s why this is one of the most annoying bugs int a = 2; int b = 3; int x = a * b - 7; arr[x] = 1000;

22 Array – zero-indexed From the explanation of how a program accesses the memory through an array, does it give you any hint why C++ array is zero-indexed?

23 Array – dimensionality
What can be the example of 1D-array? E.g., int score[100]; What can be the example of 2D-array? E.g., int color[100][100]; How about a 3D-array? How about an N-D-array?

24 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented?

25 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented?

26 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 72; str[1] = 101; str[2] = 108; str[3] = 108; str[4] = 111; str[5] = 0; printf("%s", str); return 0; }

27 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = 0; printf("%s", str); return 0; }

28 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6]; str[0] = 'H'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '\0'; printf("%s", str); return 0; }

29 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <stdio.h> int main() { char str[6] = "Hello"; printf("%s", str); return 0; }

30 String / character array
How do I print “Hello” on the screen if single-quote and double-quote are not invented? #include <iostream> using namespace std; int main() { string str = "Hello"; cout << str; return 0; }

31 String / character array
Change characters in a string #include <iostream> using namespace std; int main() { string str = "HELLO"; str[1] = '3'; str[4] = '0'; cout << str; return 0; }

32 String / character array
Double quotes are strings (“12345”) What are single quotes (‘a’, ‘3’, Treat them as a symbol for numbers ‘a’ is 97 ‘3’ is 51 is 40

33 String / character array
Double quotes are strings (“12345”) What are single quotes (‘a’, ‘3’, Treat them as a symbol for numbers ‘a’ is 97 ‘3’ is 51 is 40 That’s why ‘12’ are invalid – no such symbol! Escaped characters: ‘\n’ newline ‘\t’ tab ‘\0’ null character ‘\a’ used to be beep sound ‘\\’ a back-slash character

34 Puzzle 2 (warm-up) int main() { int a = 25 + 'Z'; cout << a; }

35 Puzzle 2 int main() { int a = '-'-'-'; cout << a; }


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

Similar presentations


Ads by Google